Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,23 +1,49 @@
|
|
| 1 |
import gradio as gr
|
|
|
|
|
|
|
| 2 |
|
| 3 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
model = gr.load("models/jbigs/stellathedog")
|
| 5 |
|
| 6 |
-
# Define custom examples
|
| 7 |
examples = [
|
| 8 |
["Stella the dog running in a park"],
|
| 9 |
["Stella the dog on a beach at sunset"],
|
| 10 |
]
|
| 11 |
|
| 12 |
-
#
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
|
| 17 |
-
|
| 18 |
-
gr.Interface(
|
| 19 |
fn=generate_output,
|
| 20 |
inputs=gr.Textbox(label="Input"),
|
| 21 |
outputs=gr.Image(label="Output"),
|
| 22 |
examples=examples,
|
| 23 |
-
)
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import os, shutil
|
| 3 |
+
from typing import Any
|
| 4 |
|
| 5 |
+
# ----------------------------------
|
| 6 |
+
# Clean out stale Gradio cache
|
| 7 |
+
# ----------------------------------
|
| 8 |
+
if os.path.isdir(".gradio/cached_examples"):
|
| 9 |
+
shutil.rmtree(".gradio/cached_examples")
|
| 10 |
+
|
| 11 |
+
# ----------------------------------
|
| 12 |
+
# Load the model
|
| 13 |
+
# ----------------------------------
|
| 14 |
model = gr.load("models/jbigs/stellathedog")
|
| 15 |
|
|
|
|
| 16 |
examples = [
|
| 17 |
["Stella the dog running in a park"],
|
| 18 |
["Stella the dog on a beach at sunset"],
|
| 19 |
]
|
| 20 |
|
| 21 |
+
# ----------------------------------
|
| 22 |
+
# Utility to pull an image‑like object
|
| 23 |
+
# ----------------------------------
|
| 24 |
+
def _extract_image(x: Any):
|
| 25 |
+
if isinstance(x, (tuple, list)): # first element from tuple/list
|
| 26 |
+
return _extract_image(x[0])
|
| 27 |
+
if isinstance(x, dict): # common keys or first value
|
| 28 |
+
for k in ("image", "img", "picture", "output", "result"):
|
| 29 |
+
if k in x:
|
| 30 |
+
return _extract_image(x[k])
|
| 31 |
+
return _extract_image(next(iter(x.values())))
|
| 32 |
+
return x # assume already Gradio‑compatible
|
| 33 |
+
|
| 34 |
+
# ----------------------------------
|
| 35 |
+
# Interface callback
|
| 36 |
+
# ----------------------------------
|
| 37 |
+
def generate_output(prompt: str):
|
| 38 |
+
raw = model(prompt)
|
| 39 |
+
return _extract_image(raw)
|
| 40 |
|
| 41 |
+
demo = gr.Interface(
|
|
|
|
| 42 |
fn=generate_output,
|
| 43 |
inputs=gr.Textbox(label="Input"),
|
| 44 |
outputs=gr.Image(label="Output"),
|
| 45 |
examples=examples,
|
| 46 |
+
)
|
| 47 |
+
|
| 48 |
+
if __name__ == "__main__":
|
| 49 |
+
demo.launch()
|