Spaces:
Runtime error
Runtime error
Update app.py
Browse files
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(
|
21 |
-
|
22 |
-
|
23 |
-
|
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 |
-
|
36 |
-
|
37 |
-
|
38 |
-
#
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
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 |
-
|
|
|
62 |
response += token
|
63 |
yield response
|
64 |
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
# واجهة `Gradio` للتفاعل مع المستخدم
|
69 |
demo = gr.ChatInterface(
|
70 |
-
respond,
|
71 |
-
chatbot=gr.Chatbot(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"),
|