Spaces:
Runtime error
Runtime error
| import os | |
| from dotenv import load_dotenv | |
| from huggingface_hub import InferenceClient | |
| import gradio as gr | |
| # تحميل متغيرات البيئة | |
| load_dotenv() | |
| api_key = os.getenv("FIREWORKS_API_KEY") | |
| if not api_key: | |
| raise ValueError("❌ لم يتم العثور على FIREWORKS_API_KEY في ملف .env") | |
| # إعداد عميل InferenceClient | |
| client = InferenceClient( | |
| provider="fireworks-ai", | |
| api_key=api_key | |
| ) | |
| # إعداد رسالة النظام | |
| system_message = { | |
| "role": "system", | |
| "content": ( | |
| "أنت مساعد ذكي متخصص في مجال الأشعة الطبية. " | |
| "تقدم إجابات دقيقة وواضحة في تفسير الصور الشعاعية والاستشارات المرتبطة بها فقط." | |
| ) | |
| } | |
| MAX_HISTORY_TURNS = 6 | |
| # دالة المحادثة | |
| def chat_fn(message, history): | |
| try: | |
| messages = [system_message] | |
| # الاحتفاظ بآخر الرسائل فقط | |
| if history: | |
| history = history[-MAX_HISTORY_TURNS:] | |
| for user_msg, bot_msg in history: | |
| messages.append({"role": "user", "content": user_msg}) | |
| messages.append({"role": "assistant", "content": bot_msg}) | |
| # إضافة الرسالة الحالية | |
| messages.append({"role": "user", "content": message}) | |
| # استدعاء النموذج | |
| completion = client.chat.completions.create( | |
| model="deepseek-ai/DeepSeek-R1", | |
| messages=messages, | |
| max_tokens=4096, | |
| temperature=0.7, | |
| ) | |
| if not completion or not completion.choices or not completion.choices[0].message.content.strip(): | |
| return "❌ لم يتم استلام رد من النموذج. حاول مرة أخرى أو تحقق من الاتصال." | |
| return completion.choices[0].message.content.strip() | |
| except Exception as e: | |
| return f"❌ خطأ: {e}" | |
| # إعداد واجهة Gradio بشكل جميل | |
| chat_interface = gr.ChatInterface( | |
| fn=chat_fn, | |
| title="🧠 RAY AI Chat - متخصص في الأشعة", | |
| description="مرحبًا بك في مساعد الذكاء الاصطناعي الخاص بتفسير الأشعة. يرجى طرح سؤالك الطبي المتعلق بالتصوير الشعاعي 👇", | |
| theme=gr.themes.Base( | |
| primary_hue="blue", | |
| secondary_hue="slate", | |
| font=["Cairo", "sans-serif"] | |
| ), | |
| submit_btn="أرسل 📨", | |
| retry_btn="🔁 إعادة", | |
| clear_btn="🧹 مسح", | |
| textbox=gr.Textbox( | |
| placeholder="اكتب سؤالك هنا... مثال: ما معنى وجود تكلسات في الغدة الدرقية؟", | |
| container=True, | |
| scale=7 | |
| ), | |
| ) | |
| # إطلاق التطبيق | |
| if __name__ == "__main__": | |
| chat_interface.launch() | |