Alic-Li commited on
Commit
0d2d1a7
·
verified ·
1 Parent(s): d01c89a

Delete app.py

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