Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,133 +1,124 @@
|
|
1 |
-
#
|
2 |
|
3 |
import gradio as gr
|
4 |
import torch
|
5 |
from PIL import Image
|
6 |
-
from transformers import AutoModelForImageTextToText, AutoProcessor
|
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"
|
16 |
-
print(f"🚀 Đang tải model '{MODEL_ID}' và processor
|
17 |
-
|
18 |
-
#
|
19 |
-
# Sử dụng
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
)
|
25 |
-
|
26 |
try:
|
27 |
-
# ### THAY ĐỔI TỐI ƯU 2: Tải model với Quantization và Flash Attention 2 ###
|
28 |
model = AutoModelForImageTextToText.from_pretrained(
|
29 |
-
MODEL_ID,
|
30 |
-
device_map="auto",
|
31 |
trust_remote_code=True,
|
32 |
-
|
33 |
-
|
|
|
|
|
34 |
)
|
35 |
processor = AutoProcessor.from_pretrained(MODEL_ID, trust_remote_code=True, use_fast=True)
|
36 |
|
37 |
-
#
|
38 |
-
#
|
39 |
-
#
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
print("✅ Biên dịch model thành công!")
|
44 |
-
except Exception as e:
|
45 |
-
print(f"⚠️ Không thể biên dịch model: {e}. Chạy ở chế độ thông thường.")
|
46 |
|
47 |
model.eval()
|
48 |
-
print(f"✅ Model và processor đã được tải và tối ưu thành công!")
|
49 |
-
|
50 |
except Exception as e:
|
51 |
-
print(f"❌ Lỗi khi tải model/processor: {e}")
|
52 |
exit()
|
53 |
|
54 |
-
# --- 2. Hàm Inference Cốt lõi
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
73 |
|
|
|
74 |
generated_ids = generated_ids[:, model_inputs['input_ids'].shape[1]:]
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
justify-content: flex-start;
|
96 |
-
padding: 8px 0;
|
97 |
-
}
|
98 |
-
.typing-indicator span {
|
99 |
-
height: 10px;
|
100 |
-
width: 10px;
|
101 |
-
margin: 0 2px;
|
102 |
-
background-color: #9E9E9E;
|
103 |
-
border-radius: 50%;
|
104 |
-
animation: blink 1.4s infinite both;
|
105 |
-
}
|
106 |
-
.typing-indicator span:nth-child(2) {
|
107 |
-
animation-delay: .2s;
|
108 |
-
}
|
109 |
-
.typing-indicator span:nth-child(3) {
|
110 |
-
animation-delay: .4s;
|
111 |
-
}
|
112 |
-
"""
|
113 |
-
|
114 |
-
def manual_chat_responder(user_question: str, chat_history: list, uploaded_image: Image.Image):
|
115 |
if uploaded_image is None:
|
116 |
gr.Warning("Vui lòng tải ảnh lên trước để đặt câu hỏi về nó.")
|
117 |
-
return "", chat_history
|
118 |
if not user_question or not user_question.strip():
|
119 |
gr.Warning("Vui lòng nhập một câu hỏi.")
|
120 |
-
return "", chat_history
|
121 |
-
|
122 |
chat_history.append({"role": "user", "content": user_question})
|
123 |
chat_history.append({"role": "assistant", "content": THINKING_HTML})
|
124 |
-
|
125 |
-
|
126 |
-
|
|
|
|
|
|
|
|
|
127 |
|
128 |
chat_history[-1]["content"] = bot_response
|
129 |
-
|
|
|
|
|
130 |
|
|
|
|
|
131 |
def run_example(evt: SelectData):
|
132 |
selected_example = example_list[evt.index]
|
133 |
image_path, question = selected_example
|
@@ -140,7 +131,7 @@ def run_example(evt: SelectData):
|
|
140 |
]
|
141 |
yield image, question, chat_history
|
142 |
|
143 |
-
bot_response =
|
144 |
|
145 |
chat_history[-1]["content"] = bot_response
|
146 |
yield image, question, chat_history
|
@@ -148,9 +139,9 @@ def run_example(evt: SelectData):
|
|
148 |
def clear_chat():
|
149 |
return []
|
150 |
|
151 |
-
# --- 4. Giao diện Người dùng Gradio
|
152 |
with gr.Blocks(theme=gr.themes.Soft(primary_hue="blue", secondary_hue="sky"), title="Vibook VQA Chatbot", css=CUSTOM_CSS) as demo:
|
153 |
-
gr.Markdown("# 🤖 Vibook VQA Chatbot")
|
154 |
|
155 |
example_list = [
|
156 |
["./assets/book_example_1.jpg", "Đâu là tên đúng của cuốn sách này?"],
|
@@ -169,13 +160,20 @@ with gr.Blocks(theme=gr.themes.Soft(primary_hue="blue", secondary_hue="sky"), ti
|
|
169 |
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=[])
|
170 |
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)
|
171 |
|
172 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
173 |
example_dataset.select(fn=run_example, inputs=None, outputs=[image_input, question_input, chatbot], show_progress="full")
|
|
|
174 |
image_input.upload(fn=clear_chat, inputs=None, outputs=[chatbot])
|
175 |
image_input.clear(fn=clear_chat, inputs=None, outputs=[chatbot])
|
176 |
|
177 |
-
# --- Phần cuối
|
178 |
-
|
179 |
ASSETS_DIR = "assets"
|
180 |
if not os.path.exists(ASSETS_DIR):
|
181 |
os.makedirs(ASSETS_DIR)
|
@@ -200,4 +198,6 @@ if __name__ == "__main__":
|
|
200 |
except requests.exceptions.RequestException as e:
|
201 |
print(f" Lỗi khi tải {filename}: {e}")
|
202 |
|
|
|
|
|
203 |
demo.launch(debug=True)
|
|
|
1 |
+
# app_optimized.py
|
2 |
|
3 |
import gradio as gr
|
4 |
import torch
|
5 |
from PIL import Image
|
6 |
+
from transformers import AutoModelForImageTextToText, AutoProcessor
|
7 |
from gradio.events import SelectData
|
8 |
import warnings
|
9 |
import os
|
10 |
import requests
|
11 |
+
from typing import List
|
12 |
|
13 |
warnings.filterwarnings("ignore", category=UserWarning, message="Overriding torch_dtype=None")
|
14 |
|
15 |
+
# --- 1. Tải Model và Processor với TỐI ƯU HÓA ---
|
16 |
MODEL_ID = "sunbv56/qwen2.5-vl-vqa-vibook"
|
17 |
+
print(f"🚀 Đang tải model '{MODEL_ID}' và processor...")
|
18 |
+
|
19 |
+
# *** TỐI ƯU HÓA 1: Lượng tử hóa (Quantization) ***
|
20 |
+
# Sử dụng `load_in_8bit=True` để tăng tốc đáng kể trên CPU.
|
21 |
+
# Yêu cầu `pip install bitsandbytes accelerate`
|
22 |
+
# Lưu ý: Lượng tử hóa sẽ không dùng `torch_dtype` vì nó hoạt động trên các kiểu dữ liệu khác.
|
23 |
+
# `device_map="auto"` sẽ tự động xử lý việc đặt model lên thiết bị.
|
24 |
+
use_gpu = torch.cuda.is_available()
|
|
|
|
|
25 |
try:
|
|
|
26 |
model = AutoModelForImageTextToText.from_pretrained(
|
27 |
+
MODEL_ID,
|
28 |
+
device_map="auto",
|
29 |
trust_remote_code=True,
|
30 |
+
# Chỉ lượng tử hóa khi chạy trên CPU để tiết kiệm tài nguyên và tăng tốc
|
31 |
+
load_in_8bit=not use_gpu,
|
32 |
+
# Nếu có GPU, sử dụng bfloat16/float16 để có hiệu năng tốt nhất
|
33 |
+
torch_dtype=torch.bfloat16 if use_gpu and torch.cuda.is_bf16_supported() else torch.float16
|
34 |
)
|
35 |
processor = AutoProcessor.from_pretrained(MODEL_ID, trust_remote_code=True, use_fast=True)
|
36 |
|
37 |
+
# *** TỐI ƯU HÓA 2: Sử dụng torch.compile() (cho PyTorch 2.0+) ***
|
38 |
+
# Biên dịch model để tăng tốc độ inference sau lần chạy đầu tiên.
|
39 |
+
# Chế độ 'reduce-overhead' tốt cho các input nhỏ và giảm gánh nặng của framework.
|
40 |
+
print("🚀 Đang biên dịch model với torch.compile()...")
|
41 |
+
model = torch.compile(model, mode="reduce-overhead", fullgraph=True)
|
42 |
+
print("✅ Model đã được biên dịch.")
|
|
|
|
|
|
|
43 |
|
44 |
model.eval()
|
45 |
+
print(f"✅ Model và processor đã được tải và tối ưu hóa thành công!")
|
|
|
46 |
except Exception as e:
|
47 |
+
print(f"❌ Lỗi khi tải/tối ưu hóa model/processor: {e}")
|
48 |
exit()
|
49 |
|
50 |
+
# --- 2. Hàm Inference Cốt lõi đã được sửa đổi để xử lý BATCH ---
|
51 |
+
def process_vqa_batch(images: List[Image.Image], questions: List[str]):
|
52 |
+
prompts = []
|
53 |
+
processed_images = []
|
54 |
+
|
55 |
+
# Chuẩn bị prompt và ảnh cho từng item trong batch
|
56 |
+
for image, question in zip(images, questions):
|
57 |
+
if image.mode != "RGB":
|
58 |
+
image = image.convert("RGB")
|
59 |
+
messages = [{"role": "user", "content": [{"type": "image"}, {"type": "text", "text": question}]}]
|
60 |
+
prompt_text = processor.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
|
61 |
+
prompts.append(prompt_text)
|
62 |
+
processed_images.append(image)
|
63 |
+
|
64 |
+
# Xử lý cả batch cùng một lúc
|
65 |
+
model_inputs = processor(text=prompts, images=processed_images, return_tensors="pt", padding=True).to(model.device)
|
66 |
+
|
67 |
+
generated_ids = model.generate(
|
68 |
+
**model_inputs,
|
69 |
+
max_new_tokens=128,
|
70 |
+
do_sample=False,
|
71 |
+
temperature=1.0,
|
72 |
+
eos_token_id=processor.tokenizer.eos_token_id,
|
73 |
+
pad_token_id=processor.tokenizer.pad_token_id
|
74 |
+
)
|
75 |
|
76 |
+
# Decode kết quả cho cả batch
|
77 |
generated_ids = generated_ids[:, model_inputs['input_ids'].shape[1]:]
|
78 |
+
responses = processor.batch_decode(generated_ids, skip_special_tokens=True)
|
79 |
+
|
80 |
+
# Strip() cho mỗi response trong list
|
81 |
+
return [res.strip() for res in responses]
|
82 |
+
|
83 |
+
|
84 |
+
# --- 3. Logic Chatbot ---
|
85 |
+
THINKING_HTML = """<div class="typing-indicator"><span></span><span></span><span></span></div>"""
|
86 |
+
CUSTOM_CSS = """@keyframes blink{0%{opacity:.2}20%{opacity:1}100%{opacity:.2}}.typing-indicator{display:flex;align-items:center;justify-content:flex-start;padding:8px 0}.typing-indicator span{height:10px;width:10px;margin:0 2px;background-color:#9E9E9E;border-radius:50%;animation:blink 1.4s infinite both}.typing-indicator span:nth-child(2){animation-delay:.2s}.typing-indicator span:nth-child(3){animation-delay:.4s}"""
|
87 |
+
|
88 |
+
# *** TỐI ƯU HÓA 3: Sửa đổi hàm để tương thích với BATCHING của Gradio ***
|
89 |
+
# Hàm này giờ nhận vào một list các câu hỏi và trả về một list các câu trả lời
|
90 |
+
def manual_chat_responder(user_questions: List[str], chat_histories: List[list], uploaded_images: List[Image.Image]):
|
91 |
+
# Do cách Gradio batching hoạt động, chúng ta chỉ lấy item đầu tiên
|
92 |
+
# vì mỗi người dùng có một giao diện riêng biệt.
|
93 |
+
# Tuy nhiên, hàm process_vqa_batch vẫn được thiết kế để xử lý batch thực sự.
|
94 |
+
user_question = user_questions[0]
|
95 |
+
chat_history = chat_histories[0]
|
96 |
+
uploaded_image = uploaded_images[0]
|
97 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
98 |
if uploaded_image is None:
|
99 |
gr.Warning("Vui lòng tải ảnh lên trước để đặt câu hỏi về nó.")
|
100 |
+
return [("", chat_history)] # Phải trả về list
|
101 |
if not user_question or not user_question.strip():
|
102 |
gr.Warning("Vui lòng nhập một câu hỏi.")
|
103 |
+
return [("", chat_history)] # Phải trả về list
|
104 |
+
|
105 |
chat_history.append({"role": "user", "content": user_question})
|
106 |
chat_history.append({"role": "assistant", "content": THINKING_HTML})
|
107 |
+
|
108 |
+
# Tạm thời yield để cập nhật UI
|
109 |
+
# Gradio batching không hỗ trợ yield trực tiếp, nên chúng ta sẽ bỏ qua bước này
|
110 |
+
# và trả về kết quả cuối cùng. Người dùng sẽ thấy indicator trong một khoảng thời gian ngắn.
|
111 |
+
|
112 |
+
# Gọi hàm xử lý batch (dù chỉ có 1 item)
|
113 |
+
bot_response = process_vqa_batch([uploaded_image], [user_question])[0]
|
114 |
|
115 |
chat_history[-1]["content"] = bot_response
|
116 |
+
|
117 |
+
# Phải trả về một list các kết quả, tương ứng với batch đầu vào
|
118 |
+
return [("", chat_history)]
|
119 |
|
120 |
+
|
121 |
+
# Hàm chạy ví dụ không cần batching vì nó chỉ là một hành động đơn lẻ
|
122 |
def run_example(evt: SelectData):
|
123 |
selected_example = example_list[evt.index]
|
124 |
image_path, question = selected_example
|
|
|
131 |
]
|
132 |
yield image, question, chat_history
|
133 |
|
134 |
+
bot_response = process_vqa_batch([image], [question])[0]
|
135 |
|
136 |
chat_history[-1]["content"] = bot_response
|
137 |
yield image, question, chat_history
|
|
|
139 |
def clear_chat():
|
140 |
return []
|
141 |
|
142 |
+
# --- 4. Định nghĩa Giao diện Người dùng Gradio ---
|
143 |
with gr.Blocks(theme=gr.themes.Soft(primary_hue="blue", secondary_hue="sky"), title="Vibook VQA Chatbot", css=CUSTOM_CSS) as demo:
|
144 |
+
gr.Markdown("# 🤖 Vibook VQA Chatbot (Optimized)")
|
145 |
|
146 |
example_list = [
|
147 |
["./assets/book_example_1.jpg", "Đâu là tên đúng của cuốn sách này?"],
|
|
|
160 |
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=[])
|
161 |
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)
|
162 |
|
163 |
+
# --- 5. Xử lý Sự kiện với TỐI ƯU HÓA BATCHING ---
|
164 |
+
question_input.submit(
|
165 |
+
fn=manual_chat_responder,
|
166 |
+
inputs=[question_input, chatbot, image_input],
|
167 |
+
outputs=[question_input, chatbot]
|
168 |
+
).batch(batch_size=4, max_latency=0.1) # Gom tối đa 4 request, hoặc xử lý sau mỗi 0.1 giây
|
169 |
+
|
170 |
example_dataset.select(fn=run_example, inputs=None, outputs=[image_input, question_input, chatbot], show_progress="full")
|
171 |
+
|
172 |
image_input.upload(fn=clear_chat, inputs=None, outputs=[chatbot])
|
173 |
image_input.clear(fn=clear_chat, inputs=None, outputs=[chatbot])
|
174 |
|
175 |
+
# --- Phần cuối ---
|
176 |
+
def setup_examples():
|
177 |
ASSETS_DIR = "assets"
|
178 |
if not os.path.exists(ASSETS_DIR):
|
179 |
os.makedirs(ASSETS_DIR)
|
|
|
198 |
except requests.exceptions.RequestException as e:
|
199 |
print(f" Lỗi khi tải {filename}: {e}")
|
200 |
|
201 |
+
if __name__ == "__main__":
|
202 |
+
setup_examples()
|
203 |
demo.launch(debug=True)
|