Spaces:
Sleeping
Sleeping
Upload folder using huggingface_hub
Browse files- .env +1 -0
- .gitignore +13 -0
- README.md +3 -9
- index.py +147 -0
- requirements.txt +2 -0
.env
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
REPLICATE_API_TOKEN=r8_DAzyOBdCwUdt0b26ZMPWLyvyHTh55uh2Lwb3c
|
.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: indigo
|
| 5 |
-
colorTo: red
|
| 6 |
sdk: gradio
|
| 7 |
-
sdk_version: 4.
|
| 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: between_two_words
|
| 3 |
+
app_file: index.py
|
|
|
|
|
|
|
| 4 |
sdk: gradio
|
| 5 |
+
sdk_version: 4.36.1
|
|
|
|
|
|
|
| 6 |
---
|
|
|
|
|
|
index.py
ADDED
|
@@ -0,0 +1,147 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
+
REPLICATE_API_TOKEN = os.getenv('REPLICATE_API_TOKEN')
|
| 17 |
+
|
| 18 |
+
def sdxl_api(weighted_prompt, weight_amt, prompt, starter_img, prompt_strength):
|
| 19 |
+
input = {
|
| 20 |
+
"seed": 1235,
|
| 21 |
+
"image": starter_img,
|
| 22 |
+
"prompt": "high quality render of (" + weighted_prompt + ")" + str(weight_amt) + " " + prompt + ", minimalist and simple on a white background",
|
| 23 |
+
"negative_prompt": "worst quality, low quality, illustration, 2d, painting, cartoons, sketch, logo, buttons, markings, text, wires, complex, screws, nails, construction, partial, multiple, pattern, words",
|
| 24 |
+
"prompt_weighting": True,
|
| 25 |
+
#"width": 768,
|
| 26 |
+
#"height": 768,
|
| 27 |
+
"apply_watermark" : False,
|
| 28 |
+
"scheduler":"K_EULER_ANCESTRAL",
|
| 29 |
+
"prompt_strength":prompt_strength
|
| 30 |
+
|
| 31 |
+
}
|
| 32 |
+
|
| 33 |
+
output = replicate.run(
|
| 34 |
+
"batouresearch/sdxl-weighting-prompts:66175a2993706e1721076d5c7f92f0c81ec6d065ec20717527f05dd8528a1fc7",
|
| 35 |
+
input=input
|
| 36 |
+
)
|
| 37 |
+
|
| 38 |
+
response = requests.get(output[0])
|
| 39 |
+
return Image.open(io.BytesIO(response.content))
|
| 40 |
+
|
| 41 |
+
def img2imgstarter(prompt):
|
| 42 |
+
input = {
|
| 43 |
+
"prompt": "high quality 3D render of a " + prompt + ", front-view, minimalist and simple mockup on a white background",
|
| 44 |
+
"output_format": "jpg",
|
| 45 |
+
"output_quality": 75,
|
| 46 |
+
"steps": 14
|
| 47 |
+
}
|
| 48 |
+
|
| 49 |
+
try:
|
| 50 |
+
output = replicate.run(
|
| 51 |
+
"stability-ai/stable-diffusion-3",
|
| 52 |
+
input=input
|
| 53 |
+
)
|
| 54 |
+
except Exception as e:
|
| 55 |
+
raise gr.Error(f"Error: {e}")
|
| 56 |
+
|
| 57 |
+
try:
|
| 58 |
+
image_url = output[0]
|
| 59 |
+
response = requests.get(image_url)
|
| 60 |
+
img1 = Image.open(io.BytesIO(response.content))
|
| 61 |
+
|
| 62 |
+
# Save the starter image to a bytes buffer
|
| 63 |
+
buffered = io.BytesIO()
|
| 64 |
+
img1.save(buffered, format="JPEG")
|
| 65 |
+
|
| 66 |
+
# Encode the starter image to base64
|
| 67 |
+
return "data:image/jpeg;base64," + base64.b64encode(buffered.getvalue()).decode('utf-8')
|
| 68 |
+
|
| 69 |
+
except Exception as e:
|
| 70 |
+
raise gr.Error(f"Image download failed: {e}")
|
| 71 |
+
|
| 72 |
+
def main(text1, text2, prompt, dropdown_value, image_input):
|
| 73 |
+
|
| 74 |
+
if image_input is not None:
|
| 75 |
+
starter_image_pil = Image.fromarray(image_input.astype('uint8'))
|
| 76 |
+
|
| 77 |
+
# Resize the starter image if either dimension is larger than 768 pixels
|
| 78 |
+
if starter_image_pil.size[0] > 768 or starter_image_pil.size[1] > 768:
|
| 79 |
+
# Calculate the new size while maintaining the aspect ratio
|
| 80 |
+
if starter_image_pil.size[0] > starter_image_pil.size[1]:
|
| 81 |
+
# Width is larger than height
|
| 82 |
+
new_width = 768
|
| 83 |
+
new_height = int((768 / starter_image_pil.size[0]) * starter_image_pil.size[1])
|
| 84 |
+
else:
|
| 85 |
+
# Height is larger than width
|
| 86 |
+
new_height = 768
|
| 87 |
+
new_width = int((768 / starter_image_pil.size[1]) * starter_image_pil.size[0])
|
| 88 |
+
|
| 89 |
+
# Resize the image
|
| 90 |
+
starter_image_pil = starter_image_pil.resize((new_width, new_height), Image.LANCZOS)
|
| 91 |
+
# Save the starter image to a bytes buffer
|
| 92 |
+
buffered = io.BytesIO()
|
| 93 |
+
starter_image_pil.save(buffered, format="JPEG")
|
| 94 |
+
|
| 95 |
+
# Encode the starter image to base64
|
| 96 |
+
starter_img = "data:image/jpeg;base64," + base64.b64encode(buffered.getvalue()).decode('utf-8')
|
| 97 |
+
else:
|
| 98 |
+
starter_img = img2imgstarter(prompt)
|
| 99 |
+
|
| 100 |
+
outputs = [text1]
|
| 101 |
+
output_amts = [2.5, 2.25, 1.5]
|
| 102 |
+
for amt in output_amts:
|
| 103 |
+
outputs.append(sdxl_api(text1, amt, prompt, starter_img, .95))
|
| 104 |
+
|
| 105 |
+
outputs.append(sdxl_api("", 1, prompt, starter_img, .5))
|
| 106 |
+
|
| 107 |
+
output_amts.reverse()
|
| 108 |
+
for amt in output_amts:
|
| 109 |
+
outputs.append(sdxl_api(text2, amt, prompt, starter_img, .95))
|
| 110 |
+
|
| 111 |
+
outputs.append(text2)
|
| 112 |
+
return outputs
|
| 113 |
+
|
| 114 |
+
|
| 115 |
+
with gr.Blocks() as demo:
|
| 116 |
+
with gr.Row():
|
| 117 |
+
with gr.Column(scale=2):
|
| 118 |
+
text1 = gr.Textbox(value="organic-shaped", label="Word 1 (Left)")
|
| 119 |
+
text2 = gr.Textbox(value="geometric-shaped", label="Word 2 (Right)")
|
| 120 |
+
with gr.Column(scale=1):
|
| 121 |
+
dropdown = gr.Dropdown(choices=["prompt", "prompt+image"], value="prompt", label="Input Type")
|
| 122 |
+
prompt = gr.Textbox(label="Prompt")
|
| 123 |
+
image_input = gr.Image(label="Image Input", visible=False)
|
| 124 |
+
|
| 125 |
+
submit_btn = gr.Button("Submit")
|
| 126 |
+
|
| 127 |
+
with gr.Row():
|
| 128 |
+
output1 = gr.Textbox(label="Word 1")
|
| 129 |
+
output2 = gr.Image(label="Output 1")
|
| 130 |
+
output3 = gr.Image(label="Output 2")
|
| 131 |
+
with gr.Row():
|
| 132 |
+
output4 = gr.Image(label="Output 3")
|
| 133 |
+
output5 = gr.Image(label="Output 4")
|
| 134 |
+
output6 = gr.Image(label="Output 5")
|
| 135 |
+
with gr.Row():
|
| 136 |
+
output7 = gr.Image(label="Output 6")
|
| 137 |
+
output8 = gr.Image(label="Output 7")
|
| 138 |
+
output9 = gr.Textbox(label="Word 2")
|
| 139 |
+
|
| 140 |
+
submit_btn.click(main, inputs=[text1, text2, prompt, dropdown, image_input], outputs=[output1, output2, output3, output4, output5, output6, output7, output8, output9])
|
| 141 |
+
|
| 142 |
+
def update_visibility(selected):
|
| 143 |
+
return gr.update(visible=(selected in ["prompt", "prompt+image"])), gr.update(visible=(selected in ["prompt+image"]))
|
| 144 |
+
|
| 145 |
+
dropdown.change(fn=update_visibility, inputs=dropdown, outputs=[prompt, image_input])
|
| 146 |
+
|
| 147 |
+
demo.launch(share=False)
|
requirements.txt
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
|
|
|
| 1 |
+
gradio
|
| 2 |
+
replicate
|