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