Delete app.py
Browse files
app.py
DELETED
@@ -1,197 +0,0 @@
|
|
1 |
-
import gradio as gr
|
2 |
-
from infer.worldmodel import Worldinfer
|
3 |
-
from PIL import Image
|
4 |
-
import re
|
5 |
-
# 初始化模型
|
6 |
-
llm_path = '/mnt/B8E84E9EE84E5B30/rwkv-models/world_rwkv/world_weights/rwkv-0'
|
7 |
-
encoder_path = '/mnt/B8E84E9EE84E5B30/rwkv-models/world_rwkv/siglip2-base-patch16-384/'
|
8 |
-
encoder_type = 'siglip'
|
9 |
-
|
10 |
-
# 全局变量存储当前上传的图片和模型状态
|
11 |
-
current_image = None
|
12 |
-
current_state = None
|
13 |
-
first_question = False # 存储模型状态
|
14 |
-
# 是否是第一轮对话
|
15 |
-
# 初始化模型
|
16 |
-
model = Worldinfer(model_path=llm_path, encoder_type=encoder_type, encoder_path=encoder_path)
|
17 |
-
|
18 |
-
# 处理用户输入的核心逻辑
|
19 |
-
import html # 导入html库
|
20 |
-
|
21 |
-
import re
|
22 |
-
|
23 |
-
# 处理用户输入的核心逻辑
|
24 |
-
def chat_fn(user_input, chat_history, image=None):
|
25 |
-
global current_image, current_state, first_question
|
26 |
-
|
27 |
-
# 如果上传了新图片,更新当前图片并重置状态
|
28 |
-
if image is not None:
|
29 |
-
current_image = image
|
30 |
-
|
31 |
-
# 如果没有图片,提示用户上传
|
32 |
-
if current_image is None:
|
33 |
-
bot_response = "请先上传一张图片!"
|
34 |
-
chat_history.append((user_input, bot_response))
|
35 |
-
return "", chat_history
|
36 |
-
|
37 |
-
# 确保图片是PIL Image格式
|
38 |
-
if not isinstance(current_image, Image.Image) and current_image != 'none':
|
39 |
-
current_image = Image.fromarray(current_image)
|
40 |
-
|
41 |
-
# 构造提示文本
|
42 |
-
prompt = f'\x16User: {user_input}\x17Assistant:'
|
43 |
-
|
44 |
-
# 生成结果,传入当前状态
|
45 |
-
try:
|
46 |
-
if first_question:
|
47 |
-
result, state = model.generate(prompt, current_image, state=None)
|
48 |
-
else:
|
49 |
-
result, state = model.generate(prompt, 'none', state=current_state)
|
50 |
-
|
51 |
-
first_question = False
|
52 |
-
bot_response, current_state = result, state
|
53 |
-
|
54 |
-
# 解析<think>和</think>标签
|
55 |
-
think_pattern = re.compile(r'<think>(.*?)</think>', re.DOTALL)
|
56 |
-
think_matches = think_pattern.findall(bot_response)
|
57 |
-
|
58 |
-
# 解析<answer></answer>标签
|
59 |
-
answer_pattern = re.compile(r'<answer>(.*?)</answer>', re.DOTALL)
|
60 |
-
answer_matches = answer_pattern.findall(bot_response)
|
61 |
-
|
62 |
-
# 构造最终的输出
|
63 |
-
final_response = ""
|
64 |
-
for match in think_matches:
|
65 |
-
final_response += f"<details><summary>Think 🤔 </summary>{html.escape(match)}</details>"
|
66 |
-
|
67 |
-
for match in answer_matches:
|
68 |
-
final_response += "Answer 💡"
|
69 |
-
final_response += "\n"
|
70 |
-
final_response += html.escape(match)
|
71 |
-
|
72 |
-
# 转义HTML标签
|
73 |
-
bot_response = final_response
|
74 |
-
|
75 |
-
except Exception as e:
|
76 |
-
bot_response = f"生成回复时出错: {str(e)}"
|
77 |
-
current_state = None # 出错时重置状态
|
78 |
-
|
79 |
-
# 更新对话历史
|
80 |
-
chat_history.append((user_input, bot_response))
|
81 |
-
|
82 |
-
# 返回更新后的组件状态
|
83 |
-
return "", chat_history # 清空输入框,更新聊天记录
|
84 |
-
# 处理图片上传
|
85 |
-
def update_image(image):
|
86 |
-
global current_image, current_state,first_question
|
87 |
-
current_image = image
|
88 |
-
current_state = None
|
89 |
-
first_question = True
|
90 |
-
# print('1111111111111111111',first_question) # 上传新图片时重置状态
|
91 |
-
return "图片已上传成功!可以开始提问了。"
|
92 |
-
|
93 |
-
# 清空图片
|
94 |
-
def clear_image():
|
95 |
-
global current_image, current_state
|
96 |
-
current_image = None
|
97 |
-
current_state = None # 清空图片时重置状态
|
98 |
-
# 返回None给image组件,文本给status组件
|
99 |
-
return None, "图片已清除,请上传新图片。"
|
100 |
-
|
101 |
-
# 清空历史和图片
|
102 |
-
def clear_all():
|
103 |
-
global current_image, current_state
|
104 |
-
current_image = None
|
105 |
-
current_state = None # 清空所有时重置状态
|
106 |
-
return [], "", "图片和对话已清空,请重新上传图片。"
|
107 |
-
|
108 |
-
# 不使用图片输入的聊天函数
|
109 |
-
def chat_without_image_update(user_input, chat_history):
|
110 |
-
return chat_fn(user_input, chat_history)
|
111 |
-
|
112 |
-
# 界面布局组件
|
113 |
-
with gr.Blocks(title="WORLD RWKV", theme=gr.themes.Soft()) as demo:
|
114 |
-
gr.Markdown("# WORLD RWKV")
|
115 |
-
gr.Markdown("上传一张图片,然后可以进行多轮提问")
|
116 |
-
|
117 |
-
with gr.Row():
|
118 |
-
# 左侧图片上传区
|
119 |
-
with gr.Column(scale=2):
|
120 |
-
image_input = gr.Image(
|
121 |
-
type="pil",
|
122 |
-
label="上传图片",
|
123 |
-
height=400
|
124 |
-
)
|
125 |
-
|
126 |
-
# 图片状态和操作
|
127 |
-
with gr.Row():
|
128 |
-
image_status = gr.Textbox(
|
129 |
-
label="图片状态",
|
130 |
-
value="请上传图片",
|
131 |
-
interactive=False
|
132 |
-
)
|
133 |
-
clear_img_btn = gr.Button("删除图片")
|
134 |
-
|
135 |
-
# 右侧对话区
|
136 |
-
with gr.Column(scale=3):
|
137 |
-
chatbot = gr.Chatbot(
|
138 |
-
label="对话记录",
|
139 |
-
bubble_full_width=False,
|
140 |
-
height=500
|
141 |
-
)
|
142 |
-
|
143 |
-
# 控制区域
|
144 |
-
with gr.Row():
|
145 |
-
# 输入组件
|
146 |
-
user_input = gr.Textbox(
|
147 |
-
placeholder="请输入问题...",
|
148 |
-
scale=7,
|
149 |
-
container=False,
|
150 |
-
label="问题输入"
|
151 |
-
)
|
152 |
-
|
153 |
-
# 操作按钮
|
154 |
-
with gr.Column(scale=1):
|
155 |
-
submit_btn = gr.Button("发送", variant="primary")
|
156 |
-
clear_btn = gr.Button("清空所有")
|
157 |
-
|
158 |
-
# 事件绑定
|
159 |
-
# 图片上传事件
|
160 |
-
image_input.change(
|
161 |
-
fn=update_image,
|
162 |
-
inputs=[image_input],
|
163 |
-
outputs=[image_status]
|
164 |
-
)
|
165 |
-
|
166 |
-
# 删除图片按钮事件 - 修复输出顺序,确保类型匹配
|
167 |
-
clear_img_btn.click(
|
168 |
-
fn=lambda: (None, "图片已清除,请上传新图片。"), # 使用lambda直接返回正确类型
|
169 |
-
inputs=None,
|
170 |
-
outputs=[image_input, image_status]
|
171 |
-
)
|
172 |
-
|
173 |
-
# 发送按钮事件
|
174 |
-
submit_btn.click(
|
175 |
-
fn=chat_fn,
|
176 |
-
inputs=[user_input, chatbot, image_input],
|
177 |
-
outputs=[user_input, chatbot]
|
178 |
-
)
|
179 |
-
|
180 |
-
# 输入框回车事件 - 使用不需要图片参数的函数
|
181 |
-
user_input.submit(
|
182 |
-
fn=chat_without_image_update,
|
183 |
-
inputs=[user_input, chatbot],
|
184 |
-
outputs=[user_input, chatbot]
|
185 |
-
)
|
186 |
-
|
187 |
-
# 清空按钮事件
|
188 |
-
clear_btn.click(
|
189 |
-
fn=lambda: ([], "", "图片和对话已清空,请重新上传图片。", None), # 修复返回值
|
190 |
-
inputs=None,
|
191 |
-
outputs=[chatbot, user_input, image_status, image_input],
|
192 |
-
queue=False
|
193 |
-
)
|
194 |
-
|
195 |
-
# 启动应用
|
196 |
-
if __name__ == "__main__":
|
197 |
-
demo.launch(server_name="127.0.0.1", server_port=7860)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|