Spaces:
Runtime error
Runtime error
Upload app.py
Browse files
app.py
CHANGED
|
@@ -1,15 +1,14 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import requests
|
| 3 |
-
import base64
|
| 4 |
from PIL import Image
|
| 5 |
from io import BytesIO
|
| 6 |
|
| 7 |
-
API_URL = "https://
|
| 8 |
|
| 9 |
|
| 10 |
def generate(prompt, seed):
|
| 11 |
response = requests.post(
|
| 12 |
-
|
| 13 |
json={
|
| 14 |
"data": [prompt, int(seed)]
|
| 15 |
}
|
|
@@ -20,11 +19,15 @@ def generate(prompt, seed):
|
|
| 20 |
|
| 21 |
data = response.json()
|
| 22 |
|
| 23 |
-
# Gradio typically returns base64 or a file path
|
| 24 |
result = data["data"][0]
|
| 25 |
|
| 26 |
-
|
| 27 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
|
| 29 |
return image
|
| 30 |
|
|
@@ -40,7 +43,6 @@ with gr.Blocks() as demo:
|
|
| 40 |
seed = gr.Number(value=123, label="Seed")
|
| 41 |
|
| 42 |
btn = gr.Button("Generate")
|
| 43 |
-
|
| 44 |
output = gr.Image()
|
| 45 |
|
| 46 |
btn.click(generate, inputs=[prompt, seed], outputs=output)
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import requests
|
|
|
|
| 3 |
from PIL import Image
|
| 4 |
from io import BytesIO
|
| 5 |
|
| 6 |
+
API_URL = "https://YOUR-BACKEND-SPACE.hf.space/run/predict"
|
| 7 |
|
| 8 |
|
| 9 |
def generate(prompt, seed):
|
| 10 |
response = requests.post(
|
| 11 |
+
API_URL,
|
| 12 |
json={
|
| 13 |
"data": [prompt, int(seed)]
|
| 14 |
}
|
|
|
|
| 19 |
|
| 20 |
data = response.json()
|
| 21 |
|
|
|
|
| 22 |
result = data["data"][0]
|
| 23 |
|
| 24 |
+
# If backend returns a PIL image serialized as URL/path
|
| 25 |
+
if isinstance(result, str) and result.startswith("http"):
|
| 26 |
+
img_bytes = requests.get(result).content
|
| 27 |
+
image = Image.open(BytesIO(img_bytes))
|
| 28 |
+
else:
|
| 29 |
+
# fallback (Gradio usually handles this automatically)
|
| 30 |
+
return result
|
| 31 |
|
| 32 |
return image
|
| 33 |
|
|
|
|
| 43 |
seed = gr.Number(value=123, label="Seed")
|
| 44 |
|
| 45 |
btn = gr.Button("Generate")
|
|
|
|
| 46 |
output = gr.Image()
|
| 47 |
|
| 48 |
btn.click(generate, inputs=[prompt, seed], outputs=output)
|