Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -3,73 +3,80 @@ from dotenv import load_dotenv
|
|
3 |
from huggingface_hub import InferenceClient
|
4 |
import gradio as gr
|
5 |
|
6 |
-
# تحميل متغيرات البيئة
|
7 |
load_dotenv()
|
8 |
-
|
9 |
-
# الحصول على مفتاح API من البيئة
|
10 |
api_key = os.getenv("FIREWORKS_API_KEY")
|
11 |
if not api_key:
|
12 |
raise ValueError("❌ لم يتم العثور على FIREWORKS_API_KEY في ملف .env")
|
13 |
|
14 |
-
# إعداد عميل InferenceClient
|
15 |
client = InferenceClient(
|
16 |
provider="fireworks-ai",
|
17 |
api_key=api_key
|
18 |
)
|
19 |
|
20 |
-
# رسالة
|
21 |
system_message = {
|
22 |
"role": "system",
|
23 |
"content": (
|
24 |
-
"أنت
|
25 |
-
"
|
26 |
-
"تجنب الرد على الأسئلة خارج مجال الأشعة الطبية."
|
27 |
)
|
28 |
}
|
29 |
|
30 |
-
|
31 |
-
MAX_HISTORY_TURNS = 5
|
32 |
|
33 |
-
# دالة
|
34 |
def chat_fn(message, history):
|
35 |
try:
|
36 |
messages = [system_message]
|
37 |
|
38 |
-
#
|
39 |
-
if
|
40 |
history = history[-MAX_HISTORY_TURNS:]
|
|
|
|
|
|
|
41 |
|
42 |
-
# إضافة
|
43 |
-
for user_msg, bot_msg in history:
|
44 |
-
messages.append({"role": "user", "content": user_msg})
|
45 |
-
messages.append({"role": "assistant", "content": bot_msg})
|
46 |
-
|
47 |
-
# إضافة رسالة المستخدم الجديدة
|
48 |
messages.append({"role": "user", "content": message})
|
49 |
|
50 |
-
#
|
51 |
completion = client.chat.completions.create(
|
52 |
model="deepseek-ai/DeepSeek-R1",
|
53 |
messages=messages,
|
54 |
max_tokens=4096,
|
|
|
55 |
)
|
56 |
|
57 |
-
|
58 |
-
|
59 |
-
return "❌ لم يتم الحصول على رد من النموذج."
|
60 |
|
61 |
-
return completion.choices[0].message.content
|
62 |
|
63 |
except Exception as e:
|
64 |
-
return f"❌
|
65 |
|
66 |
-
# واجهة
|
67 |
chat_interface = gr.ChatInterface(
|
68 |
fn=chat_fn,
|
69 |
-
title="
|
70 |
-
description="
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
71 |
)
|
72 |
|
73 |
-
# إطلاق
|
74 |
if __name__ == "__main__":
|
75 |
chat_interface.launch()
|
|
|
3 |
from huggingface_hub import InferenceClient
|
4 |
import gradio as gr
|
5 |
|
6 |
+
# تحميل متغيرات البيئة
|
7 |
load_dotenv()
|
|
|
|
|
8 |
api_key = os.getenv("FIREWORKS_API_KEY")
|
9 |
if not api_key:
|
10 |
raise ValueError("❌ لم يتم العثور على FIREWORKS_API_KEY في ملف .env")
|
11 |
|
12 |
+
# إعداد عميل InferenceClient
|
13 |
client = InferenceClient(
|
14 |
provider="fireworks-ai",
|
15 |
api_key=api_key
|
16 |
)
|
17 |
|
18 |
+
# إعداد رسالة النظام
|
19 |
system_message = {
|
20 |
"role": "system",
|
21 |
"content": (
|
22 |
+
"أنت مساعد ذكي متخصص في مجال الأشعة الطبية. "
|
23 |
+
"تقدم إجابات دقيقة وواضحة في تفسير الصور الشعاعية والاستشارات المرتبطة بها فقط."
|
|
|
24 |
)
|
25 |
}
|
26 |
|
27 |
+
MAX_HISTORY_TURNS = 6
|
|
|
28 |
|
29 |
+
# دالة المحادثة
|
30 |
def chat_fn(message, history):
|
31 |
try:
|
32 |
messages = [system_message]
|
33 |
|
34 |
+
# الاحتفاظ بآخر الرسائل فقط
|
35 |
+
if history:
|
36 |
history = history[-MAX_HISTORY_TURNS:]
|
37 |
+
for user_msg, bot_msg in history:
|
38 |
+
messages.append({"role": "user", "content": user_msg})
|
39 |
+
messages.append({"role": "assistant", "content": bot_msg})
|
40 |
|
41 |
+
# إضافة الرسالة الحالية
|
|
|
|
|
|
|
|
|
|
|
42 |
messages.append({"role": "user", "content": message})
|
43 |
|
44 |
+
# استدعاء النموذج
|
45 |
completion = client.chat.completions.create(
|
46 |
model="deepseek-ai/DeepSeek-R1",
|
47 |
messages=messages,
|
48 |
max_tokens=4096,
|
49 |
+
temperature=0.7,
|
50 |
)
|
51 |
|
52 |
+
if not completion or not completion.choices or not completion.choices[0].message.content.strip():
|
53 |
+
return "❌ لم يتم استلام رد من النموذج. حاول مرة أخرى أو تحقق من الاتصال."
|
|
|
54 |
|
55 |
+
return completion.choices[0].message.content.strip()
|
56 |
|
57 |
except Exception as e:
|
58 |
+
return f"❌ خطأ: {e}"
|
59 |
|
60 |
+
# إعداد واجهة Gradio بشكل جميل
|
61 |
chat_interface = gr.ChatInterface(
|
62 |
fn=chat_fn,
|
63 |
+
title="🧠 RAY AI Chat - متخصص في الأشعة",
|
64 |
+
description="مرحبًا بك في مساعد الذكاء الاصطناعي الخاص بتفسير الأشعة. يرجى طرح سؤالك الطبي المتعلق بالتصوير الشعاعي 👇",
|
65 |
+
theme=gr.themes.Base(
|
66 |
+
primary_hue="blue",
|
67 |
+
secondary_hue="slate",
|
68 |
+
font=["Cairo", "sans-serif"]
|
69 |
+
),
|
70 |
+
submit_btn="أرسل 📨",
|
71 |
+
retry_btn="🔁 إعادة",
|
72 |
+
clear_btn="🧹 مسح",
|
73 |
+
textbox=gr.Textbox(
|
74 |
+
placeholder="اكتب سؤالك هنا... مثال: ما معنى وجود تكلسات في الغدة الدرقية؟",
|
75 |
+
container=True,
|
76 |
+
scale=7
|
77 |
+
),
|
78 |
)
|
79 |
|
80 |
+
# إطلاق التطبيق
|
81 |
if __name__ == "__main__":
|
82 |
chat_interface.launch()
|