Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,7 +3,7 @@ from __future__ import annotations
|
|
| 3 |
# FastAPI REST API + a Gradio UI mounted at /ui
|
| 4 |
# Endpoints:
|
| 5 |
# GET /healthz
|
| 6 |
-
# POST /upload
|
| 7 |
# UI:
|
| 8 |
# /ui (upload image, choose top_k, see caption + tags)
|
| 9 |
|
|
@@ -18,7 +18,7 @@ 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.
|
| 22 |
|
| 23 |
|
| 24 |
# ---------- Pydantic model for OpenAPI ----------
|
|
@@ -73,9 +73,9 @@ async def upload(
|
|
| 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, tags
|
| 79 |
|
| 80 |
|
| 81 |
with gr.Blocks(title="Image Tagger", analytics_enabled=False) as gr_app:
|
|
@@ -86,17 +86,14 @@ with gr.Blocks(title="Image Tagger", analytics_enabled=False) as gr_app:
|
|
| 86 |
)
|
| 87 |
with gr.Row():
|
| 88 |
with gr.Column(scale=1):
|
| 89 |
-
inp = gr.Image(
|
| 90 |
-
type="pil", label="Upload image", sources=["upload"], height=360
|
| 91 |
-
)
|
| 92 |
k = gr.Slider(1, 20, value=5, step=1, label="Top-k tags")
|
| 93 |
btn = gr.Button("Tag it")
|
| 94 |
with gr.Column(scale=1):
|
| 95 |
cap = gr.Textbox(label="Caption", lines=2)
|
| 96 |
-
|
| 97 |
|
| 98 |
-
btn.click(_ui_tag, inputs=[inp, k], outputs=[cap,
|
| 99 |
|
| 100 |
# Mount Gradio on the same FastAPI app
|
| 101 |
app = gr.mount_gradio_app(app, gr_app, path="/ui")
|
| 102 |
-
|
|
|
|
| 3 |
# FastAPI REST API + a Gradio UI mounted at /ui
|
| 4 |
# Endpoints:
|
| 5 |
# GET /healthz
|
| 6 |
+
# POST /upload -> {filename, caption, tags}
|
| 7 |
# UI:
|
| 8 |
# /ui (upload image, choose top_k, see caption + tags)
|
| 9 |
|
|
|
|
| 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 ----------
|
|
|
|
| 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:
|
|
|
|
| 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")
|
|
|