Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,4 +1,4 @@
|
|
| 1 |
-
# app.py –
|
| 2 |
import gradio as gr
|
| 3 |
from PIL import Image
|
| 4 |
import requests, urllib.parse, threading, time
|
|
@@ -30,7 +30,7 @@ rewriter = T5ForConditionalGeneration.from_pretrained(TXT_MODEL).to(device).eval
|
|
| 30 |
# Helpers
|
| 31 |
# -------------------------------------------------
|
| 32 |
def load_image(url: str):
|
| 33 |
-
"""Return PIL
|
| 34 |
try:
|
| 35 |
url = url.strip()
|
| 36 |
if url.startswith("data:"):
|
|
@@ -63,7 +63,6 @@ def generate_base(img: Image.Image, max_len=40, beams=2, sample=False):
|
|
| 63 |
early_stopping=True,
|
| 64 |
)
|
| 65 |
else:
|
| 66 |
-
# ensure num_return ≤ beams
|
| 67 |
out = vision.generate(
|
| 68 |
pix,
|
| 69 |
max_length=max_len,
|
|
@@ -72,8 +71,7 @@ def generate_base(img: Image.Image, max_len=40, beams=2, sample=False):
|
|
| 72 |
early_stopping=True,
|
| 73 |
)
|
| 74 |
caps = [tokenizer.decode(o, skip_special_tokens=True).strip() for o in out]
|
| 75 |
-
|
| 76 |
-
return max(caps, key=lambda s: len(s.split()))
|
| 77 |
|
| 78 |
def expand_caption(base: str, prompt: str = None, max_len=160):
|
| 79 |
"""Rich T5 expansion."""
|
|
@@ -151,15 +149,21 @@ def final_caption(url, prompt, detail, beams, sample):
|
|
| 151 |
# UI
|
| 152 |
# -------------------------------------------------
|
| 153 |
css = "footer {display:none !important;}"
|
| 154 |
-
with gr.Blocks(
|
| 155 |
-
gr.Markdown(
|
|
|
|
|
|
|
| 156 |
with gr.Row():
|
| 157 |
with gr.Column():
|
| 158 |
url_in = gr.Textbox(label="Image URL / data‑URL")
|
| 159 |
prompt_in = gr.Textbox(label="Optional prompt")
|
| 160 |
-
detail_in = gr.Radio(
|
| 161 |
-
|
| 162 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 163 |
go_btn = gr.Button("Load & Describe (fast)")
|
| 164 |
final_btn = gr.Button("Get final caption (detailed)")
|
| 165 |
status_out = gr.Textbox(label="Status", interactive=False)
|
|
@@ -179,5 +183,15 @@ with gr.Blocks(css=css, title="Image Describer (CPU)") as demo:
|
|
| 179 |
outputs=[caption_out, status_out],
|
| 180 |
)
|
| 181 |
|
|
|
|
|
|
|
|
|
|
| 182 |
if __name__ == "__main__":
|
| 183 |
-
demo.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# app.py – corrected for Gradio 6+
|
| 2 |
import gradio as gr
|
| 3 |
from PIL import Image
|
| 4 |
import requests, urllib.parse, threading, time
|
|
|
|
| 30 |
# Helpers
|
| 31 |
# -------------------------------------------------
|
| 32 |
def load_image(url: str):
|
| 33 |
+
"""Return (PIL.Image, None) or (None, error). Handles http/https and data‑URL."""
|
| 34 |
try:
|
| 35 |
url = url.strip()
|
| 36 |
if url.startswith("data:"):
|
|
|
|
| 63 |
early_stopping=True,
|
| 64 |
)
|
| 65 |
else:
|
|
|
|
| 66 |
out = vision.generate(
|
| 67 |
pix,
|
| 68 |
max_length=max_len,
|
|
|
|
| 71 |
early_stopping=True,
|
| 72 |
)
|
| 73 |
caps = [tokenizer.decode(o, skip_special_tokens=True).strip() for o in out]
|
| 74 |
+
return max(caps, key=lambda s: len(s.split())) # longest = most detailed
|
|
|
|
| 75 |
|
| 76 |
def expand_caption(base: str, prompt: str = None, max_len=160):
|
| 77 |
"""Rich T5 expansion."""
|
|
|
|
| 149 |
# UI
|
| 150 |
# -------------------------------------------------
|
| 151 |
css = "footer {display:none !important;}"
|
| 152 |
+
with gr.Blocks() as demo: # no css here
|
| 153 |
+
gr.Markdown(
|
| 154 |
+
"## Image Describer"
|
| 155 |
+
)
|
| 156 |
with gr.Row():
|
| 157 |
with gr.Column():
|
| 158 |
url_in = gr.Textbox(label="Image URL / data‑URL")
|
| 159 |
prompt_in = gr.Textbox(label="Optional prompt")
|
| 160 |
+
detail_in = gr.Radio(
|
| 161 |
+
["Low", "Medium", "High"], value="Medium", label="Detail level"
|
| 162 |
+
)
|
| 163 |
+
beams_in = gr.Slider(1, 4, step=1, value=2, label="Beams")
|
| 164 |
+
sample_in = gr.Checkbox(
|
| 165 |
+
label="Enable sampling (more diverse)", value=False
|
| 166 |
+
)
|
| 167 |
go_btn = gr.Button("Load & Describe (fast)")
|
| 168 |
final_btn = gr.Button("Get final caption (detailed)")
|
| 169 |
status_out = gr.Textbox(label="Status", interactive=False)
|
|
|
|
| 183 |
outputs=[caption_out, status_out],
|
| 184 |
)
|
| 185 |
|
| 186 |
+
# -------------------------------------------------
|
| 187 |
+
# Launch – css and title are passed here (Gradio 6+)
|
| 188 |
+
# -------------------------------------------------
|
| 189 |
if __name__ == "__main__":
|
| 190 |
+
demo.queue() # enables background threads without leaking the event loop
|
| 191 |
+
demo.launch(
|
| 192 |
+
server_name="0.0.0.0",
|
| 193 |
+
server_port=7860,
|
| 194 |
+
css=css,
|
| 195 |
+
title="Image Describer (CPU)",
|
| 196 |
+
prevent_thread_lock=True, # clean shutdown → no “invalid file descriptor” warnings
|
| 197 |
+
)
|