kdallash commited on
Commit
931f8db
·
verified ·
1 Parent(s): d7685b4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +130 -50
app.py CHANGED
@@ -51,73 +51,153 @@ if test_emb.shape[0] != anchor_index.d:
51
  # Import retrieval logic
52
  from retrieval import hybrid_search_fixed
53
  from utils import preprocess_query
54
-
55
- # -----------------------------
56
- # Search function (UI entry)
57
- # -----------------------------
58
 
59
  def search_hadith(query, top_k):
60
- if query.strip() == "":
61
- return pd.DataFrame(columns=["الموضوع", "نص الحديث", "hadith page on Islamweb.net"])
62
-
63
- try:
64
- results_df, _ = hybrid_search_fixed(
65
- query=query,
66
- df=df,
67
- bm25=bm25,
68
- model=model,
69
- preprocess_query=preprocess_query,
70
- hadith_embeddings=hadith_embeddings,
71
- anchor_index=anchor_index,
72
- anchor_dict=anchor_dict,
73
- unique_anchor_texts=unique_anchor_texts,
74
- top_k=int(top_k)
75
- )
76
-
77
- return results_df[["main_subj", "matn_text", "url"]] \
78
- .rename(columns={
79
- "main_subj": "الموضوع",
80
- "matn_text": "نص الحديث",
81
- "url": "hadith page on Islamweb.net"
82
- })
83
- except Exception as e:
84
- print(f"Error in search: {e}")
85
- return pd.DataFrame({
86
- "الموضوع": ["Error"],
87
- "نص الحديث": [str(e)],
88
- "hadith page on Islamweb.net": [""]
89
- })
90
-
91
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
92
  # Gradio Interface
 
 
93
  interface = gr.Interface(
94
  fn=search_hadith,
95
  inputs=[
96
  gr.Textbox(
97
- label="أدخل موضوع البحث أو السؤال",
98
- placeholder="مثال: أهمية النية وأثرها في قبول الأعمال"
 
99
  ),
100
  gr.Slider(
101
  minimum=1,
102
  maximum=20,
103
  value=5,
104
  step=1,
105
- label="عدد الأحاديث المعروضة"
106
  )
107
  ],
108
- outputs=gr.Dataframe(
109
- label="نتائج البحث",
110
- wrap=True
111
- ),
112
- title="Using NLP to search hadith in sahih bukhari",
113
- description=("AI-powered search engine that understands the **meaning** of queries, not just keyword matches."),
114
- examples=[
115
- ["أهمية النية وأثرها في قبول الأعمال", 5],
116
- ["فضل الصلاة", 5],
117
- ["حقوق الجار", 5],
118
- ]
119
  )
120
 
121
- # Launch app
 
 
 
122
  if __name__ == "__main__":
123
  interface.launch()
 
51
  # Import retrieval logic
52
  from retrieval import hybrid_search_fixed
53
  from utils import preprocess_query
54
+ # ===============================
55
+ # Search Function
56
+ # ===============================
 
57
 
58
  def search_hadith(query, top_k):
59
+ if not query.strip():
60
+ return "<p class='empty'>Please enter a search query.</p>"
61
+
62
+ results_df, _ = hybrid_search_fixed(
63
+ query=query,
64
+ df=df,
65
+ bm25=bm25,
66
+ model=model,
67
+ hadith_embeddings=hadith_embeddings,
68
+ anchor_index=anchor_index,
69
+ anchor_dict=anchor_dict,
70
+ unique_anchor_texts=unique_anchor_texts,
71
+ top_k=int(top_k)
72
+ )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
73
 
74
+ html = "<div class='results'>"
75
+
76
+ for i, row in results_df.iterrows():
77
+ html += f"""
78
+ <div class="card">
79
+ <div class="card-header">
80
+ <span class="index">#{i+1}</span>
81
+ <span class="topic">{row['main_subj']}</span>
82
+ </div>
83
+
84
+ <div class="text">
85
+ {row['clean_text']}
86
+ </div>
87
+
88
+ <div class="footer">
89
+ <a href="{row['url']}" target="_blank">
90
+ 📖 View Hadith Source
91
+ </a>
92
+ </div>
93
+ </div>
94
+ """
95
+
96
+ html += "</div>"
97
+ return html
98
+
99
+ # ===============================
100
+ # Custom CSS
101
+ # ===============================
102
+
103
+ custom_css = """
104
+ body {
105
+ background-color: #f7f9fc;
106
+ }
107
+
108
+ .results {
109
+ direction: rtl;
110
+ font-family: "Tahoma", "Arial", sans-serif;
111
+ }
112
+
113
+ .card {
114
+ background: white;
115
+ border-radius: 12px;
116
+ padding: 18px 20px;
117
+ margin-bottom: 16px;
118
+ box-shadow: 0 4px 14px rgba(0,0,0,0.06);
119
+ }
120
+
121
+ .card-header {
122
+ display: flex;
123
+ align-items: center;
124
+ gap: 10px;
125
+ margin-bottom: 12px;
126
+ }
127
+
128
+ .index {
129
+ background: #2563eb;
130
+ color: white;
131
+ padding: 4px 10px;
132
+ border-radius: 20px;
133
+ font-size: 14px;
134
+ }
135
+
136
+ .topic {
137
+ font-weight: bold;
138
+ color: #1f2937;
139
+ font-size: 16px;
140
+ }
141
+
142
+ .text {
143
+ line-height: 1.9;
144
+ font-size: 16px;
145
+ color: #111827;
146
+ margin-bottom: 14px;
147
+ }
148
+
149
+ .footer a {
150
+ text-decoration: none;
151
+ color: #2563eb;
152
+ font-weight: bold;
153
+ }
154
+
155
+ .footer a:hover {
156
+ text-decoration: underline;
157
+ }
158
+
159
+ .empty {
160
+ direction: ltr;
161
+ font-size: 16px;
162
+ color: #6b7280;
163
+ text-align: center;
164
+ }
165
+ """
166
+
167
+ # ===============================
168
  # Gradio Interface
169
+ # ===============================
170
+
171
  interface = gr.Interface(
172
  fn=search_hadith,
173
  inputs=[
174
  gr.Textbox(
175
+ label="🔍 Search Query",
176
+ placeholder="e.g. Importance of intention in Islam",
177
+ lines=2
178
  ),
179
  gr.Slider(
180
  minimum=1,
181
  maximum=20,
182
  value=5,
183
  step=1,
184
+ label="📌 Number of Results"
185
  )
186
  ],
187
+ outputs=gr.HTML(),
188
+ title="📚 Intelligent Hadith Search Engine",
189
+ description="""
190
+ An AI-powered semantic search engine for Hadith retrieval.
191
+ The system combines lexical, semantic, and topic-aware retrieval
192
+ to return results based on meaning not just keyword matching.
193
+ """,
194
+ css=custom_css,
195
+ allow_flagging="never"
 
 
196
  )
197
 
198
+ # ===============================
199
+ # Launch
200
+ # ===============================
201
+
202
  if __name__ == "__main__":
203
  interface.launch()