Spaces:
Runtime error
Runtime error
Upload folder using huggingface_hub
Browse files- .gitignore +13 -0
- README.md +2 -8
- index.py +92 -0
- requirements.txt +2 -0
.gitignore
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
.vercel
|
| 2 |
+
*.log
|
| 3 |
+
*.pyc
|
| 4 |
+
__pycache__
|
| 5 |
+
|
| 6 |
+
# Environments
|
| 7 |
+
.env
|
| 8 |
+
.venv
|
| 9 |
+
env/
|
| 10 |
+
venv/
|
| 11 |
+
ENV/
|
| 12 |
+
env.bak/
|
| 13 |
+
venv.bak/
|
README.md
CHANGED
|
@@ -1,12 +1,6 @@
|
|
| 1 |
---
|
| 2 |
-
title:
|
| 3 |
-
|
| 4 |
-
colorFrom: yellow
|
| 5 |
-
colorTo: gray
|
| 6 |
sdk: gradio
|
| 7 |
sdk_version: 4.36.1
|
| 8 |
-
app_file: app.py
|
| 9 |
-
pinned: false
|
| 10 |
---
|
| 11 |
-
|
| 12 |
-
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
|
|
|
| 1 |
---
|
| 2 |
+
title: repeating-pattern-generator
|
| 3 |
+
app_file: index.py
|
|
|
|
|
|
|
| 4 |
sdk: gradio
|
| 5 |
sdk_version: 4.36.1
|
|
|
|
|
|
|
| 6 |
---
|
|
|
|
|
|
index.py
ADDED
|
@@ -0,0 +1,92 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import base64
|
| 3 |
+
import numpy as np
|
| 4 |
+
from PIL import Image, ImageChops, ImageDraw
|
| 5 |
+
|
| 6 |
+
import io
|
| 7 |
+
import requests
|
| 8 |
+
import replicate
|
| 9 |
+
import gradio as gr
|
| 10 |
+
|
| 11 |
+
from dotenv import load_dotenv, find_dotenv
|
| 12 |
+
|
| 13 |
+
# Locate the .env file
|
| 14 |
+
dotenv_path = find_dotenv()
|
| 15 |
+
load_dotenv(dotenv_path)
|
| 16 |
+
|
| 17 |
+
REPLICATE_API_TOKEN = os.getenv('REPLICATE_API_TOKEN')
|
| 18 |
+
|
| 19 |
+
def generate_pattern(image, prompt):
|
| 20 |
+
# Convert the numpy array to a PIL image
|
| 21 |
+
starter_image_pil = Image.fromarray(image.astype('uint8'))
|
| 22 |
+
|
| 23 |
+
# Resize the starter image if either dimension is larger than 768 pixels
|
| 24 |
+
if starter_image_pil.size[0] > 768 or starter_image_pil.size[1] > 768:
|
| 25 |
+
# Calculate the new size while maintaining the aspect ratio
|
| 26 |
+
if starter_image_pil.size[0] > starter_image_pil.size[1]:
|
| 27 |
+
# Width is larger than height
|
| 28 |
+
new_width = 768
|
| 29 |
+
new_height = int((768 / starter_image_pil.size[0]) * starter_image_pil.size[1])
|
| 30 |
+
else:
|
| 31 |
+
# Height is larger than width
|
| 32 |
+
new_height = 768
|
| 33 |
+
new_width = int((768 / starter_image_pil.size[1]) * starter_image_pil.size[0])
|
| 34 |
+
|
| 35 |
+
# Resize the image
|
| 36 |
+
starter_image_pil = starter_image_pil.resize((new_width, new_height), Image.LANCZOS)
|
| 37 |
+
|
| 38 |
+
|
| 39 |
+
# Move the image horizontally and vertically by 50%
|
| 40 |
+
width, height = starter_image_pil.size
|
| 41 |
+
horizontal_shift = width // 2
|
| 42 |
+
vertical_shift = height // 2
|
| 43 |
+
|
| 44 |
+
transformed_image_pil = ImageChops.offset(starter_image_pil, horizontal_shift, vertical_shift)
|
| 45 |
+
|
| 46 |
+
|
| 47 |
+
# Create a new image with black background and white cross
|
| 48 |
+
cross_image_pil = Image.new('RGB', (width, height), 'black')
|
| 49 |
+
draw = ImageDraw.Draw(cross_image_pil)
|
| 50 |
+
line_width = 50
|
| 51 |
+
# Draw vertical line
|
| 52 |
+
draw.rectangle([(width // 2 - line_width // 2, 0), (width // 2 + line_width // 2, height)], fill='white')
|
| 53 |
+
# Draw horizontal line
|
| 54 |
+
draw.rectangle([(0, height // 2 - line_width // 2), (width, height // 2 + line_width // 2)], fill='white')
|
| 55 |
+
|
| 56 |
+
buffered = io.BytesIO()
|
| 57 |
+
transformed_image_pil.save(buffered, format="JPEG")
|
| 58 |
+
image_base64 = base64.b64encode(buffered.getvalue()).decode('utf-8')
|
| 59 |
+
|
| 60 |
+
buffered = io.BytesIO()
|
| 61 |
+
cross_image_pil.save(buffered, format="JPEG")
|
| 62 |
+
cross_base64 = base64.b64encode(buffered.getvalue()).decode('utf-8')
|
| 63 |
+
|
| 64 |
+
input = {
|
| 65 |
+
"prompt": prompt + " smooth background",
|
| 66 |
+
"negative_prompt": "worst quality, low quality, cartoons, sketch, ugly, lowres",
|
| 67 |
+
"image": "data:image/jpeg;base64," + image_base64,
|
| 68 |
+
"mask": "data:image/jpeg;base64," + cross_base64,
|
| 69 |
+
"num_inference_steps": 25,
|
| 70 |
+
"num_outputs": 3,
|
| 71 |
+
}
|
| 72 |
+
|
| 73 |
+
output = replicate.run(
|
| 74 |
+
"lucataco/sdxl-inpainting:a5b13068cc81a89a4fbeefeccc774869fcb34df4dbc92c1555e0f2771d49dde7",
|
| 75 |
+
input=input
|
| 76 |
+
)
|
| 77 |
+
|
| 78 |
+
images = []
|
| 79 |
+
for i in range(min(len(output), 3)):
|
| 80 |
+
image_url = output[i]
|
| 81 |
+
response = requests.get(image_url)
|
| 82 |
+
images.append(Image.open(io.BytesIO(response.content)))
|
| 83 |
+
|
| 84 |
+
# Add empty images if fewer than 3 were returned
|
| 85 |
+
while len(images) < 3:
|
| 86 |
+
images.append(Image.new('RGB', (width, height), 'gray'))
|
| 87 |
+
|
| 88 |
+
return images
|
| 89 |
+
|
| 90 |
+
|
| 91 |
+
demo = gr.Interface(fn=generate_pattern, inputs=["image", "text"], outputs=["image", "image", "image"])
|
| 92 |
+
demo.launch(share=False)
|
requirements.txt
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
|
|
|
| 1 |
+
gradio
|
| 2 |
+
replicate
|