E-slam commited on
Commit
8089fb7
·
verified ·
1 Parent(s): fb80265

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +3 -233
main.py CHANGED
@@ -1,237 +1,7 @@
1
  import flet as ft
2
- import flet_fastapi
3
- from flet import *
4
- import requests
5
- import json
6
- import pandas as pd
7
- import elasticsearch_serverless
8
- import re
9
- import os
10
- import pickle
11
-
12
- def remove_arabic_diacritics(text):
13
- diacritics_pattern = re.compile(r'[\u064B-\u065F\u0670\u06D6-\u06ED]')
14
- no_diacritics_text = re.sub(diacritics_pattern, '', text)
15
- return no_diacritics_text
16
-
17
- diacritics = re.compile("""
18
- ّ | # Tashdid
19
- َ | # Fatha
20
- ً | # Tanwin Fath
21
- ُ | # Damma
22
- ٌ | # Tanwin Damm
23
- ِ | # Kasra
24
- ٍ | # Tanwin Kasr
25
- ْ | # Sukun
26
- ـ # Tatwil/Kashida
27
- """, re.VERBOSE)
28
-
29
- def normalize_arabic(text):
30
- text = diacritics.sub('', text)
31
- text = text.replace('أ', 'ا')
32
- text = text.replace('إ', 'ا')
33
- text = text.replace('آ', 'ا')
34
- text = text.replace('ة', 'ه')
35
- text = text.replace('ى', 'ي')
36
- return text
37
-
38
- book_selected = False
39
- first_run = 0
40
-
41
- client = elasticsearch_serverless.Elasticsearch(
42
- "https://e790c240926f48a78eec48ccb79ddcd1.us-central1.gcp.cloud.es.io:443",
43
- api_key="MmY2VlpZOEJtVVZGLVJjNHpzRTM6akFlVmM2bHhSVmU1M25qRTIyZy1kUQ"
44
- )
45
-
46
- with open('books_list.pkl', 'rb') as file:
47
- books_list = pickle.load(file)
48
-
49
- def e_search(query):
50
- query = remove_arabic_diacritics(query)
51
- query = normalize_arabic(query)
52
-
53
- j_query = {
54
- "size": 500,
55
- "query": {
56
- "match": {
57
- "Text": query
58
- }
59
- }
60
- }
61
-
62
- response_search = client.search(index="books_jsons_01", body=j_query)
63
- unique_books = {}
64
- for hit in response_search['hits']['hits']:
65
- book = hit['_source']['Book']
66
- page = hit['_source']['Page']
67
- score = hit['_score']
68
- if book not in unique_books:
69
- unique_books[book] = {'Pages': {page: score}, 'Count': 1}
70
- else:
71
- if page not in unique_books[book]['Pages']:
72
- unique_books[book]['Pages'][page] = score
73
- unique_books[book]['Count'] += 1
74
-
75
- book_data = []
76
- for book, info in unique_books.items():
77
- pages = sorted(info['Pages'].items())
78
- book_data.append({'Book': book, 'Pages': [page for page, _ in pages], 'Scores': [score for _, score in pages], 'Count': info['Count']})
79
- df = pd.DataFrame(book_data)
80
- df = df.head(12)
81
-
82
- def get_top_two(row):
83
- sorted_row = sorted(zip(row['Pages'], row['Scores']), key=lambda x: x[1], reverse=True)
84
- return [page for page, score in sorted_row[:2]]
85
-
86
- try:
87
- df['Top Two Pages'] = df.apply(get_top_two, axis=1)
88
- except:
89
- pass
90
-
91
- return df
92
 
93
  async def main(page: ft.Page):
94
- async def e_search_book(query):
95
- book_name = book_btn.text
96
-
97
- query = remove_arabic_diacritics(query)
98
- query = normalize_arabic(query)
99
-
100
- j_query = {
101
- "size": 10,
102
- "query": {
103
- "bool": {
104
- "must": [
105
- {"match": {"Text": query}},
106
- {"match": {"Book": book_name}}
107
- ]
108
- }
109
- }
110
- }
111
-
112
- response_search = client.search(index="books_jsons_01", body=j_query)
113
- data = []
114
- for hit in response_search['hits']['hits']:
115
- book = hit['_source']['Book']
116
- page = hit['_source']['Page']
117
- score = hit['_score']
118
- text = hit['_source']['Text']
119
- data.append({
120
- "Book": book,
121
- "Page": page,
122
- "Score": score,
123
- "Text": text
124
- })
125
-
126
- df = pd.DataFrame(data)
127
- return df
128
-
129
- async def printer(e, name):
130
- query_feild.value = name
131
- await page.update_async()
132
-
133
- async def query_feild_changed(e):
134
- datatable_row.visible = False
135
- listview.visible = True
136
-
137
- query_list = books_list
138
- list_items = {
139
- name: ListTile(
140
- title=Text(name),
141
- leading=Icon(icons.ARROW_RIGHT_SHARP),
142
- on_click=lambda e, name=name: printer(e, name) # Capture current value of name
143
- )
144
- for name in query_list
145
- }
146
-
147
- str_lower = normalize_arabic(e.control.value)
148
- listview.controls = [
149
- list_items.get(n) for n in query_list if str_lower in normalize_arabic(n)
150
- ] if str_lower else []
151
- await page.update_async()
152
-
153
- async def send_button(e):
154
- global first_run
155
-
156
- datatable_row.visible = True
157
- listview.visible = False
158
-
159
- if first_run >= 1:
160
- res_dt.columns.clear()
161
- res_dt.rows.clear()
162
-
163
- first_run = 1
164
-
165
- if not book_selected:
166
- e_search_df = e_search(query_feild.value)
167
-
168
- for i in range(len(e_search_df.columns)):
169
- res_dt.columns.append(DataColumn(Text(e_search_df.columns[i])))
170
-
171
- for i in range(e_search_df.shape[0]):
172
- res_dt.rows.append(DataRow(cells=[
173
- DataCell(Text(e_search_df['Book'][i], width=450)),
174
- DataCell(Text(e_search_df['Pages'][i], width=180)),
175
- DataCell(Text(e_search_df['Scores'][i], width=180)),
176
- DataCell(Text(e_search_df['Count'][i], width=120)),
177
- DataCell(Text(e_search_df['Top Two Pages'][i], width=200))
178
- ]))
179
-
180
- else:
181
- e_search_df = await e_search_book(query_feild.value)
182
-
183
- for i in range(len(e_search_df.columns)):
184
- res_dt.columns.append(DataColumn(Text(e_search_df.columns[i])))
185
-
186
- for i in range(e_search_df.shape[0]):
187
- txt = e_search_df['Text'][i][:80].replace("\n", " ")
188
- res_dt.rows.append(DataRow(cells=[
189
- DataCell(Text(e_search_df['Book'][i], width=450)),
190
- DataCell(Text(e_search_df['Page'][i], width=180)),
191
- DataCell(Text(e_search_df['Score'][i], width=180)),
192
- DataCell(Row([Text(f"{txt}...", width=400), IconButton(icon=icons.ARROW_RIGHT_OUTLINED, height=50, on_click=show_book_text)]))
193
- ]))
194
-
195
- await page.update_async()
196
-
197
- async def book_btn_filter(e):
198
- global book_selected
199
- book_value = query_feild.value
200
-
201
- if book_value in books_list:
202
- book_btn.text = query_feild.value
203
- book_btn.bgcolor = colors.GREEN
204
- book_selected = True
205
- else:
206
- book_btn.text = "No Book Found"
207
- book_btn.bgcolor = colors.CYAN
208
- book_selected = False
209
-
210
- await page.update_async()
211
-
212
- def show_book_text(e):
213
- pass
214
-
215
- res_dt = DataTable(
216
- border=border.all(2, "blue"),
217
- border_radius=10,
218
- column_spacing=10,
219
- )
220
-
221
- datatable_row = Row([res_dt], alignment=MainAxisAlignment.CENTER)
222
- datatable_row.visible = False
223
-
224
- query_feild = TextField(label="Inquiry", hint_text="Please write your inquiry", expand=True,
225
- on_change=query_feild_changed)
226
-
227
- query_send = FloatingActionButton(icon=icons.SEND, on_click=send_button)
228
- book_btn = ElevatedButton(text="Book Filter", height=55, width=180, icon=icons.FILTER,
229
- on_click=book_btn_filter, bgcolor=colors.CYAN, color=colors.WHITE,
230
- style=ButtonStyle(shape=RoundedRectangleBorder(radius=10)))
231
-
232
- listview = ListView(expand=1, spacing=10, padding=20)
233
- Query_row = Row(controls=[query_feild, book_btn, query_send])
234
-
235
- await page.add_async(Query_row, listview, datatable_row)
236
 
237
- app = flet_fastapi.app(main)
 
1
  import flet as ft
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2
 
3
  async def main(page: ft.Page):
4
+ await asyncio.sleep(1)
5
+ page.add(ft.Text("Hello, async world!"))
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6
 
7
+ ft.app(main)