E-slam commited on
Commit
9197ffc
·
verified ·
1 Parent(s): c6a3c52

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +65 -101
main.py CHANGED
@@ -1,18 +1,17 @@
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
 
11
  def remove_arabic_diacritics(text):
12
  diacritics_pattern = re.compile(r'[\u064B-\u065F\u0670\u06D6-\u06ED]')
13
-
14
  no_diacritics_text = re.sub(diacritics_pattern, '', text)
15
-
16
  return no_diacritics_text
17
 
18
  diacritics = re.compile("""
@@ -37,22 +36,17 @@ def normalize_arabic(text):
37
  return text
38
 
39
  book_selected = False
40
-
41
  first_run = 0
 
42
  client = elasticsearch_serverless.Elasticsearch(
43
- "https://e790c240926f48a78eec48ccb79ddcd1.us-central1.gcp.cloud.es.io:443",
44
- api_key="MmY2VlpZOEJtVVZGLVJjNHpzRTM6akFlVmM2bHhSVmU1M25qRTIyZy1kUQ"
45
  )
46
 
47
-
48
- import pickle
49
  with open('books_list.pkl', 'rb') as file:
50
  books_list = pickle.load(file)
51
 
52
-
53
  def e_search(query):
54
- url_search = 'http://localhost:9202/books_jsons_01/_search'
55
-
56
  query = remove_arabic_diacritics(query)
57
  query = normalize_arabic(query)
58
 
@@ -65,9 +59,7 @@ def e_search(query):
65
  }
66
  }
67
 
68
-
69
  response_search = client.search(index="books_jsons_01", body=j_query)
70
-
71
  unique_books = {}
72
  for hit in response_search['hits']['hits']:
73
  book = hit['_source']['Book']
@@ -82,16 +74,16 @@ def e_search(query):
82
 
83
  book_data = []
84
  for book, info in unique_books.items():
85
- pages = sorted(info['Pages'].items())
86
  book_data.append({'Book': book, 'Pages': [page for page, _ in pages], 'Scores': [score for _, score in pages], 'Count': info['Count']})
87
  df = pd.DataFrame(book_data)
88
-
89
  df = df.head(12)
90
  print(j_query)
91
 
92
  def get_top_two(row):
93
  sorted_row = sorted(zip(row['Pages'], row['Scores']), key=lambda x: x[1], reverse=True)
94
  return [page for page, score in sorted_row[:2]]
 
95
  try:
96
  df['Top Two Pages'] = df.apply(get_top_two, axis=1)
97
  except:
@@ -99,43 +91,33 @@ def e_search(query):
99
 
100
  return df
101
 
102
-
103
- async def main (page:ft.Page):
104
  def e_search_book(query):
105
  book_name = book_btn.text
106
  print(book_name)
107
 
108
- url_search = 'http://localhost:9202/books_jsons_01/_search'
109
-
110
  query = remove_arabic_diacritics(query)
111
  query = normalize_arabic(query)
 
112
  j_query = {
113
- "size": 10,
114
- "query": {
115
- "bool": {
116
- "must": [
117
- {
118
- "match": {
119
- "Text": query
120
- }
121
- },
122
- {
123
- "match": {
124
- "Book": book_name
125
  }
126
- }
127
- ]
128
- }
129
  }
130
- }
131
  response_search = client.search(index="books_jsons_01", body=j_query)
132
-
133
  data = []
134
  for hit in response_search['hits']['hits']:
135
  book = hit['_source']['Book']
136
  page = hit['_source']['Page']
137
  score = hit['_score']
138
- text = hit['_source']['Text']
139
  data.append({
140
  "Book": book,
141
  "Page": page,
@@ -146,16 +128,15 @@ async def main (page:ft.Page):
146
  df = pd.DataFrame(data)
147
  return df
148
 
149
- def printer(e, name):
150
- query_feild.value=name
151
  await page.update()
152
 
153
- def query_feild_changed(string):
154
  datatable_row.visible = False
155
- listview.visible=True
156
 
157
  query_list = books_list
158
-
159
  list_items = {
160
  name: ListTile(
161
  title=Text(name),
@@ -171,70 +152,61 @@ async def main (page:ft.Page):
171
  ] if str_lower else []
172
  await page.update()
173
 
174
- def send_button(e):
175
-
176
  global first_run
177
 
178
  datatable_row.visible = True
179
- listview.visible=False
180
-
181
-
182
- if first_run >= 1 :
183
 
 
184
  res_dt.columns.clear()
185
  res_dt.rows.clear()
186
 
187
- first_run=1
188
 
189
  if not book_selected:
190
  e_search_df = e_search(query_feild.value)
191
 
192
- for i in range (len(e_search_df.columns)):
193
- res_dt.columns.append(DataColumn(Text(e_search_df.columns[i]))) # Set column width here
194
 
195
- for i in range (e_search_df.shape[0]):
196
  res_dt.rows.append(DataRow(cells=[
197
- DataCell(Text(e_search_df['Book'][i], width = 450)),
198
- DataCell(Text(e_search_df['Pages'][i], width = 180)),
199
- DataCell(Text(e_search_df['Scores'][i], width = 180)),
200
- DataCell(Text(e_search_df['Count'][i], width = 120)),
201
- DataCell(Text(e_search_df['Top Two Pages'][i], width = 200))
202
  ]))
203
 
204
-
205
  else:
206
  e_search_df = e_search_book(query_feild.value)
207
 
208
- for i in range (len(e_search_df.columns)):
209
- res_dt.columns.append(DataColumn(Text(e_search_df.columns[i]))) # Set column width here
210
 
211
- for i in range ((e_search_df.shape[0])):
212
  txt = e_search_df['Text'][i][:80].replace("\n", " ")
213
  res_dt.rows.append(DataRow(cells=[
214
- DataCell(Text(e_search_df['Book'][i], width = 450)),
215
- DataCell(Text(e_search_df['Page'][i], width = 180)),
216
- DataCell(Text(e_search_df['Score'][i], width = 180)),
217
- DataCell(Row([Text(f"{txt}...", width = 400), IconButton(icon=icons.ARROW_RIGHT_OUTLINED, height = 50, on_click = show_book_text)]))
218
  ]))
219
 
220
-
221
-
222
-
223
  await page.update()
224
 
225
-
226
- def book_btn_filter(e):
227
  global book_selected
228
  book_value = query_feild.value
229
 
230
- if book_value in books_list:
231
  book_btn.text = query_feild.value
232
- book_btn.bgcolor=colors.GREEN
233
- book_selected = True
234
-
235
  else:
236
  book_btn.text = "No Book Found"
237
- book_btn.bgcolor=colors.CYAN
238
  book_selected = False
239
 
240
  await page.update()
@@ -242,34 +214,26 @@ async def main (page:ft.Page):
242
  def show_book_text(e):
243
  pass
244
 
245
- res_dt=DataTable(
246
-
247
  border=border.all(2, "blue"),
248
  border_radius=10,
249
  column_spacing=10,
250
- #data_row_min_height = 80
251
- )
252
- datatable_row = Row([res_dt],alignment=MainAxisAlignment.CENTER)
253
-
254
- query_feild=TextField(label="Inquiry",hint_text="Please write your inquiry", expand=True,
255
- on_change=query_feild_changed
256
- )
257
-
258
- query_send=FloatingActionButton(icon=icons.SEND,
259
- on_click=send_button
260
- )
261
- book_btn = ElevatedButton(text="Book Filter", height = 55, width= 180, icon=icons.FILTER,on_click = book_btn_filter,
262
- bgcolor=colors.CYAN,
263
- color=colors.WHITE,
264
- style=ButtonStyle(shape=RoundedRectangleBorder(radius=10)),
265
- )
266
- listview = ListView(expand=1, spacing=10, padding=20)
267
-
268
- Query_row = Row(controls=[query_feild,book_btn,query_send])
269
-
270
- #Initial Invisable
271
  datatable_row.visible = False
272
- await page.add_async(Query_row,listview,datatable_row)
 
 
 
 
 
 
 
273
 
274
- app =flet_fastapi.app(main)
 
 
 
275
 
 
 
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("""
 
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
 
 
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']
 
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
  print(j_query)
82
 
83
  def get_top_two(row):
84
  sorted_row = sorted(zip(row['Pages'], row['Scores']), key=lambda x: x[1], reverse=True)
85
  return [page for page, score in sorted_row[:2]]
86
+
87
  try:
88
  df['Top Two Pages'] = df.apply(get_top_two, axis=1)
89
  except:
 
91
 
92
  return df
93
 
94
+ async def main(page: ft.Page):
 
95
  def e_search_book(query):
96
  book_name = book_btn.text
97
  print(book_name)
98
 
 
 
99
  query = remove_arabic_diacritics(query)
100
  query = normalize_arabic(query)
101
+
102
  j_query = {
103
+ "size": 10,
104
+ "query": {
105
+ "bool": {
106
+ "must": [
107
+ {"match": {"Text": query}},
108
+ {"match": {"Book": book_name}}
109
+ ]
 
 
 
 
 
110
  }
111
+ }
 
 
112
  }
113
+
114
  response_search = client.search(index="books_jsons_01", body=j_query)
 
115
  data = []
116
  for hit in response_search['hits']['hits']:
117
  book = hit['_source']['Book']
118
  page = hit['_source']['Page']
119
  score = hit['_score']
120
+ text = hit['_source']['Text']
121
  data.append({
122
  "Book": book,
123
  "Page": page,
 
128
  df = pd.DataFrame(data)
129
  return df
130
 
131
+ async def printer(e, name):
132
+ query_feild.value = name
133
  await page.update()
134
 
135
+ async def query_feild_changed(string):
136
  datatable_row.visible = False
137
+ listview.visible = True
138
 
139
  query_list = books_list
 
140
  list_items = {
141
  name: ListTile(
142
  title=Text(name),
 
152
  ] if str_lower else []
153
  await page.update()
154
 
155
+ async def send_button(e):
 
156
  global first_run
157
 
158
  datatable_row.visible = True
159
+ listview.visible = False
 
 
 
160
 
161
+ if first_run >= 1:
162
  res_dt.columns.clear()
163
  res_dt.rows.clear()
164
 
165
+ first_run = 1
166
 
167
  if not book_selected:
168
  e_search_df = e_search(query_feild.value)
169
 
170
+ for i in range(len(e_search_df.columns)):
171
+ res_dt.columns.append(DataColumn(Text(e_search_df.columns[i])))
172
 
173
+ for i in range(e_search_df.shape[0]):
174
  res_dt.rows.append(DataRow(cells=[
175
+ DataCell(Text(e_search_df['Book'][i], width=450)),
176
+ DataCell(Text(e_search_df['Pages'][i], width=180)),
177
+ DataCell(Text(e_search_df['Scores'][i], width=180)),
178
+ DataCell(Text(e_search_df['Count'][i], width=120)),
179
+ DataCell(Text(e_search_df['Top Two Pages'][i], width=200))
180
  ]))
181
 
 
182
  else:
183
  e_search_df = e_search_book(query_feild.value)
184
 
185
+ for i in range(len(e_search_df.columns)):
186
+ res_dt.columns.append(DataColumn(Text(e_search_df.columns[i])))
187
 
188
+ for i in range(e_search_df.shape[0]):
189
  txt = e_search_df['Text'][i][:80].replace("\n", " ")
190
  res_dt.rows.append(DataRow(cells=[
191
+ DataCell(Text(e_search_df['Book'][i], width=450)),
192
+ DataCell(Text(e_search_df['Page'][i], width=180)),
193
+ DataCell(Text(e_search_df['Score'][i], width=180)),
194
+ DataCell(Row([Text(f"{txt}...", width=400), IconButton(icon=icons.ARROW_RIGHT_OUTLINED, height=50, on_click=show_book_text)]))
195
  ]))
196
 
 
 
 
197
  await page.update()
198
 
199
+ async def book_btn_filter(e):
 
200
  global book_selected
201
  book_value = query_feild.value
202
 
203
+ if book_value in books_list:
204
  book_btn.text = query_feild.value
205
+ book_btn.bgcolor = colors.GREEN
206
+ book_selected = True
 
207
  else:
208
  book_btn.text = "No Book Found"
209
+ book_btn.bgcolor = colors.CYAN
210
  book_selected = False
211
 
212
  await page.update()
 
214
  def show_book_text(e):
215
  pass
216
 
217
+ res_dt = DataTable(
 
218
  border=border.all(2, "blue"),
219
  border_radius=10,
220
  column_spacing=10,
221
+ )
222
+
223
+ datatable_row = Row([res_dt], alignment=MainAxisAlignment.CENTER)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
224
  datatable_row.visible = False
225
+
226
+ query_feild = TextField(label="Inquiry", hint_text="Please write your inquiry", expand=True,
227
+ on_change=query_feild_changed)
228
+
229
+ query_send = FloatingActionButton(icon=icons.SEND, on_click=send_button)
230
+ book_btn = ElevatedButton(text="Book Filter", height=55, width=180, icon=icons.FILTER,
231
+ on_click=book_btn_filter, bgcolor=colors.CYAN, color=colors.WHITE,
232
+ style=ButtonStyle(shape=RoundedRectangleBorder(radius=10)))
233
 
234
+ listview = ListView(expand=1, spacing=10, padding=20)
235
+ Query_row = Row(controls=[query_feild, book_btn, query_send])
236
+
237
+ await page.add_async(Query_row, listview, datatable_row)
238
 
239
+ app = flet_fastapi.app(main)