Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,12 +3,11 @@ import base64
|
|
| 3 |
from PIL import Image
|
| 4 |
from io import BytesIO
|
| 5 |
import gradio as gr
|
| 6 |
-
import uuid
|
| 7 |
-
import os
|
| 8 |
|
| 9 |
-
API_KEY = "sk-
|
| 10 |
|
| 11 |
def generate_image(prompt):
|
|
|
|
| 12 |
response = requests.post(
|
| 13 |
"https://api.stability.ai/v1/generation/stable-diffusion-v1-5/text-to-image",
|
| 14 |
headers={
|
|
@@ -25,20 +24,23 @@ def generate_image(prompt):
|
|
| 25 |
}
|
| 26 |
)
|
| 27 |
|
|
|
|
| 28 |
if response.status_code != 200:
|
| 29 |
return f"Error: {response.status_code} - {response.text}"
|
| 30 |
|
| 31 |
-
#
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
image = Image.open(BytesIO(image_data))
|
| 35 |
|
| 36 |
return image
|
| 37 |
|
| 38 |
-
#
|
| 39 |
-
gr.Interface(
|
| 40 |
fn=generate_image,
|
| 41 |
-
inputs=gr.Textbox(label="Enter
|
| 42 |
-
outputs=gr.Image(type="pil", label="Generated Image"),
|
| 43 |
-
title="Speech2Image Generator
|
| 44 |
-
)
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
from PIL import Image
|
| 4 |
from io import BytesIO
|
| 5 |
import gradio as gr
|
|
|
|
|
|
|
| 6 |
|
| 7 |
+
API_KEY = "sk-ESkTZ28wTlIcedRXuq2KTYbgunw578RkXprkewhJpMsu69Ec" # REPLACE THIS!
|
| 8 |
|
| 9 |
def generate_image(prompt):
|
| 10 |
+
# Step 1: Make the API request
|
| 11 |
response = requests.post(
|
| 12 |
"https://api.stability.ai/v1/generation/stable-diffusion-v1-5/text-to-image",
|
| 13 |
headers={
|
|
|
|
| 24 |
}
|
| 25 |
)
|
| 26 |
|
| 27 |
+
# Step 2: Error check
|
| 28 |
if response.status_code != 200:
|
| 29 |
return f"Error: {response.status_code} - {response.text}"
|
| 30 |
|
| 31 |
+
# Step 3: Convert base64 to PIL.Image
|
| 32 |
+
image_data = response.json()["artifacts"][0]["base64"]
|
| 33 |
+
image = Image.open(BytesIO(base64.b64decode(image_data))).convert("RGB") # Ensure RGB
|
|
|
|
| 34 |
|
| 35 |
return image
|
| 36 |
|
| 37 |
+
# Step 4: Gradio Interface — PIL Image Return Only
|
| 38 |
+
demo = gr.Interface(
|
| 39 |
fn=generate_image,
|
| 40 |
+
inputs=gr.Textbox(label="Enter a prompt"),
|
| 41 |
+
outputs=gr.Image(type="pil", label="Generated Image"),
|
| 42 |
+
title="✨ Speech2Image Generator ✨"
|
| 43 |
+
)
|
| 44 |
+
|
| 45 |
+
demo.launch()
|
| 46 |
+
|