Spaces:
Runtime error
Runtime error
| import os | |
| import gradio as gr | |
| from gradio_client import Client | |
| import json | |
| from dotenv import load_dotenv | |
| load_dotenv() | |
| BACKEND_SPACE = "Omartificial-Intelligence-Space/backend" | |
| HF_TOKEN = os.getenv("HF_TOKEN") | |
| API_KEY = os.getenv("API_ACCESS_KEY", "") | |
| client = Client(BACKEND_SPACE, hf_token=HF_TOKEN) | |
| def query_backend(message): | |
| if not message.strip(): | |
| return {"response": " الرجاء إدخال استفسار صالح."} | |
| print(f"إرسال الاستفسار إلى الخلفية: {message}") | |
| try: | |
| result = client.predict( | |
| query=message, | |
| api_key_check=API_KEY, | |
| api_name="/process_query" | |
| ) | |
| print(f" استجابة الخلفية: {result}") | |
| if isinstance(result, str): | |
| return {"response": result} | |
| elif isinstance(result, dict) and "response" in result: | |
| return result | |
| else: | |
| return {"response": "تنسيق استجابة غير متوقع."} | |
| except Exception as e: | |
| print(f"خطأ: {str(e)}") | |
| return {"response": f" خطأ: {str(e)}"} | |
| import time | |
| def respond(message, chat_history): | |
| if not message.strip(): | |
| return "", chat_history | |
| chat_history = chat_history + [{"role": "user", "content": message}] | |
| chat_history = chat_history + [{"role": "assistant", "content": "⏳ جاري البحث ..."}] | |
| yield "", chat_history | |
| backend_response = query_backend(message) | |
| if isinstance(backend_response, dict) and "response" in backend_response: | |
| answer = backend_response["response"] | |
| else: | |
| answer = backend_response | |
| chat_history[-1] = {"role": "assistant", "content": answer} | |
| yield "", chat_history # تحديث الدردشة بالإجابة النهائية | |
| example_questions = [ | |
| "من هو راوي حديث مَنْ حَجَّ لِلهِ فَلَمْ يَرْفُثْ وَلَمْ يَفْسُقْ رَجَعَ كَيَوْمِ وَلَدَتْهُ أُمُّهُ؟", | |
| "ماذا قيل عن عبدالرحمن بن عبدالله بن عتبة بن مسعود؟", | |
| "اريد حديث عن فضل بيان فضل قراءة القرآن في الصلاة.", | |
| "اذكر ثلاث فوائد لهذا الحديث : عن عبد الله بن عمرو رضي الله عنهما قال: قال رسول الله صلى الله عليه وسلم: «يقالُ لصاحبِ القرآن: اقرَأ وارتَقِ، ورتِّل كما كُنْتَ ترتِّل في الدُنيا، فإن منزِلَكَ عندَ آخرِ آية تقرؤه»" | |
| ] | |
| LOGO_PATH = "image.png" | |
| with gr.Blocks(title="مساعد قاعدة بيانات الحديث الشريف") as demo: | |
| with gr.Row(): | |
| with gr.Column(scale=1, min_width=100): | |
| gr.Image(value=LOGO_PATH, show_label=False, interactive=False, height=80, width=80) | |
| with gr.Column(scale=8): | |
| gr.HTML( | |
| """ | |
| <div style=' | |
| background-color:#2E7D32; | |
| color:white; | |
| padding:15px; | |
| border-radius:10px; | |
| text-align:center; | |
| font-size:22px; | |
| font-weight:bold; | |
| box-shadow: 0px 4px 10px rgba(0, 0, 0, 0.1); | |
| '> | |
| مساعد قاعدة بيانات الحديث الشريف | |
| <div style='font-size:16px; font-weight:normal; margin-top:5px;'> | |
| استكشف الأحاديث النبوية وعلومها بسهولة | |
| </div> | |
| </div> | |
| """ | |
| ) | |
| with gr.Row(): | |
| with gr.Column(scale=3): | |
| gr.Markdown(""" | |
| ## ℹ️ عن التطبيق | |
| يساعد هذا المساعد في الإجابة عن الأسئلة المتعلقة بالأحاديث النبوية: | |
| * نصوص الأحاديث وترجماتها | |
| * فوائد وشروحات عن الحديث | |
| * معلومات عن أشهر الرواة | |
| * الأحكام الفقهية المتعلقة بالأحاديث | |
| 🔹 تحقق دائمًا من المعلومات مع علماء موثوقين. | |
| """) | |
| gr.Markdown("## أسئلة مقترحة") | |
| example_buttons = [gr.Button(question) for question in example_questions] | |
| with gr.Column(scale=7): | |
| chatbot_component = gr.Chatbot(height=500, bubble_full_width=False, type="messages") | |
| with gr.Row(): | |
| message_box = gr.Textbox( | |
| show_label=False, | |
| placeholder="📝 اكتب استفسارك هنا...", | |
| container=False, | |
| lines=2 | |
| ) | |
| send_btn = gr.Button("إرسال", variant="primary") | |
| clear_btn = gr.Button("🗑️ مسح الدردشة") | |
| gr.Markdown( | |
| """ | |
| --- | |
| **⚠️ تنبيه:** يقدم المساعد الذكي يقدم معلومات عن الحديث الشريف، لكن يجب دائمًا الرجوع إلى العلماء للتحقق من المسائل الشرعية. | |
| """ | |
| ) | |
| msg_submit = message_box.submit(respond, [message_box, chatbot_component], [message_box, chatbot_component]) | |
| send_click = send_btn.click(respond, [message_box, chatbot_component], [message_box, chatbot_component]) | |
| clear_click = clear_btn.click(lambda: (None, []), None, [message_box, chatbot_component]) | |
| for btn in example_buttons: | |
| btn.click(lambda q: (q, []), [btn], [message_box, chatbot_component]).then( | |
| respond, [message_box, chatbot_component], [message_box, chatbot_component] | |
| ) | |
| if __name__ == "__main__": | |
| demo.launch(debug=True) |