Greff3 commited on
Commit
011e430
·
verified ·
1 Parent(s): 68669c2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +4 -502
app.py CHANGED
@@ -1,12 +1,9 @@
1
 
2
- import os
3
- import uuid
4
-
5
- import gradio as gr
6
- import modelscope_studio.components.antd as antd
7
- import modelscope_studio.components.antdx as antdx
8
- import modelscope_studio.components.base as ms
9
  from openai import OpenAI
 
 
10
  from dotenv import load_dotenv, dotenv_values
11
  load_dotenv()
12
 
@@ -121,498 +118,3 @@ if prompt := st.chat_input(f"Hi. I'm {selected_model}. How can I help you today?
121
 
122
 
123
  st.session_state.messages.append({"role": "assistant", "content": response})
124
- css = """
125
- .gradio-container {
126
- padding: 0 !important;
127
- }
128
- .gradio-container > main.fillable {
129
- padding: 0 !important;
130
- }
131
- #chatbot {
132
- height: calc(100vh - 21px - 16px);
133
- }
134
- #chatbot .chatbot-conversations {
135
- height: 100%;
136
- background-color: var(--ms-gr-ant-color-bg-layout);
137
- }
138
- #chatbot .chatbot-conversations .chatbot-conversations-list {
139
- padding-left: 0;
140
- padding-right: 0;
141
- }
142
- #chatbot .chatbot-chat {
143
- padding: 32px;
144
- height: 100%;
145
- }
146
- @media (max-width: 768px) {
147
- #chatbot .chatbot-chat {
148
- padding: 0;
149
- }
150
- }
151
- #chatbot .chatbot-chat .chatbot-chat-messages {
152
- flex: 1;
153
- }
154
- #chatbot .chatbot-chat .chatbot-chat-messages .chatbot-chat-message .chatbot-chat-message-footer {
155
- visibility: hidden;
156
- opacity: 0;
157
- transition: opacity 0.2s;
158
- }
159
- #chatbot .chatbot-chat .chatbot-chat-messages .chatbot-chat-message:last-child .chatbot-chat-message-footer {
160
- visibility: visible;
161
- opacity: 1;
162
- }
163
- #chatbot .chatbot-chat .chatbot-chat-messages .chatbot-chat-message:hover .chatbot-chat-message-footer {
164
- visibility: visible;
165
- opacity: 1;
166
- }
167
- """
168
-
169
-
170
- def logo():
171
- with antd.Typography.Title(level=1,
172
- elem_style=dict(fontSize=24,
173
- padding=8,
174
- margin=0)):
175
- with antd.Flex(align="center", gap="small", justify="center"):
176
- antd.Image(qwen_logo,
177
- preview=False,
178
- alt="logo",
179
- width=24,
180
- height=24)
181
- ms.Span("QwQ-32B")
182
-
183
-
184
- with gr.Blocks(css=css, fill_width=True) as demo:
185
- state = gr.State({
186
- "conversations_history": {},
187
- "conversations": [],
188
- "conversation_id": "",
189
- "editing_message_index": -1,
190
- })
191
-
192
- with ms.Application(), antdx.XProvider(
193
- theme=DEFAULT_THEME, locale=DEFAULT_LOCALE), ms.AutoLoading():
194
- with antd.Row(gutter=[20, 20], wrap=False, elem_id="chatbot"):
195
- # Left Column
196
- with antd.Col(md=dict(flex="0 0 260px", span=24, order=0),
197
- span=0,
198
- order=1,
199
- elem_classes="chatbot-conversations"):
200
- with antd.Flex(vertical=True,
201
- gap="small",
202
- elem_style=dict(height="100%")):
203
- # Logo
204
- logo()
205
-
206
- # New Conversation Button
207
- with antd.Button(value=None,
208
- color="primary",
209
- variant="filled",
210
- block=True) as add_conversation_btn:
211
- ms.Text(get_text("New Conversation", "新建对话"))
212
- with ms.Slot("icon"):
213
- antd.Icon("PlusOutlined")
214
-
215
- # Conversations List
216
- with antdx.Conversations(
217
- elem_classes="chatbot-conversations-list",
218
- ) as conversations:
219
- with ms.Slot('menu.items'):
220
- with antd.Menu.Item(
221
- label="Delete", key="delete", danger=True
222
- ) as conversation_delete_menu_item:
223
- with ms.Slot("icon"):
224
- antd.Icon("DeleteOutlined")
225
- # Right Column
226
- with antd.Col(flex=1, elem_style=dict(height="100%")):
227
- with antd.Flex(vertical=True,
228
- gap="middle",
229
- elem_classes="chatbot-chat"):
230
- # Chatbot
231
- with antdx.Bubble.List(
232
- items=DEFAULT_CONVERSATIONS_HISTORY,
233
- elem_classes="chatbot-chat-messages") as chatbot:
234
- # Define Chatbot Roles
235
- with ms.Slot("roles"):
236
- # Placeholder Role
237
- with antdx.Bubble.List.Role(
238
- role="placeholder",
239
- styles=dict(content=dict(width="100%")),
240
- variant="borderless"):
241
- with ms.Slot("messageRender"):
242
- with antd.Space(
243
- direction="vertical",
244
- size=16,
245
- elem_style=dict(width="100%")):
246
- with antdx.Welcome(
247
- styles=dict(icon=dict(
248
- flexShrink=0)),
249
- variant="borderless",
250
- title=get_text(
251
- "Hello, I'm QwQ-32B",
252
- "你好,我是 QwQ-32B"),
253
- description=get_text(
254
- "You can type text to get started.",
255
- "你可以输入文本开始对话。"),
256
- ):
257
- with ms.Slot("icon"):
258
- antd.Image(qwen_logo,
259
- preview=False)
260
- with antdx.Prompts(title=get_text(
261
- "How can I help you today?",
262
- "有什么我能帮助你的吗?"),
263
- styles={
264
- "list": {
265
- "width":
266
- '100%',
267
- },
268
- "item": {
269
- "flex": 1,
270
- },
271
- }) as prompts:
272
- for item in DEFAULT_PROMPTS:
273
- with antdx.Prompts.Item(
274
- label=item["category"]
275
- ):
276
- for prompt in item[
277
- "prompts"]:
278
- antdx.Prompts.Item(
279
- description=prompt,
280
- )
281
-
282
- # User Role
283
- with antdx.Bubble.List.Role(
284
- role="user",
285
- placement="end",
286
- elem_classes="chatbot-chat-message",
287
- class_names=dict(
288
- footer="chatbot-chat-message-footer"),
289
- styles=dict(content=dict(
290
- maxWidth="100%",
291
- overflow='auto',
292
- ))):
293
- with ms.Slot(
294
- "messageRender",
295
- params_mapping="(content) => content"):
296
-
297
- ms.Markdown()
298
- with ms.Slot("footer",
299
- params_mapping="""(bubble) => {
300
- return {
301
- copy_btn: {
302
- copyable: { text: typeof bubble.content === 'string' ? bubble.content : bubble.content?.text, tooltips: false },
303
- },
304
- edit_btn: { conversationKey: bubble.key, disabled: bubble.meta.disabled },
305
- delete_btn: { conversationKey: bubble.key, disabled: bubble.meta.disabled },
306
- };
307
- }"""):
308
- with antd.Typography.Text(
309
- copyable=dict(tooltips=False),
310
- as_item="copy_btn"):
311
- with ms.Slot("copyable.icon"):
312
- with antd.Button(value=None,
313
- size="small",
314
- color="default",
315
- variant="text"):
316
- with ms.Slot("icon"):
317
- antd.Icon("CopyOutlined")
318
- with antd.Button(value=None,
319
- size="small",
320
- color="default",
321
- variant="text"):
322
- with ms.Slot("icon"):
323
- antd.Icon("CheckOutlined")
324
- with antd.Button(value=None,
325
- size="small",
326
- color="default",
327
- variant="text",
328
- as_item="edit_btn"
329
- ) as user_edit_btn:
330
- with ms.Slot("icon"):
331
- antd.Icon("EditOutlined")
332
- with antd.Popconfirm(
333
- title="Delete the message",
334
- description=
335
- "Are you sure to delete this message?",
336
- ok_button_props=dict(danger=True),
337
- as_item="delete_btn"
338
- ) as user_delete_popconfirm:
339
- with antd.Button(value=None,
340
- size="small",
341
- color="default",
342
- variant="text",
343
- as_item="delete_btn"):
344
- with ms.Slot("icon"):
345
- antd.Icon("DeleteOutlined")
346
-
347
- # Chatbot Role
348
- with antdx.Bubble.List.Role(
349
- role="assistant",
350
- placement="start",
351
- elem_classes="chatbot-chat-message",
352
- class_names=dict(
353
- footer="chatbot-chat-message-footer"),
354
- styles=dict(content=dict(
355
- maxWidth="100%", overflow='auto'))):
356
- with ms.Slot("avatar"):
357
- antd.Avatar(
358
- os.path.join(os.path.dirname(__file__),
359
- "qwen.png"))
360
- with ms.Slot(
361
- "messageRender",
362
- params_mapping="""(content, bubble) => {
363
- const reason_content = bubble?.meta?.reason_content
364
- const has_error = bubble?.meta?.error
365
- return {
366
- reasoning: reason_content || content,
367
- reasoning_container: has_error ? { style: { display: 'none' } } : undefined,
368
- answer: {
369
- value: reason_content || has_error ? content : undefined
370
- },
371
- collapse_label: bubble.meta?.thought_end_message,
372
- collapse_progress: bubble.meta?.thought_end_message ? { style: { display: 'none' } } : undefined,
373
- canceled: bubble.meta?.canceled ? undefined : { style: { display: 'none' } }
374
- }
375
- }"""):
376
- with antd.Flex(vertical=True,
377
- gap="middle"):
378
- with antd.Collapse(
379
- default_active_key=[
380
- "reasoning"
381
- ],
382
- as_item="reasoning_container"):
383
- with antd.Collapse.Item(
384
- key="reasoning"):
385
- with ms.Slot("label"):
386
- with antd.Space(
387
- size="middle"):
388
- ms.Span(
389
- get_text(
390
- "Thinking...",
391
- "思考中..."),
392
- as_item=
393
- "collapse_label")
394
- antd.Progress(
395
- percent="100",
396
- status="active",
397
- elem_style=dict(
398
- display="flex",
399
- alignItems=
400
- "center",
401
- ),
402
- show_info=False,
403
- size=[110, 5],
404
- as_item=
405
- "collapse_progress"
406
- )
407
- with antd.Alert(
408
- type="warning"):
409
- with ms.Slot(
410
- "description"):
411
- ms.Markdown(
412
- as_item="reasoning"
413
- )
414
- ms.Markdown(
415
- as_item="answer",
416
- elem_classes="answer-content")
417
-
418
- antd.Divider(as_item="canceled")
419
- antd.Typography.Text(get_text(
420
- "Chat completion paused.", "聊天已暂停。"),
421
- as_item="canceled",
422
- type="warning")
423
-
424
- with ms.Slot("footer",
425
- params_mapping="""(bubble) => {
426
- if (bubble?.meta?.end) {
427
- return {
428
- copy_btn: {
429
- copyable: { text: bubble.content, tooltips: false },
430
- },
431
- regenerate_btn: { conversationKey: bubble.key, disabled: bubble.meta.disabled },
432
- delete_btn: { conversationKey: bubble.key, disabled: bubble.meta.disabled },
433
- edit_btn: { conversationKey: bubble.key, disabled: bubble.meta.disabled },
434
- };
435
- }
436
- return { actions_container: { style: { display: 'none' } } };
437
- }"""):
438
- with ms.Div(as_item="actions_container"):
439
- with antd.Typography.Text(
440
- copyable=dict(tooltips=False),
441
- as_item="copy_btn"):
442
- with ms.Slot("copyable.icon"):
443
- with antd.Button(
444
- value=None,
445
- size="small",
446
- color="default",
447
- variant="text"):
448
- with ms.Slot("icon"):
449
- antd.Icon(
450
- "CopyOutlined")
451
- with antd.Button(
452
- value=None,
453
- size="small",
454
- color="default",
455
- variant="text"):
456
- with ms.Slot("icon"):
457
- antd.Icon(
458
- "CheckOutlined")
459
-
460
- with antd.Popconfirm(
461
- title=get_text(
462
- "Regenerate the message",
463
- "重新生成消息"),
464
- description=get_text(
465
- "Regenerate the message will also delete all subsequent messages.",
466
- "重新生成消息将会删除所有的后续消息。"),
467
- ok_button_props=dict(
468
- danger=True),
469
- as_item="regenerate_btn"
470
- ) as chatbot_regenerate_popconfirm:
471
- with antd.Button(
472
- value=None,
473
- size="small",
474
- color="default",
475
- variant="text",
476
- as_item="regenerate_btn",
477
- ):
478
- with ms.Slot("icon"):
479
- antd.Icon("SyncOutlined")
480
- with antd.Button(value=None,
481
- size="small",
482
- color="default",
483
- variant="text",
484
- as_item="edit_btn"
485
- ) as chatbot_edit_btn:
486
- with ms.Slot("icon"):
487
- antd.Icon("EditOutlined")
488
- with antd.Popconfirm(
489
- title=get_text("Delete the message", "删除消息"),
490
- description=get_text(
491
- "Are you sure to delete this message?",
492
- "确定要删除这条消息吗?"),
493
- ok_button_props=dict(
494
- danger=True),
495
- as_item="delete_btn"
496
- ) as chatbot_delete_popconfirm:
497
- with antd.Button(
498
- value=None,
499
- size="small",
500
- color="default",
501
- variant="text",
502
- as_item="delete_btn"):
503
- with ms.Slot("icon"):
504
- antd.Icon("DeleteOutlined")
505
-
506
- # Sender
507
- with antdx.Suggestion(
508
- items=DEFAULT_SUGGESTIONS,
509
- # onKeyDown Handler in Javascript
510
- should_trigger="""(e, { onTrigger, onKeyDown }) => {
511
- switch(e.key) {
512
- case '/':
513
- onTrigger()
514
- break
515
- case 'ArrowRight':
516
- case 'ArrowLeft':
517
- case 'ArrowUp':
518
- case 'ArrowDown':
519
- break;
520
- default:
521
- onTrigger(false)
522
- }
523
- onKeyDown(e)
524
- }""") as suggestion:
525
- with ms.Slot("children"):
526
- with antdx.Sender(placeholder=get_text(
527
- "Enter / to get suggestions",
528
- "输入 / 获取建议"), ) as sender:
529
- with ms.Slot("prefix"):
530
- # Clear Button
531
- with antd.Tooltip(title=get_text(
532
- "Clear Conversation History",
533
- "清空对话历史"), ):
534
- with antd.Button(
535
- value=None,
536
- type="text") as clear_btn:
537
- with ms.Slot("icon"):
538
- antd.Icon("ClearOutlined")
539
-
540
- # Modals
541
- with antd.Modal(title=get_text("Edit Message", "编辑消息"),
542
- open=False,
543
- centered=True,
544
- width="60%") as edit_modal:
545
- edit_textarea = antd.Input.Textarea(auto_size=dict(minRows=2,
546
- maxRows=6),
547
- elem_style=dict(width="100%"))
548
- # Events Handler
549
- if save_history:
550
- browser_state = gr.BrowserState(
551
- {
552
- "conversations_history": {},
553
- "conversations": [],
554
- },
555
- storage_key="qwen_qwq_chatbot_storage")
556
- state.change(fn=Gradio_Events.update_browser_state,
557
- inputs=[state],
558
- outputs=[browser_state])
559
-
560
- demo.load(fn=Gradio_Events.apply_browser_state,
561
- inputs=[browser_state, state],
562
- outputs=[conversations, state])
563
-
564
- add_conversation_btn.click(fn=Gradio_Events.new_chat,
565
- inputs=[state],
566
- outputs=[conversations, chatbot, state])
567
- conversations.active_change(fn=Gradio_Events.select_conversation,
568
- inputs=[state],
569
- outputs=[conversations, chatbot, state])
570
- conversations.menu_click(fn=Gradio_Events.click_conversation_menu,
571
- inputs=[state],
572
- outputs=[conversations, chatbot, state])
573
- prompts.item_click(fn=Gradio_Events.apply_prompt, outputs=[sender])
574
-
575
- clear_btn.click(fn=Gradio_Events.clear_conversation_history,
576
- inputs=[state],
577
- outputs=[chatbot, state])
578
-
579
- suggestion.select(fn=Gradio_Events.select_suggestion,
580
- inputs=[sender],
581
- outputs=[sender])
582
-
583
- gr.on(triggers=[user_edit_btn.click, chatbot_edit_btn.click],
584
- fn=Gradio_Events.edit_message,
585
- inputs=[state],
586
- outputs=[edit_textarea, state]).then(fn=Gradio_Events.open_modal,
587
- outputs=[edit_modal])
588
- edit_modal.ok(fn=Gradio_Events.confirm_edit_message,
589
- inputs=[edit_textarea, state],
590
- outputs=[chatbot, state]).then(fn=Gradio_Events.close_modal,
591
- outputs=[edit_modal])
592
- edit_modal.cancel(fn=Gradio_Events.close_modal, outputs=[edit_modal])
593
- gr.on(triggers=[
594
- chatbot_delete_popconfirm.confirm, user_delete_popconfirm.confirm
595
- ],
596
- fn=Gradio_Events.delete_message,
597
- inputs=[state],
598
- outputs=[chatbot, state])
599
-
600
- regenerating_event = chatbot_regenerate_popconfirm.confirm(
601
- fn=Gradio_Events.regenerate_message,
602
- inputs=[state],
603
- outputs=[sender, clear_btn, conversation_delete_menu_item, add_conversation_btn, conversations, chatbot, state])
604
-
605
- submit_event = sender.submit(fn=Gradio_Events.submit,
606
- inputs=[sender, state],
607
- outputs=[sender, clear_btn, conversation_delete_menu_item,
608
- add_conversation_btn, conversations,chatbot, state])
609
- sender.cancel(fn=None, cancels=[submit_event, regenerating_event])
610
- sender.cancel(fn=Gradio_Events.cancel,
611
- inputs=[state],
612
- outputs=[
613
- sender, conversation_delete_menu_item, clear_btn,
614
- conversations, add_conversation_btn, chatbot, state
615
- ])
616
-
617
- if __name__ == "__main__":
618
- demo.queue(default_concurrency_limit=200).launch(ssr_mode=False, max_threads=200)
 
1
 
2
+ import numpy as np
3
+ import streamlit as st
 
 
 
 
 
4
  from openai import OpenAI
5
+ import os
6
+ import sys
7
  from dotenv import load_dotenv, dotenv_values
8
  load_dotenv()
9
 
 
118
 
119
 
120
  st.session_state.messages.append({"role": "assistant", "content": response})