Spaces:
Sleeping
Sleeping
File size: 5,615 Bytes
6cb6ca1 540a4b0 6cb6ca1 b298f23 97a7c69 b298f23 6cb6ca1 5120471 6cb6ca1 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 |
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():
gr.HTML(
f"""
<div style='background-color:#4A90E2; color:white; padding:20px; border-radius:10px; text-align:center; font-size:22px; font-weight:bold; display: flex; flex-direction: column; align-items: center; justify-content: center;'>
<img src='{LOGO_PATH}' alt='Logo' style='height:70px; margin-bottom:10px; display: block;'>
<div>📚 مساعد قاعدة بيانات الحديث الشريف</div>
<span style='font-size:16px;'>استكشف الأحاديث النبوية وعلومها بسهولة</span>
</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) |