Update app.py
Browse files
app.py
CHANGED
|
@@ -1,12 +1,94 @@
|
|
| 1 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
|
| 3 |
-
|
| 4 |
-
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import torch
|
| 3 |
+
from threading import Thread
|
| 4 |
+
from transformers import (
|
| 5 |
+
AutoProcessor,
|
| 6 |
+
AutoModelForImageTextToText,
|
| 7 |
+
TextIteratorStreamer,
|
| 8 |
+
)
|
| 9 |
|
| 10 |
+
# ======================
|
| 11 |
+
# INIT
|
| 12 |
+
# ======================
|
| 13 |
+
DEVICE = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 14 |
+
MODEL_ID = "HuggingFaceTB/SmolVLM2-2.2B-Instruct"
|
| 15 |
|
| 16 |
+
processor = AutoProcessor.from_pretrained(MODEL_ID)
|
| 17 |
+
model = AutoModelForImageTextToText.from_pretrained(
|
| 18 |
+
MODEL_ID,
|
| 19 |
+
torch_dtype=torch.bfloat16 if torch.cuda.is_available() else torch.float32,
|
| 20 |
+
).to(DEVICE).eval()
|
| 21 |
+
|
| 22 |
+
|
| 23 |
+
# ======================
|
| 24 |
+
# STREAMING INFERENCE (SAFE)
|
| 25 |
+
# ======================
|
| 26 |
+
def analyze_stream(text, image, max_tokens):
|
| 27 |
+
content = []
|
| 28 |
+
if image is not None:
|
| 29 |
+
content.append({"type": "image", "path": image})
|
| 30 |
+
if text.strip():
|
| 31 |
+
content.append({"type": "text", "text": text})
|
| 32 |
+
|
| 33 |
+
messages = [{"role": "user", "content": content}]
|
| 34 |
+
|
| 35 |
+
inputs = processor.apply_chat_template(
|
| 36 |
+
messages,
|
| 37 |
+
add_generation_prompt=True,
|
| 38 |
+
tokenize=True,
|
| 39 |
+
return_tensors="pt",
|
| 40 |
+
).to(DEVICE)
|
| 41 |
+
|
| 42 |
+
streamer = TextIteratorStreamer(
|
| 43 |
+
processor,
|
| 44 |
+
skip_prompt=True,
|
| 45 |
+
skip_special_tokens=True,
|
| 46 |
+
)
|
| 47 |
+
|
| 48 |
+
thread = Thread(
|
| 49 |
+
target=model.generate,
|
| 50 |
+
kwargs=dict(
|
| 51 |
+
**inputs,
|
| 52 |
+
streamer=streamer,
|
| 53 |
+
max_new_tokens=max_tokens,
|
| 54 |
+
do_sample=False,
|
| 55 |
+
temperature=0.0,
|
| 56 |
+
),
|
| 57 |
+
)
|
| 58 |
+
thread.start()
|
| 59 |
+
|
| 60 |
+
partial = ""
|
| 61 |
+
for token in streamer:
|
| 62 |
+
partial += token
|
| 63 |
+
yield partial
|
| 64 |
+
|
| 65 |
+
|
| 66 |
+
# ======================
|
| 67 |
+
# UI STABLE
|
| 68 |
+
# ======================
|
| 69 |
+
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 70 |
+
gr.Markdown("## ⚡ SmolVLM2 – Real-time Analysis")
|
| 71 |
+
|
| 72 |
+
with gr.Row():
|
| 73 |
+
with gr.Column():
|
| 74 |
+
txt = gr.Textbox(
|
| 75 |
+
label="Question",
|
| 76 |
+
lines=3,
|
| 77 |
+
)
|
| 78 |
+
img = gr.Image(type="filepath", label="Image")
|
| 79 |
+
max_tokens = gr.Slider(50, 400, value=200, step=50)
|
| 80 |
+
btn = gr.Button("🚀 Analyze", variant="primary")
|
| 81 |
+
|
| 82 |
+
with gr.Column():
|
| 83 |
+
out = gr.Textbox(
|
| 84 |
+
label="Streaming Output",
|
| 85 |
+
lines=14,
|
| 86 |
+
)
|
| 87 |
+
|
| 88 |
+
btn.click(
|
| 89 |
+
fn=analyze_stream,
|
| 90 |
+
inputs=[txt, img, max_tokens],
|
| 91 |
+
outputs=out,
|
| 92 |
+
)
|
| 93 |
|
| 94 |
demo.launch()
|