dj86 commited on
Commit
12514b0
1 Parent(s): dc9f26d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +421 -59
app.py CHANGED
@@ -1,63 +1,425 @@
 
 
1
  import gradio as gr
2
- from huggingface_hub import InferenceClient
3
-
4
- """
5
- For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
6
- """
7
- client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
8
-
9
-
10
- def respond(
11
- message,
12
- history: list[tuple[str, str]],
13
- system_message,
14
- max_tokens,
15
- temperature,
16
- top_p,
17
- ):
18
- messages = [{"role": "system", "content": system_message}]
19
-
20
- for val in history:
21
- if val[0]:
22
- messages.append({"role": "user", "content": val[0]})
23
- if val[1]:
24
- messages.append({"role": "assistant", "content": val[1]})
25
-
26
- messages.append({"role": "user", "content": message})
27
-
28
- response = ""
29
-
30
- for message in client.chat_completion(
31
- messages,
32
- max_tokens=max_tokens,
33
- stream=True,
34
- temperature=temperature,
35
- top_p=top_p,
36
- ):
37
- token = message.choices[0].delta.content
38
-
39
- response += token
40
- yield response
41
-
42
- """
43
- For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
44
- """
45
- demo = gr.ChatInterface(
46
- respond,
47
- additional_inputs=[
48
- gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
49
- gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
50
- gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
51
- gr.Slider(
52
- minimum=0.1,
53
- maximum=1.0,
54
- value=0.95,
55
- step=0.05,
56
- label="Top-p (nucleus sampling)",
57
- ),
58
- ],
59
- )
60
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
61
 
62
  if __name__ == "__main__":
63
- demo.launch()
 
1
+ import os
2
+ import json
3
  import gradio as gr
4
+ import openai
5
+ import requests
6
+ import csv
7
+ import argparse
8
+ import shutil
9
+ from models.vlog4chat import Vlogger4chat
10
+ from models.vlog4debate import Debate
11
+ from utils.utils import download_video
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
12
 
13
+ #prompt_templates = {"Default ChatGPT": ""}
14
+ os.environ['HF_HOME'] = '/root/autodl-tmp/cache/'
15
+
16
+ parser = argparse.ArgumentParser()
17
+ parser.add_argument('--video_path', default='examples/training.mp4')
18
+ parser.add_argument('--alpha', default=10, type=int, help='Determine the maximum segment number for KTS algorithm, the larger the value, the fewer segments.')
19
+ parser.add_argument('--beta', default=1, type=int, help='The smallest time gap between successive clips, in seconds.')
20
+ parser.add_argument('--data_dir', default='./examples', type=str, help='Directory for saving videos and logs.')
21
+ parser.add_argument('--tmp_dir', default='./tmp', type=str, help='Directory for saving intermediate files.')
22
+
23
+ # * Models settings *
24
+ parser.add_argument('--openai_api_key', default='xxx', type=str, help='OpenAI API key')
25
+ parser.add_argument('--image_caption', action='store_true', dest='image_caption', default=True, help='Set this flag to True if you want to use BLIP Image Caption')
26
+ parser.add_argument('--dense_caption', action='store_true', dest='dense_caption', default=True, help='Set this flag to True if you want to use Dense Caption')
27
+ parser.add_argument('--feature_extractor', default='./clip-vit-base-patch32', help='Select the feature extractor model for video segmentation')
28
+ parser.add_argument('--feature_extractor_device', choices=['cuda', 'cpu'], default='cuda', help='Select the device: cuda or cpu')
29
+ parser.add_argument('--image_captioner', choices=['blip2-opt', 'blip2-flan-t5', 'blip'], dest='captioner_base_model', default='blip2-opt', help='blip2 requires 15G GPU memory, blip requires 6G GPU memory')
30
+ parser.add_argument('--image_captioner_device', choices=['cuda', 'cpu'], default='cuda', help='Select the device: cuda or cpu, gpu memory larger than 14G is recommended')
31
+ parser.add_argument('--dense_captioner_device', choices=['cuda', 'cpu'], default='cuda', help='Select the device: cuda or cpu, < 6G GPU is not recommended>')
32
+ parser.add_argument('--audio_translator', default='large')
33
+ parser.add_argument('--audio_translator_device', choices=['cuda', 'cpu'], default='cuda')
34
+ parser.add_argument('--gpt_version', choices=['gpt-3.5-turbo'], default='gpt-3.5-turbo')
35
+
36
+ args = parser.parse_args()
37
+
38
+ vlogger = Vlogger4chat(args)
39
+
40
+ def get_empty_state():
41
+ return {"total_tokens": 0, "messages": []}
42
+
43
+
44
+ def submit_message(prompt, state):
45
+ history = state['messages']
46
+
47
+ if not prompt:
48
+ return gr.update(value=''), [(history[i]['content'], history[i+1]['content']) for i in range(0, len(history)-1, 2)], state
49
+
50
+ prompt_msg = { "role": "user", "content": prompt }
51
+
52
+ try:
53
+ history.append(prompt_msg)
54
+ answer = vlogger.chat2video(prompt)
55
+ history.append({"role": "system", "content": answer})
56
+
57
+
58
+ except Exception as e:
59
+ history.append(prompt_msg)
60
+ history.append({
61
+ "role": "system",
62
+ "content": f"Error: {e}"
63
+ })
64
+
65
+ chat_messages = [(history[i]['content'], history[i+1]['content']) for i in range(0, len(history)-1, 2)]
66
+ return '', chat_messages, state
67
+
68
+
69
+ def submit_message_debate(prompt, state):
70
+ history = state['messages']
71
+
72
+ if not prompt:
73
+ return gr.update(value=''), [(history[i]['content'], history[i+1]['content']) for i in range(0, len(history)-1, 2)], state
74
+
75
+ prompt_msg = { "role": "user", "content": prompt }
76
+
77
+ try:
78
+ history.append(prompt_msg)
79
+
80
+ debate_topic = ""
81
+ while debate_topic == "":
82
+ debate_topic = prompt
83
+
84
+ config = json.load(open("/root/autodl-tmp/cache/config4all.json", "r"))
85
+ config['debate_topic'] = debate_topic
86
+
87
+ debate = Debate(num_players=3, config=config, temperature=0, sleep_time=0)
88
+ answer = debate.run()
89
+
90
+ #chat_messages = [(res["debate_topic"]), (res["base_answer"]), (res["debate_answer"]), (res["Reason"])]
91
+ history.append({"role": "system", "content": answer})
92
+
93
+
94
+ except Exception as e:
95
+ history.append(prompt_msg)
96
+ history.append({
97
+ "role": "system",
98
+ "content": f"Error: {e}"
99
+ })
100
+
101
+ chat_messages = [(history[i]['content'], history[i+1]['content']) for i in range(0, len(history)-1, 2)]
102
+ return '', chat_messages, state
103
+
104
+
105
+ def clear_conversation():
106
+ vlogger.clean_history()
107
+ return gr.update(value=None, visible=True), gr.update(value=None, visible=True), gr.update(value=None, interactive=True), None, gr.update(value=None, visible=True), get_empty_state()
108
+
109
+
110
+ # download video from any online URL
111
+ def subvid_fn(vid):
112
+ print(vid)
113
+ save_path = download_video(vid)
114
+ return gr.update(value=save_path)
115
+
116
+
117
+ # 本地上传,适用于Running on local URL: http://127.0.0.1:6006
118
+ def uploaded_video(video_file):
119
+ UPLOAD_FOLDER = "./examples"
120
+ if not os.path.exists(UPLOAD_FOLDER):
121
+ os.mkdir(UPLOAD_FOLDER)
122
+ shutil.copy(video_file, UPLOAD_FOLDER)
123
+ gr.Info("File Uploaded!!!")
124
+ save_path = os.path.join(UPLOAD_FOLDER, os.path.basename(video_file))
125
+ return gr.update(value=save_path)
126
+
127
+
128
+ def vlog_fn(vid_path):
129
+ print(vid_path)
130
+ if vid_path is None:
131
+ log_text = "====== Please choose existing video from the library or provide video URL 🤔====="
132
+ else:
133
+ log_list = vlogger.video2log(vid_path)
134
+ log_text = "\n".join(log_list)
135
+ return gr.update(value=log_text, visible=True)
136
+
137
+ # 初始化一个空的答案记录字典
138
+ answers = {}
139
+
140
+ # 定义处理用户选择的函数
141
+ def submit_answers_pretest(question1, question2, question3, question4, question5, question6, question7, question8, question9, question10):
142
+ answers['Question 1'] = question1
143
+ answers['Question 2'] = question2
144
+ answers['Question 3'] = question3
145
+ answers['Question 4'] = question4
146
+ answers['Question 5'] = question5
147
+ answers['Question 6'] = question6
148
+ answers['Question 7'] = question7
149
+ answers['Question 8'] = question8
150
+ answers['Question 9'] = question9
151
+ answers['Question 10'] = question10
152
+
153
+ # 可以将结果保存到文件
154
+ with open('answers4pretest.txt', 'a') as f:
155
+ f.write(f"Question 1: {question1}\n")
156
+ f.write(f"Question 2: {question2}\n")
157
+ f.write(f"Question 3: {question3}\n")
158
+ f.write(f"Question 4: {question4}\n")
159
+ f.write(f"Question 5: {question5}\n")
160
+ f.write(f"Question 6: {question6}\n")
161
+ f.write(f"Question 7: {question7}\n")
162
+ f.write(f"Question 8: {question8}\n")
163
+ f.write(f"Question 9: {question9}\n")
164
+ f.write(f"Question 10: {question10}\n\n")
165
+
166
+ # 返回一个确认消息
167
+ return "谢谢你提交答案!"
168
+
169
+ def submit_answers_posttest(question1, question2, question3, question4, question5, question6, question7, question8, question9, question10):
170
+ answers['Question 1'] = question1
171
+ answers['Question 2'] = question2
172
+ answers['Question 3'] = question3
173
+ answers['Question 4'] = question4
174
+ answers['Question 5'] = question5
175
+ answers['Question 6'] = question6
176
+ answers['Question 7'] = question7
177
+ answers['Question 8'] = question8
178
+ answers['Question 9'] = question9
179
+ answers['Question 10'] = question10
180
+
181
+ # 可以将结果保存到文件
182
+ with open('answers4posttest.txt', 'a') as f:
183
+ f.write(f"Question 1: {question1}\n")
184
+ f.write(f"Question 2: {question2}\n")
185
+ f.write(f"Question 3: {question3}\n")
186
+ f.write(f"Question 4: {question4}\n")
187
+ f.write(f"Question 5: {question5}\n")
188
+ f.write(f"Question 6: {question6}\n")
189
+ f.write(f"Question 7: {question7}\n")
190
+ f.write(f"Question 8: {question8}\n")
191
+ f.write(f"Question 9: {question9}\n")
192
+ f.write(f"Question 10: {question10}\n\n")
193
+
194
+ # 返回一个确认消息
195
+ return "谢谢你提交答案!"
196
+
197
+ css = """
198
+ #col-container {max-width: 80%; margin-left: auto; margin-right: auto;}
199
+ #video_inp {min-height: 100px}
200
+ #chatbox {min-height: 100px;}
201
+ #header {text-align: center;}
202
+ #hint {font-size: 1.0em; padding: 0.5em; margin: 0;}
203
+ .message { font-size: 1.2em; }
204
+ """
205
+
206
+ with gr.Blocks(css=css) as demo:
207
+
208
+ with gr.Tabs():
209
+ # 第一个标签页
210
+ with gr.TabItem("第一步(预先测试)"):
211
+ gr.Markdown("## Survey: Please answer the following questions")
212
+
213
+ # 问题1
214
+ question1 = gr.Radio(
215
+ choices=["1", "2", "3", "4", "5"],
216
+ label="1. What is your favorite color?",
217
+ )
218
+
219
+ # 问题2
220
+ question2 = gr.Radio(
221
+ choices=["1", "2", "3", "4", "5"],
222
+ label="2. What is your preferred mode of transport?",
223
+ )
224
+
225
+ # 问题3
226
+ question3 = gr.Radio(
227
+ choices=["1", "2", "3", "4", "5"],
228
+ label="3. Which type of cuisine do you prefer?",
229
+ )
230
+
231
+ # 问题4
232
+ question4 = gr.Radio(
233
+ choices=["1", "2", "3", "4", "5"],
234
+ label="4. What is your favorite color?",
235
+ )
236
+
237
+ # 问题5
238
+ question5 = gr.Radio(
239
+ choices=["1", "2", "3", "4", "5"],
240
+ label="5. What is your preferred mode of transport?",
241
+ )
242
+
243
+ # 问题6
244
+ question6 = gr.Radio(
245
+ choices=["1", "2", "3", "4", "5"],
246
+ label="6. Which type of cuisine do you prefer?",
247
+ )
248
+
249
+ # 问题7
250
+ question7 = gr.Radio(
251
+ choices=["1", "2", "3", "4", "5"],
252
+ label="7. Which type of cuisine do you prefer?",
253
+ )
254
+
255
+ # 问题8
256
+ question8 = gr.Radio(
257
+ choices=["1", "2", "3", "4", "5"],
258
+ label="8. What is your favorite color?",
259
+ )
260
+
261
+ # 问题9
262
+ question9 = gr.Radio(
263
+ choices=["1", "2", "3", "4", "5"],
264
+ label="9. What is your preferred mode of transport?",
265
+ )
266
+
267
+ # 问题10
268
+ question10 = gr.Radio(
269
+ choices=["1", "2", "3", "4", "5"],
270
+ label="10. Which type of cuisine do you prefer?",
271
+ )
272
+
273
+ # 提交按钮
274
+ submit_button = gr.Button("Submit Answers")
275
+
276
+ # 显示结果
277
+ output = gr.Textbox(label="Message")
278
+
279
+ # 点击提交按钮时,调用submit_answers函数
280
+ submit_button.click(
281
+ submit_answers_pretest,
282
+ inputs=[question1, question2, question3, question4, question5, question6, question7, question8, question9, question10],
283
+ outputs=output
284
+ )
285
+ # 第二个标签页
286
+ with gr.TabItem("第二步(VLog使用)"):
287
+ state = gr.State(get_empty_state())
288
+
289
+ with gr.Column(elem_id="col-container"):
290
+ gr.Markdown("""## 🎞️ 视频Chat:
291
+ Powered by CLIP, BLIP2, GRIT, RAM++, PaddleOCR, Whisper, Custom LLMs and LangChain""",
292
+ elem_id="header")
293
+
294
+ with gr.Row():
295
+ with gr.Column():
296
+ video_inp = gr.Video(label="video_input")
297
+ # 设置点击事件,点击按钮后保存上传的视频
298
+ #save_btn = gr.Button("Upload Video")
299
+ # 本地上传,适用于Running on local URL: http://127.0.0.1:6006
300
+ #save_btn.click(uploaded_video, [video_inp], [video_inp])
301
+
302
+ gr.Markdown("请在下方输入需要播放的视频完整网址, *e.g.* *B站地址*", elem_id="hint")
303
+ with gr.Row():
304
+ video_id = gr.Textbox(value="", placeholder="Download video url", show_label=False)
305
+ vidsub_btn = gr.Button("上传网站视频")
306
+
307
+ chatbot = gr.Chatbot(elem_id="chatbox")
308
+ input_message = gr.Textbox(show_label=False, placeholder="输入文字并按回车", visible=True).style(container=False)
309
+ btn_submit = gr.Button("提问视频内容")
310
+
311
+ gr.Markdown("如果对上面的回答不满意,请在下方输入需要辩论的问题, *e.g.* *方差越小越好?*", elem_id="hint")
312
+ #chatbot_debate = gr.Chatbot(elem_id="chatbox")
313
+ input_message_debate = gr.Textbox(show_label=False, placeholder="输入文字并按回车", visible=True).style(container=False)
314
+ btn_submit_debate = gr.Button("发起问题辩论")
315
+
316
+ btn_clear_conversation = gr.Button("🔃 开始新的对话")
317
+
318
+ with gr.Column():
319
+ vlog_btn = gr.Button("点击此处,生成视频日志")
320
+ vlog_outp = gr.Textbox(label="Document output", lines=60)
321
+ total_tokens_str = gr.Markdown(elem_id="total_tokens_str")
322
+
323
+ gr.Markdown("请点击下方视频(任意选择一个视频进行播放)", elem_id="hint")
324
+ examples = gr.Examples(
325
+ examples=[
326
+ ["examples/BV11H4y1F7uH.mp4"],
327
+ ["examples/BV1J14y1d7X4.mp4"],
328
+ ],
329
+ inputs=[video_inp],
330
+ )
331
+
332
+ gr.HTML('''<br><br><br><center>You can duplicate this Space to skip the queue:<a href="https://huggingface.co/spaces/anzorq/chatgpt-demo?duplicate=true"><img src="https://bit.ly/3gLdBN6" alt="Duplicate Space"></a><br></center>''')
333
+
334
+ btn_submit.click(submit_message, [input_message, state], [input_message, chatbot])
335
+ input_message.submit(submit_message, [input_message, state], [input_message, chatbot])
336
+ btn_submit_debate.click(submit_message_debate, [input_message_debate, state], [input_message_debate, chatbot])
337
+ input_message_debate.submit(submit_message_debate, [input_message_debate, state], [input_message_debate, chatbot])
338
+ btn_clear_conversation.click(clear_conversation, [], [input_message, input_message_debate, video_inp, chatbot, vlog_outp, state])
339
+ vlog_btn.click(vlog_fn, [video_inp], [vlog_outp])
340
+ vidsub_btn.click(subvid_fn, [video_id], [video_inp])
341
+
342
+ # 第三个标签页
343
+ with gr.TabItem("第三步(再次测试)"):
344
+ gr.Markdown("## Survey: Please answer the following questions")
345
+
346
+ # 问题1
347
+ question1 = gr.Radio(
348
+ choices=["1", "2", "3", "4", "5"],
349
+ label="1. What is your favorite color?",
350
+ )
351
+
352
+ # 问题2
353
+ question2 = gr.Radio(
354
+ choices=["1", "2", "3", "4", "5"],
355
+ label="2. What is your preferred mode of transport?",
356
+ )
357
+
358
+ # 问题3
359
+ question3 = gr.Radio(
360
+ choices=["1", "2", "3", "4", "5"],
361
+ label="3. Which type of cuisine do you prefer?",
362
+ )
363
+
364
+ # 问题4
365
+ question4 = gr.Radio(
366
+ choices=["1", "2", "3", "4", "5"],
367
+ label="4. What is your favorite color?",
368
+ )
369
+
370
+ # 问题5
371
+ question5 = gr.Radio(
372
+ choices=["1", "2", "3", "4", "5"],
373
+ label="5. What is your preferred mode of transport?",
374
+ )
375
+
376
+ # 问题6
377
+ question6 = gr.Radio(
378
+ choices=["1", "2", "3", "4", "5"],
379
+ label="6. Which type of cuisine do you prefer?",
380
+ )
381
+
382
+ # 问题7
383
+ question7 = gr.Radio(
384
+ choices=["1", "2", "3", "4", "5"],
385
+ label="7. Which type of cuisine do you prefer?",
386
+ )
387
+
388
+ # 问题8
389
+ question8 = gr.Radio(
390
+ choices=["1", "2", "3", "4", "5"],
391
+ label="8. What is your favorite color?",
392
+ )
393
+
394
+ # 问题9
395
+ question9 = gr.Radio(
396
+ choices=["1", "2", "3", "4", "5"],
397
+ label="9. What is your preferred mode of transport?",
398
+ )
399
+
400
+ # 问题10
401
+ question10 = gr.Radio(
402
+ choices=["1", "2", "3", "4", "5"],
403
+ label="10. Which type of cuisine do you prefer?",
404
+ )
405
+
406
+ # 提交按钮
407
+ submit_button = gr.Button("Submit Answers")
408
+
409
+ # 显示结果
410
+ output = gr.Textbox(label="Message")
411
+
412
+ # 点击提交按钮时,调用submit_answers函数
413
+ submit_button.click(
414
+ submit_answers_posttest,
415
+ inputs=[question1, question2, question3, question4, question5, question6, question7, question8, question9, question10],
416
+ outputs=output
417
+ )
418
+
419
+ demo.load(queue=False)
420
+
421
+
422
+ demo.queue(concurrency_count=10)
423
 
424
  if __name__ == "__main__":
425
+ demo.launch(share=True)