Commit
6cb6ca1
·
verified ·
1 Parent(s): b8f1a35

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +135 -0
app.py ADDED
@@ -0,0 +1,135 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import gradio as gr
3
+ from gradio_client import Client
4
+ import json
5
+ from dotenv import load_dotenv
6
+
7
+ load_dotenv()
8
+
9
+ BACKEND_SPACE = "Omartificial-Intelligence-Space/backend"
10
+
11
+ HF_TOKEN = os.getenv("HF_TOKEN")
12
+ API_KEY = os.getenv("API_ACCESS_KEY", "")
13
+
14
+ client = Client(BACKEND_SPACE, hf_token=HF_TOKEN)
15
+
16
+ def query_backend(message):
17
+ if not message.strip():
18
+ return {"response": " الرجاء إدخال استفسار صالح."}
19
+
20
+ print(f"إرسال الاستفسار إلى الخلفية: {message}")
21
+
22
+ try:
23
+ result = client.predict(
24
+ query=message,
25
+ api_key_check=API_KEY,
26
+ api_name="/process_query"
27
+ )
28
+
29
+ print(f" استجابة الخلفية: {result}")
30
+
31
+ if isinstance(result, str):
32
+ return {"response": result}
33
+ elif isinstance(result, dict) and "response" in result:
34
+ return result
35
+ else:
36
+ return {"response": "تنسيق استجابة غير متوقع."}
37
+
38
+ except Exception as e:
39
+ print(f"خطأ: {str(e)}")
40
+ return {"response": f" خطأ: {str(e)}"}
41
+
42
+ import time
43
+
44
+ def respond(message, chat_history):
45
+ if not message.strip():
46
+ return "", chat_history
47
+
48
+ chat_history = chat_history + [{"role": "user", "content": message}]
49
+
50
+ chat_history = chat_history + [{"role": "assistant", "content": "⏳ جاري البحث ..."}]
51
+
52
+ yield "", chat_history
53
+
54
+ backend_response = query_backend(message)
55
+
56
+ if isinstance(backend_response, dict) and "response" in backend_response:
57
+ answer = backend_response["response"]
58
+ else:
59
+ answer = backend_response
60
+
61
+ chat_history[-1] = {"role": "assistant", "content": answer}
62
+
63
+ yield "", chat_history # تحديث الدردشة بالإجابة النهائية
64
+
65
+ example_questions = [
66
+ "من هو راوي حديث مَنْ حَجَّ لِلهِ فَلَمْ يَرْفُثْ وَلَمْ يَفْسُقْ رَجَعَ كَيَوْمِ وَلَدَتْهُ أُمُّهُ؟",
67
+ "ماذا قيل عن عبدالرحمن بن عبدالله بن عتبة بن مسعود؟",
68
+ "اريد حديث عن فضل بيان فضل قراءة القرآن في الصلاة.",
69
+ "اذكر ثلاث فوائد لهذا الحديث : عن عبد الله بن عمرو رضي الله عنهما قال: قال رسول الله صلى الله عليه وسلم: «يقالُ لصاحبِ القرآن: اقرَأ وارتَقِ، ورتِّل كما كُنْتَ ترتِّل في الدُنيا، فإن منزِلَكَ عندَ آخرِ آية تقرؤه»"
70
+ ]
71
+
72
+ LOGO_PATH = "whitelogo.jpeg"
73
+
74
+ with gr.Blocks(title="مساعد قاعدة بيانات الحديث الشريف") as demo:
75
+ with gr.Row():
76
+ gr.HTML(
77
+ f"""
78
+ <div style='background-color:#4A90E2; color:white; padding:20px; border-radius:10px; text-align:center; font-size:20px; font-weight:bold; display: flex; align-items: center; justify-content: center;'>
79
+ <img src='{LOGO_PATH}' alt='Logo' style='height:50px; margin-right:15px;'>
80
+ مساعد قاعدة بيانات الحديث الشريف<br>
81
+ <span style='font-size:16px;'>استكشف الأحاديث النبوية وعلومها بسهولة</span>
82
+ </div>
83
+ """
84
+ )
85
+
86
+ with gr.Row():
87
+ with gr.Column(scale=3):
88
+ gr.Markdown("""
89
+ ## ℹ️ عن التطبيق
90
+
91
+ يساعد هذا المساعد في الإجابة عن الأسئلة المتعلقة بالأحاديث النبوية:
92
+
93
+ * نصوص الأحاديث وترجماتها
94
+ * فوائد وشروحات عن الحديث
95
+ * معلومات عن أشهر الرواة
96
+ * الأحكام الفقهية المتعلقة بالأحاديث
97
+
98
+ 🔹 تحقق دائمًا من المعلومات مع علماء موثوقين.
99
+ """)
100
+
101
+ gr.Markdown("## أسئلة مقترحة")
102
+ example_buttons = [gr.Button(question) for question in example_questions]
103
+
104
+ with gr.Column(scale=7):
105
+ chatbot_component = gr.Chatbot(height=500, bubble_full_width=False, type="messages")
106
+
107
+ with gr.Row():
108
+ message_box = gr.Textbox(
109
+ show_label=False,
110
+ placeholder="📝 اكتب استفسارك هنا...",
111
+ container=False,
112
+ lines=2
113
+ )
114
+
115
+ send_btn = gr.Button("إرسال", variant="primary")
116
+ clear_btn = gr.Button("🗑️ مسح الدردشة")
117
+
118
+ gr.Markdown(
119
+ """
120
+ ---
121
+ **⚠️ تنبيه:** هذا المساعد الذكي يقدم معلومات حديثية، لكن يجب دائمًا الرجوع إلى العلماء للتحقق من المسائل الشرعية.
122
+ """
123
+ )
124
+
125
+ msg_submit = message_box.submit(respond, [message_box, chatbot_component], [message_box, chatbot_component])
126
+ send_click = send_btn.click(respond, [message_box, chatbot_component], [message_box, chatbot_component])
127
+ clear_click = clear_btn.click(lambda: (None, []), None, [message_box, chatbot_component])
128
+
129
+ for btn in example_buttons:
130
+ btn.click(lambda q: (q, []), [btn], [message_box, chatbot_component]).then(
131
+ respond, [message_box, chatbot_component], [message_box, chatbot_component]
132
+ )
133
+
134
+ if __name__ == "__main__":
135
+ demo.launch(debug=True)