stephenebert commited on
Commit
6aba4b8
·
verified ·
1 Parent(s): f451444

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -27
app.py CHANGED
@@ -1,6 +1,6 @@
1
  from __future__ import annotations
2
 
3
- # FastAPI REST API + a Gradio UI mounted at /ui
4
  # Endpoints:
5
  # GET /healthz
6
  # POST /upload -> {filename, caption, tags}
@@ -18,28 +18,24 @@ import io
18
  import gradio as gr
19
  from tagger import tag_pil_image # returns (caption: str, tags: List[str])
20
 
21
- app = FastAPI(title="Image Tagger API", version="0.4.1")
22
 
23
-
24
- # ---------- Pydantic model for OpenAPI ----------
25
  class TagOut(BaseModel):
26
  filename: str
27
  caption: str
28
  tags: List[str]
29
 
30
-
31
  # ---------- Basic routes ----------
32
  @app.get("/", include_in_schema=False)
33
  def root_redirect():
34
  # Send users to the nicer UI
35
  return RedirectResponse(url="/ui", status_code=302)
36
 
37
-
38
  @app.get("/healthz")
39
  def healthz():
40
  return {"ok": True}
41
 
42
-
43
  # ---------- REST endpoint ----------
44
  @app.post("/upload", response_model=TagOut)
45
  async def upload(
@@ -69,31 +65,28 @@ async def upload(
69
 
70
  return JSONResponse({"filename": file.filename, "caption": caption, "tags": tags})
71
 
72
-
73
- # ---------- Gradio UI (mounted at /ui) ----------
74
  def _ui_tag(image: Image.Image, top_k: int):
75
  if image is None:
76
  return "", ""
77
  caption, tags = tag_pil_image(image.convert("RGB"), "upload", top_k=top_k)
78
  return caption, ", ".join(tags)
79
 
80
-
81
- with gr.Blocks(title="Image Tagger", analytics_enabled=False) as gr_app:
82
- gr.Markdown(
83
- "## 🏷️ Image Tagger\n"
84
- "Upload an image to get a caption and top-k tags. "
85
- "Programmatic API is available at **/docs**."
86
- )
87
- with gr.Row():
88
- with gr.Column(scale=1):
89
- inp = gr.Image(type="pil", label="Upload image", sources=["upload"], height=360)
90
- k = gr.Slider(1, 20, value=5, step=1, label="Top-k tags")
91
- btn = gr.Button("Tag it")
92
- with gr.Column(scale=1):
93
- cap = gr.Textbox(label="Caption", lines=2)
94
- tags_box = gr.Textbox(label="Tags (comma-separated)", lines=2)
95
-
96
- btn.click(_ui_tag, inputs=[inp, k], outputs=[cap, tags_box])
97
 
98
  # Mount Gradio on the same FastAPI app
99
- app = gr.mount_gradio_app(app, gr_app, path="/ui")
 
 
1
  from __future__ import annotations
2
 
3
+ # FastAPI REST API + Gradio Interface mounted at /ui
4
  # Endpoints:
5
  # GET /healthz
6
  # POST /upload -> {filename, caption, tags}
 
18
  import gradio as gr
19
  from tagger import tag_pil_image # returns (caption: str, tags: List[str])
20
 
21
+ app = FastAPI(title="Image Tagger API", version="0.4.2")
22
 
23
+ # ---------- Pydantic model ----------
 
24
  class TagOut(BaseModel):
25
  filename: str
26
  caption: str
27
  tags: List[str]
28
 
 
29
  # ---------- Basic routes ----------
30
  @app.get("/", include_in_schema=False)
31
  def root_redirect():
32
  # Send users to the nicer UI
33
  return RedirectResponse(url="/ui", status_code=302)
34
 
 
35
  @app.get("/healthz")
36
  def healthz():
37
  return {"ok": True}
38
 
 
39
  # ---------- REST endpoint ----------
40
  @app.post("/upload", response_model=TagOut)
41
  async def upload(
 
65
 
66
  return JSONResponse({"filename": file.filename, "caption": caption, "tags": tags})
67
 
68
+ # ---------- Gradio Interface (mounted at /ui) ----------
 
69
  def _ui_tag(image: Image.Image, top_k: int):
70
  if image is None:
71
  return "", ""
72
  caption, tags = tag_pil_image(image.convert("RGB"), "upload", top_k=top_k)
73
  return caption, ", ".join(tags)
74
 
75
+ demo = gr.Interface(
76
+ fn=_ui_tag,
77
+ inputs=[
78
+ gr.Image(type="pil", label="Upload image"),
79
+ gr.Slider(1, 20, value=5, step=1, label="Top-k tags"),
80
+ ],
81
+ outputs=[
82
+ gr.Textbox(label="Caption", lines=2),
83
+ gr.Textbox(label="Tags (comma-separated)", lines=2),
84
+ ],
85
+ allow_flagging="never",
86
+ title="Image Tagger",
87
+ description="Upload an image to get a caption and top-k tags. Programmatic API at /docs.",
88
+ )
 
 
 
89
 
90
  # Mount Gradio on the same FastAPI app
91
+ app = gr.mount_gradio_app(app, demo, path="/ui")
92
+