zizo66 commited on
Commit
75da7c7
·
verified ·
1 Parent(s): a532daf

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +29 -40
app.py CHANGED
@@ -12,45 +12,36 @@ scenarios = {
12
  "shopping": "You are in a store. Help the user ask for prices and sizes.",
13
  }
14
 
15
- # دالة لاختيار السيناريو المناسب
16
  def scenario_prompt(choice):
17
  return scenarios.get(choice, "You are a language tutor AI. Help users practice real-life conversations.")
18
 
19
- # دالة للمحادثة مع الذكاء الاصطناعي
20
- def respond(*args):
21
- """
22
- نتوقع أن يتم تمرير 7 معاملات بالترتيب التالي:
23
- 0: message
24
- 1: history
25
- 2: scenario (من قائمة الاختيارات)
26
- 3: system_message (قيمة مُدخلة، لكن سنستخدم scenario لتحديدها)
27
- 4: max_tokens
28
- 5: temperature
29
- 6: top_p
30
- """
31
- # التأكد من وجود العدد الكافي من المعاملات:
32
- if len(args) < 7:
33
- return "Insufficient arguments provided."
34
 
35
- message = args[0]
36
- history = args[1]
37
- scenario = args[2]
38
- # تجاهل system_message المُمرر واستبداله بالرسالة المناسبة للسيناريو:
39
- system_message = scenario_prompt(scenario)
40
- max_tokens = args[4]
41
- temperature = args[5]
42
- top_p = args[6]
43
-
44
- messages = [{"role": "system", "content": system_message}]
45
- # إذا كان history يحتوي على محادثات سابقة:
46
- for user_msg, assistant_msg in history:
47
- if user_msg:
48
- messages.append({"role": "user", "content": user_msg})
49
- if assistant_msg:
50
- messages.append({"role": "assistant", "content": assistant_msg})
51
  messages.append({"role": "user", "content": message})
52
-
53
  response = ""
 
54
  for m in client.chat_completion(
55
  messages,
56
  max_tokens=max_tokens,
@@ -58,17 +49,15 @@ def respond(*args):
58
  temperature=temperature,
59
  top_p=top_p,
60
  ):
61
- token = m.choices[0].delta.content
 
62
  response += token
63
  yield response
64
 
65
-
66
-
67
-
68
- # واجهة `Gradio` للتفاعل مع المستخدم
69
  demo = gr.ChatInterface(
70
- respond,
71
- chatbot=gr.Chatbot(type="messages"), # إصلاح التحذير باستخدام `type="messages"`
72
  additional_inputs=[
73
  gr.Dropdown(choices=list(scenarios.keys()), label="Choose a scenario", value="restaurant"),
74
  gr.Textbox(value=scenario_prompt("restaurant"), label="System message"),
 
12
  "shopping": "You are in a store. Help the user ask for prices and sizes.",
13
  }
14
 
15
+ # دالة لاختيار الرسالة المناسبة للسيناريو
16
  def scenario_prompt(choice):
17
  return scenarios.get(choice, "You are a language tutor AI. Help users practice real-life conversations.")
18
 
19
+ # دالة المحادثة مع الذكاء الاصطناعي
20
+ def respond(message, history, scenario, system_message, max_tokens, temperature, top_p):
21
+ # التأكد من أن history عبارة عن قائمة، وإذا لم توجد بيانات سابقة، اجعلها قائمة فارغة.
22
+ if history is None:
23
+ history = []
 
 
 
 
 
 
 
 
 
 
24
 
25
+ # استبدال system_message بالرسالة الخاصة بالسيناريو
26
+ sys_msg = scenario_prompt(scenario)
27
+
28
+ # إعداد قائمة الرسائل بما يتوافق مع تنسيق Chat Completion
29
+ messages = [{"role": "system", "content": sys_msg}]
30
+
31
+ # التأكد من أن history عبارة عن قائمة من الـ tuples
32
+ for pair in history:
33
+ if isinstance(pair, (list, tuple)) and len(pair) == 2:
34
+ user_msg, assistant_msg = pair
35
+ if user_msg:
36
+ messages.append({"role": "user", "content": user_msg})
37
+ if assistant_msg:
38
+ messages.append({"role": "assistant", "content": assistant_msg})
39
+
40
+ # إضافة الرسالة الحالية للمستخدم
41
  messages.append({"role": "user", "content": message})
42
+
43
  response = ""
44
+ # الحصول على الرد مع البث (streaming)
45
  for m in client.chat_completion(
46
  messages,
47
  max_tokens=max_tokens,
 
49
  temperature=temperature,
50
  top_p=top_p,
51
  ):
52
+ # استخراج النص من كل توكن متدفق
53
+ token = m.choices[0].delta.content if m.choices[0].delta.content is not None else ""
54
  response += token
55
  yield response
56
 
57
+ # إنشاء واجهة Gradio للتفاعل مع المستخدم
 
 
 
58
  demo = gr.ChatInterface(
59
+ fn=respond,
60
+ chatbot=gr.Chatbot(type="messages"),
61
  additional_inputs=[
62
  gr.Dropdown(choices=list(scenarios.keys()), label="Choose a scenario", value="restaurant"),
63
  gr.Textbox(value=scenario_prompt("restaurant"), label="System message"),