enotkrutoy commited on
Commit
050cc5a
·
verified ·
1 Parent(s): d713635

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +60 -46
app.py CHANGED
@@ -1,33 +1,30 @@
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
 
@@ -49,8 +46,7 @@ def messages_to_history(messages: Messages) -> Tuple[str, History]:
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:
@@ -58,51 +54,51 @@ def model_chat(query: Optional[str], history: Optional[History], system: str, ra
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():
@@ -134,7 +130,7 @@ 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
 
@@ -143,4 +139,22 @@ def main():
143
 
144
 
145
  if __name__ == "__main__":
146
- main()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #### Конечный:
 
2
  import os
 
 
 
 
 
3
  from http import HTTPStatus
4
+ import gradio as gr
5
+ from dashscope import Generation, Role
 
6
  from typing import List, Optional, Tuple, Dict
7
  from urllib.error import HTTPError
8
+ import unittest
9
 
10
  default_system = 'You are Qwen, created by Alibaba Cloud. You are a helpful assistant.'
11
 
12
+ GROQ_API_KEY = os.environ.get('GROQ_API_KEY')
13
+ client = OpenAI(
14
+ api_key=GROQ_API_KEY,
15
+ base_url="https://api.groq.com/openai/v1",
16
+ )
17
 
18
  History = List[Tuple[str, str]]
19
  Messages = List[Dict[str, str]]
20
 
21
 
22
+ def clear_session() -> Tuple[str, History]:
23
  return '', []
24
 
25
 
26
+ def modify_system_session(system: str) -> Tuple[str, str, History]:
27
+ if not system:
28
  system = default_system
29
  return system, system, []
30
 
 
46
  return system, history
47
 
48
 
49
+ def model_chat(query: Optional[str], history: Optional[History], system: str, radio: str) -> Tuple[str, str, History]:
 
50
  if query is None:
51
  query = ''
52
  if history is None:
 
54
  messages = history_to_messages(history, system)
55
  messages.append({'role': Role.USER, 'content': query})
56
  label_model = f"qwen2.5-coder-{radio.lower()}-instruct"
57
+ try:
58
+ gen = Generation.call(
59
+ model=label_model,
60
+ messages=messages,
61
+ result_format='message',
62
+ stream=True
63
+ )
64
+ for response in gen:
65
+ if response.status_code == HTTPStatus.OK:
66
+ role = response.output.choices[0].message.role
67
+ response = response.output.choices[0].message.content
68
+ system, history = messages_to_history(messages + [{'role': role, 'content': response}])
69
+ yield '', history, system
70
+ else:
71
+ raise HTTPError(code=response.status_code, msg='Request failed with status code: %s' % response.status_code)
72
+ except HTTPError as e:
73
+ print(f"HTTP error occurred: {e}")
74
+ except Exception as e:
75
+ print(f"An error occurred: {e}")
76
+
77
+
78
+ def choose_radio(radio, system):
79
  mark_ = gr.Markdown(value=f"<center><font size=8>Qwen2.5-Coder-{radio}-instruct👾</center>")
80
  chatbot = gr.Chatbot(label=f'Qwen2.5-Coder-{radio.lower()}-instruct')
81
 
82
+ if not system:
83
  system = default_system
84
 
85
  return mark_, chatbot, system, system, ""
86
 
87
 
88
  def update_other_radios(value, other_radio1, other_radio2):
89
+ if not value:
90
+ selected = other_radio1 or other_radio2
 
 
 
91
  return selected, other_radio1, other_radio2
92
  return value, "", ""
93
 
94
 
95
  def main():
96
+ # Создание интерфейса Gradio
97
  with gr.Blocks() as demo:
98
  with gr.Row():
99
  options_coder = ["0.5B", "1.5B", "3B", "7B", "14B", "32B", ]
100
  with gr.Row():
101
+ radio = gr.Radio(choices=options_coder, label="Qwen2.5-Coder:", value="32B")
102
 
103
  with gr.Row():
104
  with gr.Accordion():
 
130
  inputs=[system_input],
131
  outputs=[system_state, system_input, chatbot])
132
 
133
+ radio.change(choose_radio,
134
  inputs=[radio, system_input],
135
  outputs=[mark_, chatbot, system_state, system_input, textbox])
136
 
 
139
 
140
 
141
  if __name__ == "__main__":
142
+ main()
143
+
144
+ # Тесты
145
+ class TestApp(unittest.TestCase):
146
+ def test_clear_session(self):
147
+ self.assertEqual(clear_session(), ('', []))
148
+
149
+ def test_modify_system_session(self):
150
+ self.assertEqual(modify_system_session(None), (default_system, default_system, []))
151
+ self.assertEqual(modify_system_session(""), (default_system, default_system, []))
152
+ self.assertEqual(modify_system_session("Custom System"), ("Custom System", "Custom System", []))
153
+
154
+ def test_update_other_radios(self):
155
+ self.assertEqual(update_other_radios("", "32B", ""), ("32B", "32B", ""))
156
+ self.assertEqual(update_other_radios("", "", "14B"), ("14B", "", "14B"))
157
+ self.assertEqual(update_other_radios("7B", "", ""), ("7B", "", ""))
158
+
159
+ if __name__ == "__main__":
160
+ unittest.main()