Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# app.py
|
| 2 |
+
import gradio as gr
|
| 3 |
+
import torch
|
| 4 |
+
from transformers import AutoProcessor, AutoModelForVision2Seq
|
| 5 |
+
|
| 6 |
+
# -----------------------------------------------------------------------------
|
| 7 |
+
# Model initialization
|
| 8 |
+
# -----------------------------------------------------------------------------
|
| 9 |
+
MODEL_ID = "RedHatAI/Qwen2.5-VL-3B-Instruct-quantized.w8a8"
|
| 10 |
+
|
| 11 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 12 |
+
dtype = torch.float16 if device == "cuda" else torch.float32
|
| 13 |
+
|
| 14 |
+
print(f"🚀 Loading model on {device} ...")
|
| 15 |
+
processor = AutoProcessor.from_pretrained(MODEL_ID)
|
| 16 |
+
model = AutoModelForVision2Seq.from_pretrained(
|
| 17 |
+
MODEL_ID,
|
| 18 |
+
torch_dtype=dtype,
|
| 19 |
+
device_map="auto" if device == "cuda" else None
|
| 20 |
+
)
|
| 21 |
+
model.eval()
|
| 22 |
+
print("✅ Model ready!")
|
| 23 |
+
|
| 24 |
+
# -----------------------------------------------------------------------------
|
| 25 |
+
# Inference function
|
| 26 |
+
# -----------------------------------------------------------------------------
|
| 27 |
+
def ask_about_image(image, prompt):
|
| 28 |
+
"""
|
| 29 |
+
Take an uploaded image and user question, run vision-language inference,
|
| 30 |
+
and return model's generated text.
|
| 31 |
+
"""
|
| 32 |
+
if image is None or prompt.strip() == "":
|
| 33 |
+
return "Please upload an image and enter a question."
|
| 34 |
+
|
| 35 |
+
# Prepare input
|
| 36 |
+
inputs = processor(images=image, text=prompt, return_tensors="pt").to(device, dtype)
|
| 37 |
+
|
| 38 |
+
# Generate answer
|
| 39 |
+
with torch.no_grad():
|
| 40 |
+
generated_ids = model.generate(**inputs, max_new_tokens=256)
|
| 41 |
+
answer = processor.batch_decode(generated_ids, skip_special_tokens=True)[0]
|
| 42 |
+
return answer.strip()
|
| 43 |
+
|
| 44 |
+
# -----------------------------------------------------------------------------
|
| 45 |
+
# Gradio UI
|
| 46 |
+
# -----------------------------------------------------------------------------
|
| 47 |
+
with gr.Blocks(title="🧠 Qwen2.5-VL Image Q&A") as demo:
|
| 48 |
+
gr.Markdown("## 🧩 Qwen 2.5 VL – 3B Instruct (Quantized)\nAsk questions about any uploaded image.")
|
| 49 |
+
with gr.Row():
|
| 50 |
+
img = gr.Image(type="pil", label="Upload Image")
|
| 51 |
+
with gr.Column():
|
| 52 |
+
prompt = gr.Textbox(label="Enter your question", placeholder="e.g. What’s written on the sign?")
|
| 53 |
+
ask_btn = gr.Button("Ask")
|
| 54 |
+
output = gr.Textbox(label="Model Response", lines=5)
|
| 55 |
+
|
| 56 |
+
ask_btn.click(ask_about_image, [img, prompt], output)
|
| 57 |
+
prompt.submit(ask_about_image, [img, prompt], output)
|
| 58 |
+
|
| 59 |
+
demo.launch()
|