Spaces:
Runtime error
Runtime error
Upload folder using huggingface_hub
Browse files- api/index.py +31 -7
api/index.py
CHANGED
|
@@ -60,9 +60,20 @@ def image_classifier(moodboard, starter_image, image_strength, prompt):
|
|
| 60 |
pil_image = Image.fromarray(moodboard.astype('uint8'))
|
| 61 |
starter_image_pil = Image.fromarray(starter_image.astype('uint8'))
|
| 62 |
|
| 63 |
-
# Resize the starter image if
|
| 64 |
if starter_image_pil.size[0] > 768 or starter_image_pil.size[1] > 768:
|
| 65 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 66 |
|
| 67 |
openai_response = call_openai(pil_image)
|
| 68 |
openai_response = openai_response.replace('moodboard', '')
|
|
@@ -84,7 +95,8 @@ def image_classifier(moodboard, starter_image, image_strength, prompt):
|
|
| 84 |
"image": "data:image/jpeg;base64," + starter_image_base64,
|
| 85 |
"apply_watermark": False,
|
| 86 |
"num_inference_steps": 25,
|
| 87 |
-
"prompt_strength": 1-image_strength
|
|
|
|
| 88 |
}
|
| 89 |
|
| 90 |
output = replicate.run(
|
|
@@ -97,9 +109,21 @@ def image_classifier(moodboard, starter_image, image_strength, prompt):
|
|
| 97 |
print(image_url)
|
| 98 |
response = requests.get(image_url)
|
| 99 |
print(response)
|
| 100 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 101 |
|
| 102 |
-
return
|
| 103 |
|
| 104 |
|
| 105 |
# app = Flask(__name__)
|
|
@@ -108,5 +132,5 @@ def image_classifier(moodboard, starter_image, image_strength, prompt):
|
|
| 108 |
# @app.route("/")
|
| 109 |
# def index():
|
| 110 |
|
| 111 |
-
demo = gr.Interface(fn=image_classifier, inputs=["image", "image", gr.Slider(0, 1, step=0.025, value=0.2, label="Image Strength"), "text"], outputs="image")
|
| 112 |
-
demo.launch(share=
|
|
|
|
| 60 |
pil_image = Image.fromarray(moodboard.astype('uint8'))
|
| 61 |
starter_image_pil = Image.fromarray(starter_image.astype('uint8'))
|
| 62 |
|
| 63 |
+
# Resize the starter image if either dimension is larger than 768 pixels
|
| 64 |
if starter_image_pil.size[0] > 768 or starter_image_pil.size[1] > 768:
|
| 65 |
+
# Calculate the new size while maintaining the aspect ratio
|
| 66 |
+
if starter_image_pil.size[0] > starter_image_pil.size[1]:
|
| 67 |
+
# Width is larger than height
|
| 68 |
+
new_width = 768
|
| 69 |
+
new_height = int((768 / starter_image_pil.size[0]) * starter_image_pil.size[1])
|
| 70 |
+
else:
|
| 71 |
+
# Height is larger than width
|
| 72 |
+
new_height = 768
|
| 73 |
+
new_width = int((768 / starter_image_pil.size[1]) * starter_image_pil.size[0])
|
| 74 |
+
|
| 75 |
+
# Resize the image
|
| 76 |
+
starter_image_pil = starter_image_pil.resize((new_width, new_height), Image.LANCZOS)
|
| 77 |
|
| 78 |
openai_response = call_openai(pil_image)
|
| 79 |
openai_response = openai_response.replace('moodboard', '')
|
|
|
|
| 95 |
"image": "data:image/jpeg;base64," + starter_image_base64,
|
| 96 |
"apply_watermark": False,
|
| 97 |
"num_inference_steps": 25,
|
| 98 |
+
"prompt_strength": 1-image_strength,
|
| 99 |
+
"num_outputs": 3,
|
| 100 |
}
|
| 101 |
|
| 102 |
output = replicate.run(
|
|
|
|
| 109 |
print(image_url)
|
| 110 |
response = requests.get(image_url)
|
| 111 |
print(response)
|
| 112 |
+
img1 = Image.open(io.BytesIO(response.content))
|
| 113 |
+
|
| 114 |
+
image_url = output[1]
|
| 115 |
+
print(image_url)
|
| 116 |
+
response = requests.get(image_url)
|
| 117 |
+
print(response)
|
| 118 |
+
img2 = Image.open(io.BytesIO(response.content))
|
| 119 |
+
|
| 120 |
+
image_url = output[2]
|
| 121 |
+
print(image_url)
|
| 122 |
+
response = requests.get(image_url)
|
| 123 |
+
print(response)
|
| 124 |
+
img3 = Image.open(io.BytesIO(response.content))
|
| 125 |
|
| 126 |
+
return [img1, img2, img3] # Return the image object
|
| 127 |
|
| 128 |
|
| 129 |
# app = Flask(__name__)
|
|
|
|
| 132 |
# @app.route("/")
|
| 133 |
# def index():
|
| 134 |
|
| 135 |
+
demo = gr.Interface(fn=image_classifier, inputs=["image", "image", gr.Slider(0, 1, step=0.025, value=0.2, label="Image Strength"), "text"], outputs=["image", "image", "image"])
|
| 136 |
+
demo.launch(share=False)
|