DHEIVER commited on
Commit
ba02552
·
verified ·
1 Parent(s): 9f98caf

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +8 -15
app.py CHANGED
@@ -1,16 +1,15 @@
1
  import os
2
- os.system('pip install dashscope')
3
  import gradio as gr
4
  from http import HTTPStatus
5
  import dashscope
6
  from dashscope import Generation
7
  from dashscope.api_entities.dashscope_response import Role
8
  from typing import List, Optional, Tuple, Dict
9
- from urllib.error import HTTPError
10
  default_system = 'You are a helpful assistant.'
11
 
12
- YOUR_API_TOKEN = os.getenv('YOUR_API_TOKEN')
13
- dashscope.api_key = YOUR_API_TOKEN
14
 
15
  History = List[Tuple[str, str]]
16
  Messages = List[Dict[str, str]]
@@ -30,7 +29,6 @@ def history_to_messages(history: History, system: str) -> Messages:
30
  messages.append({'role': Role.ASSISTANT, 'content': h[1]})
31
  return messages
32
 
33
-
34
  def messages_to_history(messages: Messages) -> Tuple[str, History]:
35
  assert messages[0]['role'] == Role.SYSTEM
36
  system = messages[0]['content']
@@ -39,7 +37,6 @@ def messages_to_history(messages: Messages) -> Tuple[str, History]:
39
  history.append([q['content'], r['content']])
40
  return system, history
41
 
42
-
43
  def model_chat(query: Optional[str], history: Optional[History], system: str
44
  ) -> Tuple[str, str, History]:
45
  if query is None:
@@ -66,10 +63,8 @@ def model_chat(query: Optional[str], history: Optional[History], system: str
66
  response.code, response.message
67
  ))
68
 
69
-
70
  with gr.Blocks() as demo:
71
  gr.Markdown("""<center><font size=8>Qwen2-72B-instruct Chat👾</center>""")
72
-
73
  with gr.Row():
74
  with gr.Column(scale=3):
75
  system_input = gr.Textbox(value=default_system, lines=1, label='System')
@@ -78,7 +73,6 @@ with gr.Blocks() as demo:
78
  system_state = gr.Textbox(value=default_system, visible=False)
79
  chatbot = gr.Chatbot(label='qwen2-72B-instruct')
80
  textbox = gr.Textbox(lines=1, label='Input')
81
-
82
  with gr.Row():
83
  clear_history = gr.Button("🧹 Clear history")
84
  sumbit = gr.Button("🚀 Send")
@@ -86,20 +80,19 @@ with gr.Blocks() as demo:
86
  textbox.submit(model_chat,
87
  inputs=[textbox, chatbot, system_state],
88
  outputs=[textbox, chatbot, system_input],
89
- concurrency_limit = 40)
90
-
91
  sumbit.click(model_chat,
92
  inputs=[textbox, chatbot, system_state],
93
  outputs=[textbox, chatbot, system_input],
94
- concurrency_limit = 40)
95
  clear_history.click(fn=clear_session,
96
  inputs=[],
97
  outputs=[textbox, chatbot],
98
- concurrency_limit = 40)
99
  modify_system.click(fn=modify_system_session,
100
  inputs=[system_input],
101
  outputs=[system_state, system_input, chatbot],
102
- concurrency_limit = 40)
103
 
104
  demo.queue(api_open=False)
105
- demo.launch(max_threads=40)
 
1
  import os
 
2
  import gradio as gr
3
  from http import HTTPStatus
4
  import dashscope
5
  from dashscope import Generation
6
  from dashscope.api_entities.dashscope_response import Role
7
  from typing import List, Optional, Tuple, Dict
8
+
9
  default_system = 'You are a helpful assistant.'
10
 
11
+ # Set the API key from environment
12
+ dashscope.api_key = os.getenv('YOUR_API_TOKEN')
13
 
14
  History = List[Tuple[str, str]]
15
  Messages = List[Dict[str, str]]
 
29
  messages.append({'role': Role.ASSISTANT, 'content': h[1]})
30
  return messages
31
 
 
32
  def messages_to_history(messages: Messages) -> Tuple[str, History]:
33
  assert messages[0]['role'] == Role.SYSTEM
34
  system = messages[0]['content']
 
37
  history.append([q['content'], r['content']])
38
  return system, history
39
 
 
40
  def model_chat(query: Optional[str], history: Optional[History], system: str
41
  ) -> Tuple[str, str, History]:
42
  if query is None:
 
63
  response.code, response.message
64
  ))
65
 
 
66
  with gr.Blocks() as demo:
67
  gr.Markdown("""<center><font size=8>Qwen2-72B-instruct Chat👾</center>""")
 
68
  with gr.Row():
69
  with gr.Column(scale=3):
70
  system_input = gr.Textbox(value=default_system, lines=1, label='System')
 
73
  system_state = gr.Textbox(value=default_system, visible=False)
74
  chatbot = gr.Chatbot(label='qwen2-72B-instruct')
75
  textbox = gr.Textbox(lines=1, label='Input')
 
76
  with gr.Row():
77
  clear_history = gr.Button("🧹 Clear history")
78
  sumbit = gr.Button("🚀 Send")
 
80
  textbox.submit(model_chat,
81
  inputs=[textbox, chatbot, system_state],
82
  outputs=[textbox, chatbot, system_input],
83
+ concurrency_limit=40)
 
84
  sumbit.click(model_chat,
85
  inputs=[textbox, chatbot, system_state],
86
  outputs=[textbox, chatbot, system_input],
87
+ concurrency_limit=40)
88
  clear_history.click(fn=clear_session,
89
  inputs=[],
90
  outputs=[textbox, chatbot],
91
+ concurrency_limit=40)
92
  modify_system.click(fn=modify_system_session,
93
  inputs=[system_input],
94
  outputs=[system_state, system_input, chatbot],
95
+ concurrency_limit=40)
96
 
97
  demo.queue(api_open=False)
98
+ demo.launch(max_threads=40)