sunbv56 commited on
Commit
22fe62c
·
verified ·
1 Parent(s): d5bebb0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +45 -15
app.py CHANGED
@@ -1,4 +1,4 @@
1
- # app.py (Phiên bản cuối cùng: Sửa lỗi cảnh báo và thêm tin nhắn "Thinking...")
2
 
3
  import gradio as gr
4
  import torch
@@ -32,8 +32,6 @@ def process_vqa(image: Image.Image, question: str):
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
 
35
- # SỬA LỖI 1: Ghi đè `temperature` để tắt cảnh báo.
36
- # Đặt là 1.0 (trung tính) vì do_sample=False nên nó sẽ không được sử dụng.
37
  generated_ids = model.generate(
38
  **model_inputs,
39
  max_new_tokens=1024,
@@ -48,6 +46,44 @@ def process_vqa(image: Image.Image, question: str):
48
  return response
49
 
50
  # --- 3. Logic Chatbot ---
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
51
  # Hàm dành cho việc người dùng tự nhập câu hỏi
52
  def manual_chat_responder(user_question: str, chat_history: list, uploaded_image: Image.Image):
53
  if uploaded_image is None:
@@ -57,46 +93,41 @@ def manual_chat_responder(user_question: str, chat_history: list, uploaded_image
57
  gr.Warning("Vui lòng nhập một câu hỏi.")
58
  return "", chat_history
59
 
60
- # THÊM TÍNH NĂNG 2: Hiển thị tin nhắn chờ
61
  chat_history.append({"role": "user", "content": user_question})
62
- chat_history.append({"role": "assistant", "content": "🤔 Thinking..."})
 
63
  yield "", chat_history
64
 
65
  bot_response = process_vqa(uploaded_image, user_question)
66
 
67
- # THÊM TÍNH NĂNG 2: Cập nhật tin nhắn chờ bằng câu trả lời thật
68
  chat_history[-1]["content"] = bot_response
69
  yield "", chat_history
70
 
71
  # Hàm dành riêng cho việc xử lý khi nhấn vào ví dụ
72
  def run_example(evt: SelectData):
73
- # Dùng list toàn cục đã được định nghĩa trong khối `with`
74
  selected_example = example_list[evt.index]
75
  image_path, question = selected_example
76
  gr.Info(f"Đang chạy ví dụ: \"{question}\"")
77
  image = Image.open(image_path).convert("RGB")
78
 
79
- # THÊM TÍNH NĂNG 2: Hiển thị tin nhắn chờ
80
  chat_history = [
81
  {"role": "user", "content": question},
82
- {"role": "assistant", "content": "🤔 Thinking..."}
83
  ]
84
- # `yield` lần đầu để cập nhật UI ngay lập tức
85
  yield image, question, chat_history
86
 
87
- # Chạy xử lý và lấy câu trả lời thật
88
  bot_response = process_vqa(image, question)
89
 
90
- # THÊM TÍNH NĂNG 2: Cập nhật tin nhắn chờ bằng câu trả lời thật
91
  chat_history[-1]["content"] = bot_response
92
- # `yield` lần cuối để hiển thị kết quả cuối cùng
93
  yield image, question, chat_history
94
 
95
  def clear_chat():
96
  return []
97
 
98
  # --- 4. Định nghĩa Giao diện Người dùng Gradio ---
99
- with gr.Blocks(theme=gr.themes.Soft(primary_hue="blue", secondary_hue="sky"), title="Vibook VQA Chatbot") as demo:
 
100
  gr.Markdown("# 🤖 Vibook VQA Chatbot")
101
 
102
  example_list = [
@@ -119,7 +150,6 @@ with gr.Blocks(theme=gr.themes.Soft(primary_hue="blue", secondary_hue="sky"), ti
119
  # --- 5. Xử lý Sự kiện ---
120
  question_input.submit(fn=manual_chat_responder, inputs=[question_input, chatbot, image_input], outputs=[question_input, chatbot])
121
 
122
- # THÊM TÍNH NĂNG 2: Hàm `run_example` giờ là một generator, Gradio sẽ tự động xử lý các `yield`
123
  example_dataset.select(fn=run_example, inputs=None, outputs=[image_input, question_input, chatbot], show_progress="full")
124
 
125
  image_input.upload(fn=clear_chat, inputs=None, outputs=[chatbot])
 
1
+ # app.py
2
 
3
  import gradio as gr
4
  import torch
 
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
 
 
 
35
  generated_ids = model.generate(
36
  **model_inputs,
37
  max_new_tokens=1024,
 
46
  return response
47
 
48
  # --- 3. Logic Chatbot ---
49
+ # ### THAY ĐỔI MỚI 1: Định nghĩa HTML và CSS cho hiệu ứng động ###
50
+ # HTML cho hiệu ứng "đang gõ"
51
+ THINKING_HTML = """
52
+ <div class="typing-indicator">
53
+ <span></span>
54
+ <span></span>
55
+ <span></span>
56
+ </div>
57
+ """
58
+ # CSS để tạo hiệu ứng
59
+ CUSTOM_CSS = """
60
+ @keyframes blink {
61
+ 0% { opacity: .2; }
62
+ 20% { opacity: 1; }
63
+ 100% { opacity: .2; }
64
+ }
65
+ .typing-indicator {
66
+ display: flex;
67
+ align-items: center;
68
+ justify-content: flex-start; /* Căn trái */
69
+ padding: 8px 0; /* Thêm chút khoảng đệm */
70
+ }
71
+ .typing-indicator span {
72
+ height: 10px;
73
+ width: 10px;
74
+ margin: 0 2px;
75
+ background-color: #9E9E9E; /* Màu xám */
76
+ border-radius: 50%;
77
+ animation: blink 1.4s infinite both;
78
+ }
79
+ .typing-indicator span:nth-child(2) {
80
+ animation-delay: .2s;
81
+ }
82
+ .typing-indicator span:nth-child(3) {
83
+ animation-delay: .4s;
84
+ }
85
+ """
86
+
87
  # Hàm dành cho việc người dùng tự nhập câu hỏi
88
  def manual_chat_responder(user_question: str, chat_history: list, uploaded_image: Image.Image):
89
  if uploaded_image is None:
 
93
  gr.Warning("Vui lòng nhập một câu hỏi.")
94
  return "", chat_history
95
 
 
96
  chat_history.append({"role": "user", "content": user_question})
97
+ # ### THAY ĐỔI MỚI 2: Sử dụng HTML động thay cho text tĩnh ###
98
+ chat_history.append({"role": "assistant", "content": THINKING_HTML})
99
  yield "", chat_history
100
 
101
  bot_response = process_vqa(uploaded_image, user_question)
102
 
 
103
  chat_history[-1]["content"] = bot_response
104
  yield "", chat_history
105
 
106
  # Hàm dành riêng cho việc xử lý khi nhấn vào ví dụ
107
  def run_example(evt: SelectData):
 
108
  selected_example = example_list[evt.index]
109
  image_path, question = selected_example
110
  gr.Info(f"Đang chạy ví dụ: \"{question}\"")
111
  image = Image.open(image_path).convert("RGB")
112
 
113
+ # ### THAY ĐỔI MỚI 3: Sử dụng HTML động thay cho text tĩnh ###
114
  chat_history = [
115
  {"role": "user", "content": question},
116
+ {"role": "assistant", "content": THINKING_HTML}
117
  ]
 
118
  yield image, question, chat_history
119
 
 
120
  bot_response = process_vqa(image, question)
121
 
 
122
  chat_history[-1]["content"] = bot_response
 
123
  yield image, question, chat_history
124
 
125
  def clear_chat():
126
  return []
127
 
128
  # --- 4. Định nghĩa Giao diện Người dùng Gradio ---
129
+ # ### THAY ĐỔI MỚI 4: Thêm CSS vào Blocks ###
130
+ with gr.Blocks(theme=gr.themes.Soft(primary_hue="blue", secondary_hue="sky"), title="Vibook VQA Chatbot", css=CUSTOM_CSS) as demo:
131
  gr.Markdown("# 🤖 Vibook VQA Chatbot")
132
 
133
  example_list = [
 
150
  # --- 5. Xử lý Sự kiện ---
151
  question_input.submit(fn=manual_chat_responder, inputs=[question_input, chatbot, image_input], outputs=[question_input, chatbot])
152
 
 
153
  example_dataset.select(fn=run_example, inputs=None, outputs=[image_input, question_input, chatbot], show_progress="full")
154
 
155
  image_input.upload(fn=clear_chat, inputs=None, outputs=[chatbot])