Upload app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,106 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from PIL import Image
|
| 3 |
+
from transformers import BlipProcessor, BlipForQuestionAnswering
|
| 4 |
+
|
| 5 |
+
# 預先載入可選模型(避免每次都下載)
|
| 6 |
+
AVAILABLE_MODELS = {
|
| 7 |
+
"BLIP VQA Base": "Salesforce/blip-vqa-base",
|
| 8 |
+
"BLIP VQA Large (CapFilt)": "Salesforce/blip-vqa-capfilt-large",
|
| 9 |
+
}
|
| 10 |
+
|
| 11 |
+
# 預設模型
|
| 12 |
+
current_model_name = list(AVAILABLE_MODELS.keys())[0]
|
| 13 |
+
processor = BlipProcessor.from_pretrained(AVAILABLE_MODELS[current_model_name])
|
| 14 |
+
model = BlipForQuestionAnswering.from_pretrained(AVAILABLE_MODELS[current_model_name])
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
# 模型切換功能
|
| 18 |
+
def change_model(model_choice):
|
| 19 |
+
global processor, model, current_model_name
|
| 20 |
+
current_model_name = model_choice
|
| 21 |
+
model_id = AVAILABLE_MODELS[model_choice]
|
| 22 |
+
processor = BlipProcessor.from_pretrained(model_id)
|
| 23 |
+
model = BlipForQuestionAnswering.from_pretrained(model_id)
|
| 24 |
+
return f"✅ Switched to: {model_choice}"
|
| 25 |
+
|
| 26 |
+
|
| 27 |
+
# 問答邏輯
|
| 28 |
+
def answer_question(history, image, question):
|
| 29 |
+
if image is None:
|
| 30 |
+
return history + [("Please upload an image first.", None)]
|
| 31 |
+
if not question.strip():
|
| 32 |
+
return history + [("Please enter a question.", None)]
|
| 33 |
+
|
| 34 |
+
inputs = processor(image, question, return_tensors="pt")
|
| 35 |
+
out = model.generate(**inputs, max_new_tokens=50)
|
| 36 |
+
answer = processor.decode(out[0], skip_special_tokens=True)
|
| 37 |
+
reply = f"🤖({current_model_name}) Answer: {answer}"
|
| 38 |
+
return history + [(question, reply)]
|
| 39 |
+
|
| 40 |
+
|
| 41 |
+
# 上傳新圖片時重設聊天
|
| 42 |
+
def reset_chat(_):
|
| 43 |
+
return []
|
| 44 |
+
|
| 45 |
+
|
| 46 |
+
# 建立 Gradio 介面
|
| 47 |
+
def build_ui():
|
| 48 |
+
with gr.Blocks(title="Vision-Language Chatbot") as demo:
|
| 49 |
+
gr.Markdown("## 🤖 Vision-Language Chatbot")
|
| 50 |
+
gr.Markdown("Upload an image and ask multiple questions about it!")
|
| 51 |
+
|
| 52 |
+
# 模型選擇
|
| 53 |
+
model_selector = gr.Dropdown(
|
| 54 |
+
choices=list(AVAILABLE_MODELS.keys()),
|
| 55 |
+
value=current_model_name,
|
| 56 |
+
label="Select Model",
|
| 57 |
+
)
|
| 58 |
+
model_status = gr.Markdown(f"✅ Current model: {current_model_name}")
|
| 59 |
+
|
| 60 |
+
# 版面配置
|
| 61 |
+
with gr.Row():
|
| 62 |
+
with gr.Column(scale=1):
|
| 63 |
+
image_input = gr.Image(type="pil", label="Upload Image")
|
| 64 |
+
|
| 65 |
+
with gr.Column(scale=2):
|
| 66 |
+
question_input = gr.Textbox(
|
| 67 |
+
placeholder="Ask something about the image...",
|
| 68 |
+
label="Question",
|
| 69 |
+
)
|
| 70 |
+
ask_btn = gr.Button("Ask", variant="primary")
|
| 71 |
+
clear_btn = gr.Button("Clear Chat")
|
| 72 |
+
|
| 73 |
+
chatbot = gr.Chatbot(height=400, label="Chat History")
|
| 74 |
+
|
| 75 |
+
# 邏輯綁定
|
| 76 |
+
ask_btn.click(
|
| 77 |
+
fn=answer_question,
|
| 78 |
+
inputs=[chatbot, image_input, question_input],
|
| 79 |
+
outputs=chatbot,
|
| 80 |
+
)
|
| 81 |
+
|
| 82 |
+
clear_btn.click(fn=lambda: [], outputs=chatbot)
|
| 83 |
+
image_input.change(fn=reset_chat, inputs=image_input, outputs=chatbot)
|
| 84 |
+
|
| 85 |
+
model_selector.change(
|
| 86 |
+
fn=change_model,
|
| 87 |
+
inputs=model_selector,
|
| 88 |
+
outputs=model_status,
|
| 89 |
+
)
|
| 90 |
+
|
| 91 |
+
# 範例
|
| 92 |
+
gr.Examples(
|
| 93 |
+
examples=[
|
| 94 |
+
["sample_images/app.jpg", "How many apples are in the picture?"],
|
| 95 |
+
["sample_images/cat_dog.jpg", "What animals are in the image?"],
|
| 96 |
+
["sample_images/city.jpg", "What is the man doing?"]
|
| 97 |
+
],
|
| 98 |
+
inputs=[image_input, question_input],
|
| 99 |
+
label="🏞️ Example Inputs",
|
| 100 |
+
)
|
| 101 |
+
|
| 102 |
+
return demo
|
| 103 |
+
|
| 104 |
+
|
| 105 |
+
if __name__ == "__main__":
|
| 106 |
+
build_ui().launch()
|