Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -17,39 +17,54 @@ 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(
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
28 |
|
29 |
messages = [{"role": "system", "content": system_message}]
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
messages.append({"role": "user", "content": message})
|
37 |
|
38 |
response = ""
|
39 |
-
|
40 |
-
for message in client.chat_completion(
|
41 |
messages,
|
42 |
max_tokens=max_tokens,
|
43 |
stream=True,
|
44 |
temperature=temperature,
|
45 |
top_p=top_p,
|
46 |
):
|
47 |
-
token =
|
48 |
response += token
|
49 |
yield response
|
50 |
|
51 |
|
52 |
|
|
|
53 |
# واجهة `Gradio` للتفاعل مع المستخدم
|
54 |
demo = gr.ChatInterface(
|
55 |
respond,
|
|
|
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,
|
57 |
stream=True,
|
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,
|