Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,4 +1,4 @@
|
|
1 |
-
# app.py (Phiên bản cuối cùng đã sửa lỗi và cảnh báo)
|
2 |
|
3 |
import gradio as gr
|
4 |
import torch
|
@@ -7,17 +7,16 @@ from transformers import AutoModelForImageTextToText, AutoProcessor
|
|
7 |
from gradio.events import SelectData
|
8 |
import warnings
|
9 |
import os
|
10 |
-
|
11 |
|
12 |
warnings.filterwarnings("ignore", category=UserWarning, message="Overriding torch_dtype=None")
|
13 |
|
14 |
# --- 1. Tải Model và Processor ---
|
15 |
-
MODEL_ID = "sunbv56/qwen2.5-vl-vqa-vibook"
|
16 |
print(f"🚀 Đang tải model '{MODEL_ID}' và processor...")
|
17 |
try:
|
18 |
dtype = torch.bfloat16 if torch.cuda.is_available() and torch.cuda.is_bf16_supported() else torch.float16
|
19 |
model = AutoModelForImageTextToText.from_pretrained(MODEL_ID, torch_dtype=dtype, device_map="auto", trust_remote_code=True)
|
20 |
-
# SỬA LỖI 3: Thêm use_fast=True để tắt cảnh báo
|
21 |
processor = AutoProcessor.from_pretrained(MODEL_ID, trust_remote_code=True, use_fast=True)
|
22 |
model.eval()
|
23 |
print(f"✅ Model và processor đã được tải thành công!")
|
@@ -32,19 +31,12 @@ def process_vqa(image: Image.Image, question: str):
|
|
32 |
messages = [{"role": "user", "content": [{"type": "image"}, {"type": "text", "text": question}]}]
|
33 |
prompt_text = processor.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
|
34 |
model_inputs = processor(text=[prompt_text], images=[image], return_tensors="pt").to(model.device)
|
35 |
-
generated_ids = model.generate(
|
36 |
-
**model_inputs,
|
37 |
-
max_new_tokens=1024,
|
38 |
-
do_sample=False,
|
39 |
-
eos_token_id=processor.tokenizer.eos_token_id,
|
40 |
-
pad_token_id=processor.tokenizer.pad_token_id
|
41 |
-
)
|
42 |
generated_ids = generated_ids[:, model_inputs['input_ids'].shape[1]:]
|
43 |
response = processor.tokenizer.decode(generated_ids[0], skip_special_tokens=True).strip()
|
44 |
return response
|
45 |
|
46 |
# --- 3. Logic Chatbot ---
|
47 |
-
# Hàm dành cho việc người dùng tự nhập câu hỏi
|
48 |
def manual_chat_responder(user_question: str, chat_history: list, uploaded_image: Image.Image):
|
49 |
if uploaded_image is None:
|
50 |
gr.Warning("Vui lòng tải ảnh lên trước để đặt câu hỏi về nó.")
|
@@ -52,30 +44,12 @@ def manual_chat_responder(user_question: str, chat_history: list, uploaded_image
|
|
52 |
if not user_question or not user_question.strip():
|
53 |
gr.Warning("Vui lòng nhập một câu hỏi.")
|
54 |
return "", chat_history
|
55 |
-
|
56 |
-
# SỬA LỖI 2: Sử dụng định dạng `messages` mới
|
57 |
chat_history.append({"role": "user", "content": user_question})
|
58 |
yield "", chat_history
|
59 |
-
|
60 |
bot_response = process_vqa(uploaded_image, user_question)
|
61 |
chat_history.append({"role": "assistant", "content": bot_response})
|
62 |
yield "", chat_history
|
63 |
|
64 |
-
# Hàm dành riêng cho việc xử lý khi nhấn vào ví dụ
|
65 |
-
def run_example(example_list: list, evt: SelectData):
|
66 |
-
selected_example = example_list[evt.index]
|
67 |
-
image_path, question = selected_example
|
68 |
-
gr.Info(f"Đang chạy ví dụ: \"{question}\"")
|
69 |
-
image = Image.open(image_path).convert("RGB")
|
70 |
-
|
71 |
-
# SỬA LỖI 2: Bắt đầu cuộc trò chuyện với định dạng `messages` mới
|
72 |
-
chat_history = [{"role": "user", "content": question}]
|
73 |
-
|
74 |
-
bot_response = process_vqa(image, question)
|
75 |
-
chat_history.append({"role": "assistant", "content": bot_response})
|
76 |
-
|
77 |
-
return image, question, chat_history
|
78 |
-
|
79 |
def clear_chat():
|
80 |
return []
|
81 |
|
@@ -89,33 +63,42 @@ with gr.Blocks(theme=gr.themes.Soft(primary_hue="blue", secondary_hue="sky"), ti
|
|
89 |
["./assets/book_example_2.jpg", "tác giả và tên của cuốn sách là gì?"],
|
90 |
]
|
91 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
92 |
with gr.Row(equal_height=False):
|
93 |
with gr.Column(scale=1, min_width=350):
|
94 |
gr.Markdown("### Bảng điều khiển")
|
95 |
image_input = gr.Image(type="pil", label="Tải ảnh lên", sources=["upload", "clipboard"])
|
96 |
gr.Markdown("---")
|
97 |
gr.Markdown("### Ví dụ (Nhấn để chạy)")
|
98 |
-
example_dataset = gr.Dataset(
|
99 |
-
components=[gr.Image(visible=False), gr.Textbox(visible=False)],
|
100 |
-
samples=example_list,
|
101 |
-
label="Ví dụ",
|
102 |
-
type="index"
|
103 |
-
)
|
104 |
with gr.Column(scale=2):
|
105 |
-
#
|
106 |
-
chatbot = gr.Chatbot(
|
107 |
-
label="Cuộc trò chuyện",
|
108 |
-
bubble_full_width=False,
|
109 |
-
height=600,
|
110 |
-
avatar_images=(None, "https://huggingface.co/datasets/huggingface/brand-assets/resolve/main/hf-logo.png"),
|
111 |
-
type="messages",
|
112 |
-
value=[]
|
113 |
-
)
|
114 |
question_input = gr.Textbox(label="Hoặc nhập câu hỏi về ảnh đã tải lên", placeholder="Nhập câu hỏi và nhấn Enter...", container=False, scale=7)
|
115 |
|
116 |
# --- 5. Xử lý Sự kiện ---
|
117 |
question_input.submit(fn=manual_chat_responder, inputs=[question_input, chatbot, image_input], outputs=[question_input, chatbot])
|
118 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
119 |
image_input.upload(fn=clear_chat, inputs=None, outputs=[chatbot])
|
120 |
image_input.clear(fn=clear_chat, inputs=None, outputs=[chatbot])
|
121 |
|
@@ -126,18 +109,23 @@ if __name__ == "__main__":
|
|
126 |
os.makedirs(ASSETS_DIR)
|
127 |
print("Đã tạo thư mục 'assets' cho các hình ảnh ví dụ.")
|
128 |
|
129 |
-
# SỬA LỖI 1: Thêm định nghĩa EXAMPLE_FILES bị thiếu
|
130 |
EXAMPLE_FILES = {
|
131 |
-
"book_example_1.jpg": "https://
|
132 |
-
"book_example_2.jpg": "https://
|
133 |
}
|
134 |
|
|
|
135 |
for filename, url in EXAMPLE_FILES.items():
|
136 |
filepath = os.path.join(ASSETS_DIR, filename)
|
137 |
if not os.path.exists(filepath):
|
138 |
print(f"Đang tải xuống hình ảnh ví dụ: {filename}...")
|
139 |
-
|
140 |
-
|
141 |
-
|
|
|
|
|
|
|
|
|
|
|
142 |
|
143 |
demo.launch(debug=True)
|
|
|
1 |
+
# app.py (Phiên bản cuối cùng đã sửa lỗi TypeError và các cảnh báo)
|
2 |
|
3 |
import gradio as gr
|
4 |
import torch
|
|
|
7 |
from gradio.events import SelectData
|
8 |
import warnings
|
9 |
import os
|
10 |
+
import requests
|
11 |
|
12 |
warnings.filterwarnings("ignore", category=UserWarning, message="Overriding torch_dtype=None")
|
13 |
|
14 |
# --- 1. Tải Model và Processor ---
|
15 |
+
MODEL_ID = "sunbv56/qwen2.5-vl-vqa-vibook-lora-merged"
|
16 |
print(f"🚀 Đang tải model '{MODEL_ID}' và processor...")
|
17 |
try:
|
18 |
dtype = torch.bfloat16 if torch.cuda.is_available() and torch.cuda.is_bf16_supported() else torch.float16
|
19 |
model = AutoModelForImageTextToText.from_pretrained(MODEL_ID, torch_dtype=dtype, device_map="auto", trust_remote_code=True)
|
|
|
20 |
processor = AutoProcessor.from_pretrained(MODEL_ID, trust_remote_code=True, use_fast=True)
|
21 |
model.eval()
|
22 |
print(f"✅ Model và processor đã được tải thành công!")
|
|
|
31 |
messages = [{"role": "user", "content": [{"type": "image"}, {"type": "text", "text": question}]}]
|
32 |
prompt_text = processor.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
|
33 |
model_inputs = processor(text=[prompt_text], images=[image], return_tensors="pt").to(model.device)
|
34 |
+
generated_ids = model.generate(**model_inputs, max_new_tokens=1024, do_sample=False, eos_token_id=processor.tokenizer.eos_token_id, pad_token_id=processor.tokenizer.pad_token_id)
|
|
|
|
|
|
|
|
|
|
|
|
|
35 |
generated_ids = generated_ids[:, model_inputs['input_ids'].shape[1]:]
|
36 |
response = processor.tokenizer.decode(generated_ids[0], skip_special_tokens=True).strip()
|
37 |
return response
|
38 |
|
39 |
# --- 3. Logic Chatbot ---
|
|
|
40 |
def manual_chat_responder(user_question: str, chat_history: list, uploaded_image: Image.Image):
|
41 |
if uploaded_image is None:
|
42 |
gr.Warning("Vui lòng tải ảnh lên trước để đặt câu hỏi về nó.")
|
|
|
44 |
if not user_question or not user_question.strip():
|
45 |
gr.Warning("Vui lòng nhập một câu hỏi.")
|
46 |
return "", chat_history
|
|
|
|
|
47 |
chat_history.append({"role": "user", "content": user_question})
|
48 |
yield "", chat_history
|
|
|
49 |
bot_response = process_vqa(uploaded_image, user_question)
|
50 |
chat_history.append({"role": "assistant", "content": bot_response})
|
51 |
yield "", chat_history
|
52 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
53 |
def clear_chat():
|
54 |
return []
|
55 |
|
|
|
63 |
["./assets/book_example_2.jpg", "tác giả và tên của cuốn sách là gì?"],
|
64 |
]
|
65 |
|
66 |
+
# SỬA LỖI: Di chuyển hàm xử lý ví dụ vào bên trong khối `with`
|
67 |
+
# để nó có thể truy cập `example_list` từ phạm vi bên ngoài.
|
68 |
+
def run_example(evt: SelectData):
|
69 |
+
# `example_list` giờ đây là biến list chính xác từ phạm vi ngoài
|
70 |
+
selected_example = example_list[evt.index]
|
71 |
+
image_path, question = selected_example
|
72 |
+
gr.Info(f"Đang chạy ví dụ: \"{question}\"")
|
73 |
+
image = Image.open(image_path).convert("RGB")
|
74 |
+
chat_history = [{"role": "user", "content": question}]
|
75 |
+
bot_response = process_vqa(image, question)
|
76 |
+
chat_history.append({"role": "assistant", "content": bot_response})
|
77 |
+
return image, question, chat_history
|
78 |
+
|
79 |
with gr.Row(equal_height=False):
|
80 |
with gr.Column(scale=1, min_width=350):
|
81 |
gr.Markdown("### Bảng điều khiển")
|
82 |
image_input = gr.Image(type="pil", label="Tải ảnh lên", sources=["upload", "clipboard"])
|
83 |
gr.Markdown("---")
|
84 |
gr.Markdown("### Ví dụ (Nhấn để chạy)")
|
85 |
+
example_dataset = gr.Dataset(components=[gr.Image(visible=False), gr.Textbox(visible=False)], samples=example_list, label="Ví dụ", type="index")
|
|
|
|
|
|
|
|
|
|
|
86 |
with gr.Column(scale=2):
|
87 |
+
# Sửa cảnh báo: Xóa `bubble_full_width` đã lỗi thời
|
88 |
+
chatbot = gr.Chatbot(label="Cuộc trò chuyện", height=600, avatar_images=(None, "https://huggingface.co/datasets/huggingface/brand-assets/resolve/main/hf-logo.png"), type="messages", value=[])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
89 |
question_input = gr.Textbox(label="Hoặc nhập câu hỏi về ảnh đã tải lên", placeholder="Nhập câu hỏi và nhấn Enter...", container=False, scale=7)
|
90 |
|
91 |
# --- 5. Xử lý Sự kiện ---
|
92 |
question_input.submit(fn=manual_chat_responder, inputs=[question_input, chatbot, image_input], outputs=[question_input, chatbot])
|
93 |
+
|
94 |
+
# SỬA LỖI: Loại bỏ `inputs` để hàm chỉ nhận `evt`
|
95 |
+
example_dataset.select(
|
96 |
+
fn=run_example,
|
97 |
+
inputs=None,
|
98 |
+
outputs=[image_input, question_input, chatbot],
|
99 |
+
show_progress="full"
|
100 |
+
)
|
101 |
+
|
102 |
image_input.upload(fn=clear_chat, inputs=None, outputs=[chatbot])
|
103 |
image_input.clear(fn=clear_chat, inputs=None, outputs=[chatbot])
|
104 |
|
|
|
109 |
os.makedirs(ASSETS_DIR)
|
110 |
print("Đã tạo thư mục 'assets' cho các hình ảnh ví dụ.")
|
111 |
|
|
|
112 |
EXAMPLE_FILES = {
|
113 |
+
"book_example_1.jpg": "https://cdn0.fahasa.com/media/catalog/product/d/i/dieu-ky-dieu-cua-tiem-tap-hoa-namiya---tai-ban-2020.jpg",
|
114 |
+
"book_example_2.jpg": "https://cdn0.fahasa.com/media/catalog/product/d/r/dr.-stone_bia_tap-26.jpg"
|
115 |
}
|
116 |
|
117 |
+
headers = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36"}
|
118 |
for filename, url in EXAMPLE_FILES.items():
|
119 |
filepath = os.path.join(ASSETS_DIR, filename)
|
120 |
if not os.path.exists(filepath):
|
121 |
print(f"Đang tải xuống hình ảnh ví dụ: {filename}...")
|
122 |
+
try:
|
123 |
+
response = requests.get(url, headers=headers, timeout=10)
|
124 |
+
response.raise_for_status()
|
125 |
+
with open(filepath, 'wb') as f:
|
126 |
+
f.write(response.content)
|
127 |
+
print("...Đã xong.")
|
128 |
+
except requests.exceptions.RequestException as e:
|
129 |
+
print(f" Lỗi khi tải {filename}: {e}")
|
130 |
|
131 |
demo.launch(debug=True)
|