Files changed (1) hide show
  1. app.py +99 -47
app.py CHANGED
@@ -7,11 +7,18 @@ 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]]
17
 
@@ -19,18 +26,21 @@ def clear_session() -> History:
19
  return '', []
20
 
21
  def modify_system_session(system: str) -> str:
22
- if system is None or len(system) == 0:
23
  system = default_system
24
  return system, system, []
25
 
26
- def history_to_messages(history: History, system: str) -> Messages:
 
 
 
 
27
  messages = [{'role': Role.SYSTEM, 'content': system}]
28
  for h in history:
29
  messages.append({'role': Role.USER, 'content': h[0]})
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,59 +49,101 @@ 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:
46
  query = ''
47
- if history is None:
48
- history = []
49
- messages = history_to_messages(history, system)
 
50
  messages.append({'role': Role.USER, 'content': query})
51
- gen = Generation.call(
52
- model = "qwen-72b-chat",
53
- messages=messages,
54
- result_format='message',
55
- stream=True
56
- )
57
- for response in gen:
58
- if response.status_code == HTTPStatus.OK:
59
- role = response.output.choices[0].message.role
60
- response = response.output.choices[0].message.content
61
- system, history = messages_to_history(messages + [{'role': role, 'content': response}])
62
- yield '', history, system
63
- else:
64
- raise HTTPError('Request id: %s, Status code: %s, error code: %s, error message: %s' % (
65
- response.request_id, response.status_code,
66
- response.code, response.message
67
- ))
68
-
 
 
 
 
69
 
 
70
  with gr.Blocks() as demo:
71
  gr.Markdown("""<center><font size=8>Qwen-72B-Chat</center>""")
72
- gr.Markdown("""<center><font size=4>Qwen-72B-Chat is the 72-billion parameter chat model of the Qwen series.</center>""")
73
-
 
74
  with gr.Row():
75
  with gr.Column(scale=3):
76
- system_input = gr.Textbox(value=default_system, lines=1, label='System')
 
 
 
 
 
77
  with gr.Column(scale=1):
78
- modify_system = gr.Button("🛠️ Set system prompt and clear history.", scale=2)
79
  system_state = gr.Textbox(value=default_system, visible=False)
80
- chatbot = gr.Chatbot(label='Qwen-72B-Chat')
81
- textbox = gr.Textbox(lines=2, label='Input')
82
-
 
 
 
 
 
 
 
 
 
 
 
 
83
  with gr.Row():
84
- clear_history = gr.Button("🧹 Clear history")
85
- sumbit = gr.Button("🚀 Send")
86
-
87
- sumbit.click(model_chat,
88
- inputs=[textbox, chatbot, system_state],
89
- outputs=[textbox, chatbot, system_input])
90
- clear_history.click(fn=clear_session,
91
- inputs=[],
92
- outputs=[textbox, chatbot])
93
- modify_system.click(fn=modify_system_session,
94
- inputs=[system_input],
95
- outputs=[system_state, system_input, chatbot])
 
 
 
 
 
 
 
96
 
97
- demo.queue(api_open=False).launch(max_threads=10,height=800, share=False)
 
 
 
 
 
 
7
  from dashscope.api_entities.dashscope_response import Role
8
  from typing import List, Optional, Tuple, Dict
9
  from urllib.error import HTTPError
10
+
11
+ # الإعدادات الافتراضية
12
  default_system = 'You are a helpful assistant.'
13
+ default_temp = 0.7
14
+ default_top_p = 0.9
15
+ default_max_tokens = 1024
16
+ default_max_history = 5
17
 
18
  YOUR_API_TOKEN = os.getenv('YOUR_API_TOKEN')
19
  dashscope.api_key = YOUR_API_TOKEN
20
 
21
+ # تعريف أنواع البيانات
22
  History = List[Tuple[str, str]]
23
  Messages = List[Dict[str, str]]
24
 
 
26
  return '', []
27
 
28
  def modify_system_session(system: str) -> str:
29
+ if not system:
30
  system = default_system
31
  return system, system, []
32
 
33
+ def history_to_messages(history: History, system: str, max_history: int) -> Messages:
34
+ # تحديد عدد الرسائل التاريخية المسموح بها
35
+ if max_history > 0:
36
+ history = history[-max_history:]
37
+
38
  messages = [{'role': Role.SYSTEM, 'content': system}]
39
  for h in history:
40
  messages.append({'role': Role.USER, 'content': h[0]})
41
  messages.append({'role': Role.ASSISTANT, 'content': h[1]})
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']
 
49
  history.append([q['content'], r['content']])
50
  return system, history
51
 
52
+ def model_chat(
53
+ query: Optional[str],
54
+ history: Optional[History],
55
+ system: str,
56
+ temperature: float,
57
+ top_p: float,
58
+ max_tokens: int,
59
+ max_history: int
60
  ) -> Tuple[str, str, History]:
61
+ if not query:
62
  query = ''
63
+ history = history or []
64
+
65
+ # تحويل التاريخ إلى رسائل مع تطبيق الحد الأقصى
66
+ messages = history_to_messages(history, system, max_history)
67
  messages.append({'role': Role.USER, 'content': query})
68
+
69
+ try:
70
+ gen = Generation.call(
71
+ model="qwen-72b-chat",
72
+ messages=messages,
73
+ result_format='message',
74
+ stream=True,
75
+ temperature=temperature,
76
+ top_p=top_p,
77
+ max_tokens=max_tokens
78
+ )
79
+
80
+ for response in gen:
81
+ if response.status_code == HTTPStatus.OK:
82
+ role = response.output.choices[0].message.role
83
+ content = response.output.choices[0].message.content
84
+ system, history = messages_to_history(messages + [{'role': role, 'content': content}])
85
+ yield '', history, system
86
+ else:
87
+ raise HTTPError(f'Error: {response.code} - {response.message}')
88
+ except Exception as e:
89
+ raise gr.Error(f'حدث خطأ في الاتصال: {str(e)}')
90
 
91
+ # واجهة المستخدم
92
  with gr.Blocks() as demo:
93
  gr.Markdown("""<center><font size=8>Qwen-72B-Chat</center>""")
94
+ gr.Markdown("""<center><font size=4>نموذج محادثة متقدم بدعم الذاكرة والإعدادات</center>""")
95
+
96
+ # قسم إعدادات النظام
97
  with gr.Row():
98
  with gr.Column(scale=3):
99
+ system_input = gr.Textbox(value=default_system, lines=2, label='تعليمات النظام')
100
+ gr.Markdown("""
101
+ **ملاحظة:** يمكنك كتابة توجيهات خاصة للنموذج هنا. مثال:
102
+ - "أنت مساعد يتحدث العربية بطلاقة"
103
+ - "أجب دائمًا بصيغة الشعر"
104
+ """)
105
  with gr.Column(scale=1):
106
+ modify_system = gr.Button("🛠️ تحديث التعليمات ومسح الذاكرة")
107
  system_state = gr.Textbox(value=default_system, visible=False)
108
+
109
+ # عناصر التحكم في النموذج
110
+ with gr.Accordion("الإعدادات المتقدمة", open=False):
111
+ with gr.Row():
112
+ temperature = gr.Slider(0.0, 2.0, value=default_temp, label="الابتكار (Temperature)")
113
+ top_p = gr.Slider(0.0, 1.0, value=default_top_p, label="الدقة (Top-P)")
114
+ with gr.Row():
115
+ max_tokens = gr.Number(default_max_tokens, label="الحد الأقصى للرموز")
116
+ max_history = gr.Number(default_max_history, label="الحد الأقصى للذاكرة (عدد الدورات)")
117
+
118
+ # منطقة المحادثة
119
+ chatbot = gr.Chatbot(label='المحادثة')
120
+ textbox = gr.Textbox(lines=2, label='الرسالة')
121
+
122
+ # أزرار التحكم
123
  with gr.Row():
124
+ clear_history = gr.Button("🧹 مسح المحادثة")
125
+ submit = gr.Button("🚀 إرسال")
126
+
127
+ # معالجة الأحداث
128
+ submit.click(
129
+ model_chat,
130
+ inputs=[textbox, chatbot, system_state, temperature, top_p, max_tokens, max_history],
131
+ outputs=[textbox, chatbot, system_input]
132
+ )
133
+ clear_history.click(
134
+ fn=clear_session,
135
+ inputs=[],
136
+ outputs=[textbox, chatbot]
137
+ )
138
+ modify_system.click(
139
+ fn=modify_system_session,
140
+ inputs=[system_input],
141
+ outputs=[system_state, system_input, chatbot]
142
+ )
143
 
144
+ # تشغيل التطبيق
145
+ demo.queue(api_open=False).launch(
146
+ max_threads=10,
147
+ height=800,
148
+ share=False
149
+ )