Files changed (1) hide show
  1. app.py +55 -59
app.py CHANGED
@@ -1,109 +1,106 @@
1
  # app.py
2
 
3
  import os
 
4
  from http.client import HTTPMessage
5
-
6
- os.system('pip install dashscope')
7
-
8
- import gradio as gr
9
  from http import HTTPStatus
10
- import dashscope
11
- from dashscope import Generation
12
- from dashscope.api_entities.dashscope_response import Role
13
  from typing import List, Optional, Tuple, Dict
14
  from urllib.error import HTTPError
15
 
16
- default_system = 'You are Qwen, created by Alibaba Cloud. You are a helpful assistant.'
 
17
 
 
18
  YOUR_API_TOKEN = os.getenv('YOUR_API_TOKEN')
 
 
 
 
19
  dashscope.api_key = YOUR_API_TOKEN
20
 
21
  History = List[Tuple[str, str]]
22
  Messages = List[Dict[str, str]]
23
 
 
24
 
25
  def clear_session() -> History:
26
  return '', []
27
 
28
-
29
- def modify_system_session(system: str) -> str:
30
- if system is None or len(system) == 0:
31
  system = default_system
32
  return system, system, []
33
 
34
-
35
  def history_to_messages(history: History, system: str) -> Messages:
36
  messages = [{'role': Role.SYSTEM, 'content': system}]
37
- for h in history:
38
- messages.append({'role': Role.USER, 'content': h[0]})
39
- messages.append({'role': Role.ASSISTANT, 'content': h[1]})
40
  return messages
41
 
42
-
43
  def messages_to_history(messages: Messages) -> Tuple[str, History]:
44
  assert messages[0]['role'] == Role.SYSTEM
45
  system = messages[0]['content']
46
- history = []
47
- for q, r in zip(messages[1::2], messages[2::2]):
48
- history.append([q['content'], r['content']])
49
  return system, history
50
 
51
-
52
- def model_chat(query: Optional[str], history: Optional[History], system: str, radio: str
53
- ) -> Tuple[str, str, History]:
54
- if query is None:
55
  query = ''
56
- if history is None:
57
  history = []
58
  messages = history_to_messages(history, system)
59
  messages.append({'role': Role.USER, 'content': query})
60
  label_model = f"qwen2.5-coder-{radio.lower()}-instruct"
61
- gen = Generation.call(
62
- model=label_model,
63
- messages=messages,
64
- result_format='message',
65
- stream=True
66
- )
67
- for response in gen:
68
- if response.status_code == HTTPStatus.OK:
69
- role = response.output.choices[0].message.role
70
- response = response.output.choices[0].message.content
71
- system, history = messages_to_history(messages + [{'role': role, 'content': response}])
72
- yield '', history, system
73
- else:
74
- raise HTTPError(code=404, msg='Request id: %s, Status code: %s, error code: %s, error message: %s' % (
75
- response.request_id, response.status_code,
76
- response.code, response.message), hdrs=HTTPMessage(), url='http://example.com', fp=None)
77
-
78
-
79
- def chiose_radio(radio, system):
 
 
 
 
80
  mark_ = gr.Markdown(value=f"<center><font size=8>Qwen2.5-Coder-{radio}-instruct👾</center>")
81
  chatbot = gr.Chatbot(label=f'Qwen2.5-Coder-{radio.lower()}-instruct')
82
-
83
- if system is None or len(system) == 0:
84
  system = default_system
85
-
86
  return mark_, chatbot, system, system, ""
87
 
88
-
89
  def update_other_radios(value, other_radio1, other_radio2):
90
- if value == "":
91
- if other_radio1 != "":
92
  selected = other_radio1
93
  else:
94
  selected = other_radio2
95
  return selected, other_radio1, other_radio2
96
  return value, "", ""
97
 
98
-
99
  def main():
100
- # 创建两个标签
101
  with gr.Blocks() as demo:
102
  with gr.Row():
103
- options_coder = ["0.5B", "1.5B", "3B", "7B", "14B", "32B", ]
104
  with gr.Row():
105
- radio = gr.Radio(choices=options_coder, label="Qwen2.5-Coder", value="32B")
106
-
107
  with gr.Row():
108
  with gr.Accordion():
109
  mark_ = gr.Markdown("""<center><font size=8>Qwen2.5-Coder-32B-Instruct Bot👾</center>""")
@@ -118,12 +115,12 @@ def main():
118
 
119
  with gr.Row():
120
  clear_history = gr.Button("🧹 Clear History")
121
- sumbit = gr.Button("🚀 Send")
122
 
123
  textbox.submit(model_chat,
124
  inputs=[textbox, chatbot, system_state, radio],
125
  outputs=[textbox, chatbot, system_input])
126
- sumbit.click(model_chat,
127
  inputs=[textbox, chatbot, system_state, radio],
128
  outputs=[textbox, chatbot, system_input],
129
  concurrency_limit=100)
@@ -134,13 +131,12 @@ def main():
134
  inputs=[system_input],
135
  outputs=[system_state, system_input, chatbot])
136
 
137
- radio.change(chiose_radio,
138
  inputs=[radio, system_input],
139
  outputs=[mark_, chatbot, system_state, system_input, textbox])
140
 
141
  demo.queue(api_open=False, default_concurrency_limit=40)
142
- demo.launch(max_threads=5)
143
-
144
 
145
  if __name__ == "__main__":
146
  main()
 
1
  # app.py
2
 
3
  import os
4
+ import logging
5
  from http.client import HTTPMessage
 
 
 
 
6
  from http import HTTPStatus
7
+ import gradio as gr
8
+ from dashscope import Generation, Role
9
+ from dashscope.api_entities.dashscope_response import DashScopeResponse
10
  from typing import List, Optional, Tuple, Dict
11
  from urllib.error import HTTPError
12
 
13
+ # Configure logging
14
+ logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
15
 
16
+ # Environment variable for API token
17
  YOUR_API_TOKEN = os.getenv('YOUR_API_TOKEN')
18
+ if not YOUR_API_TOKEN:
19
+ logging.error("API token not found in environment variables.")
20
+ exit(1)
21
+
22
  dashscope.api_key = YOUR_API_TOKEN
23
 
24
  History = List[Tuple[str, str]]
25
  Messages = List[Dict[str, str]]
26
 
27
+ default_system = 'You are Qwen, created by Alibaba Cloud. You are a helpful assistant.'
28
 
29
  def clear_session() -> History:
30
  return '', []
31
 
32
+ def modify_system_session(system: str) -> Tuple[str, str, History]:
33
+ if not system:
 
34
  system = default_system
35
  return system, system, []
36
 
 
37
  def history_to_messages(history: History, system: str) -> Messages:
38
  messages = [{'role': Role.SYSTEM, 'content': system}]
39
+ for user_msg, assistant_msg in history:
40
+ messages.append({'role': Role.USER, 'content': user_msg})
41
+ messages.append({'role': Role.ASSISTANT, 'content': assistant_msg})
42
  return messages
43
 
 
44
  def messages_to_history(messages: Messages) -> Tuple[str, History]:
45
  assert messages[0]['role'] == Role.SYSTEM
46
  system = messages[0]['content']
47
+ history = [(msg['content'], next_msg['content']) for msg, next_msg in zip(messages[1::2], messages[2::2])]
 
 
48
  return system, history
49
 
50
+ def model_chat(query: Optional[str], history: Optional[History], system: str, radio: str) -> Tuple[str, str, History]:
51
+ if not query:
 
 
52
  query = ''
53
+ if not history:
54
  history = []
55
  messages = history_to_messages(history, system)
56
  messages.append({'role': Role.USER, 'content': query})
57
  label_model = f"qwen2.5-coder-{radio.lower()}-instruct"
58
+ try:
59
+ gen = Generation.call(
60
+ model=label_model,
61
+ messages=messages,
62
+ result_format='message',
63
+ stream=True
64
+ )
65
+ for response in gen:
66
+ if response.status_code == HTTPStatus.OK:
67
+ role = response.output.choices[0].message.role
68
+ content = response.output.choices[0].message.content
69
+ system, history = messages_to_history(messages + [{'role': role, 'content': content}])
70
+ yield '', history, system
71
+ else:
72
+ raise HTTPError(code=response.status_code, msg=f'Request ID: {response.request_id}, Status Code: {response.status_code}, Error Code: {response.code}, Error Message: {response.message}', hdrs=HTTPMessage(), url='http://example.com', fp=None)
73
+ except HTTPError as e:
74
+ logging.error(f"HTTP Error: {e}")
75
+ yield '', history, system
76
+ except Exception as e:
77
+ logging.error(f"Unexpected error: {e}")
78
+ yield '', history, system
79
+
80
+ def choose_radio(radio, system):
81
  mark_ = gr.Markdown(value=f"<center><font size=8>Qwen2.5-Coder-{radio}-instruct👾</center>")
82
  chatbot = gr.Chatbot(label=f'Qwen2.5-Coder-{radio.lower()}-instruct')
83
+ if not system:
 
84
  system = default_system
 
85
  return mark_, chatbot, system, system, ""
86
 
 
87
  def update_other_radios(value, other_radio1, other_radio2):
88
+ if not value:
89
+ if other_radio1:
90
  selected = other_radio1
91
  else:
92
  selected = other_radio2
93
  return selected, other_radio1, other_radio2
94
  return value, "", ""
95
 
 
96
  def main():
97
+ # Create two tabs
98
  with gr.Blocks() as demo:
99
  with gr.Row():
100
+ options_coder = ["0.5B", "1.5B", "3B", "7B", "14B", "32B"]
101
  with gr.Row():
102
+ radio = gr.Radio(choices=options_coder, label="Qwen2.5-Coder:", value="32B")
103
+
104
  with gr.Row():
105
  with gr.Accordion():
106
  mark_ = gr.Markdown("""<center><font size=8>Qwen2.5-Coder-32B-Instruct Bot👾</center>""")
 
115
 
116
  with gr.Row():
117
  clear_history = gr.Button("🧹 Clear History")
118
+ submit = gr.Button("🚀 Send")
119
 
120
  textbox.submit(model_chat,
121
  inputs=[textbox, chatbot, system_state, radio],
122
  outputs=[textbox, chatbot, system_input])
123
+ submit.click(model_chat,
124
  inputs=[textbox, chatbot, system_state, radio],
125
  outputs=[textbox, chatbot, system_input],
126
  concurrency_limit=100)
 
131
  inputs=[system_input],
132
  outputs=[system_state, system_input, chatbot])
133
 
134
+ radio.change(choose_radio,
135
  inputs=[radio, system_input],
136
  outputs=[mark_, chatbot, system_state, system_input, textbox])
137
 
138
  demo.queue(api_open=False, default_concurrency_limit=40)
139
+ demo.launch(max_threads=5, server_name="0.0.0.0", server_port=7860, share=True)
 
140
 
141
  if __name__ == "__main__":
142
  main()