Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,28 +1,39 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import requests
|
| 3 |
-
from PIL import Image
|
| 4 |
import io
|
|
|
|
| 5 |
import os
|
|
|
|
| 6 |
|
| 7 |
API_URL = "https://api-inference.huggingface.co/models/CompVis/stable-diffusion-v1-4"
|
| 8 |
headers = {"Authorization": f"Bearer {os.getenv('HF_API_KEY')}"}
|
| 9 |
|
| 10 |
def generate_image(prompt):
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
return image
|
| 16 |
-
|
| 17 |
-
return f"
|
| 18 |
|
| 19 |
iface = gr.Interface(
|
| 20 |
fn=generate_image,
|
| 21 |
inputs=gr.Textbox(label="Enter your prompt"),
|
| 22 |
outputs=gr.Image(type="pil", label="Generated Image"),
|
| 23 |
title="Text-to-Image Generator",
|
| 24 |
-
theme="default"
|
| 25 |
)
|
| 26 |
|
| 27 |
iface.launch()
|
| 28 |
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import requests
|
|
|
|
| 3 |
import io
|
| 4 |
+
from PIL import Image
|
| 5 |
import os
|
| 6 |
+
import base64
|
| 7 |
|
| 8 |
API_URL = "https://api-inference.huggingface.co/models/CompVis/stable-diffusion-v1-4"
|
| 9 |
headers = {"Authorization": f"Bearer {os.getenv('HF_API_KEY')}"}
|
| 10 |
|
| 11 |
def generate_image(prompt):
|
| 12 |
+
payload = {"inputs": prompt}
|
| 13 |
+
response = requests.post(API_URL, headers=headers, json=payload)
|
| 14 |
+
|
| 15 |
+
# If error or waiting for model to load
|
| 16 |
+
if response.status_code != 200:
|
| 17 |
+
try:
|
| 18 |
+
return f"Error: {response.json()}"
|
| 19 |
+
except:
|
| 20 |
+
return f"Error: {response.status_code} - {response.text}"
|
| 21 |
+
|
| 22 |
+
# If it's actually an image, convert and return
|
| 23 |
+
try:
|
| 24 |
+
image = Image.open(io.BytesIO(response.content))
|
| 25 |
return image
|
| 26 |
+
except Exception as e:
|
| 27 |
+
return f"Could not load image: {str(e)}"
|
| 28 |
|
| 29 |
iface = gr.Interface(
|
| 30 |
fn=generate_image,
|
| 31 |
inputs=gr.Textbox(label="Enter your prompt"),
|
| 32 |
outputs=gr.Image(type="pil", label="Generated Image"),
|
| 33 |
title="Text-to-Image Generator",
|
| 34 |
+
theme="default"
|
| 35 |
)
|
| 36 |
|
| 37 |
iface.launch()
|
| 38 |
|
| 39 |
+
|