FDSRashid commited on
Commit
6baf346
ยท
verified ยท
1 Parent(s): 321b6f5

all tabs have speed search capability

Browse files
Files changed (1) hide show
  1. app.py +55 -57
app.py CHANGED
@@ -13,7 +13,7 @@ import re
13
  from collections import defaultdict
14
  from huggingface_hub import hf_hub_download
15
  import json
16
- from huggingface_hub import hf_hub_download
17
 
18
 
19
  pattern = r'"(.*?)"'
@@ -144,8 +144,8 @@ def visualize_isnad(taraf_num, yaxis):
144
  isnad_hadith = isnad_hadith.merge(narrator_bios[['Rawi ID', 'Famous Name']], left_on='Destination', right_on='Rawi ID', how='left').rename(columns={'Famous Name': 'Student'})
145
  isnad_hadith[['Source', 'Destination']] = isnad_hadith[['Source', 'Destination']].astype(str)
146
  # Fill missing values with 'ูู„ุงู†'
147
- isnad_hadith['Teacher'].fillna('ูู„ุงู†', inplace=True)
148
- isnad_hadith['Student'].fillna('ูู„ุงู†', inplace=True)
149
 
150
  end_nodes = df['End Transmitter ID'].tolist()
151
  G = nx.from_pandas_edgelist(isnad_hadith, source = 'Source', target = 'Destination', create_using = nx.DiGraph())
@@ -221,17 +221,15 @@ def visualize_subTaraf(taraf_num, hadith_str, yaxis):
221
  sub_taraf = taraf[taraf['Index'].isin(hadiths)]
222
 
223
  taraf_hadith = sub_taraf['bookid_hadithid'].to_list()
224
- taraf_hadith_split = [i.split('_') for i in taraf_hadith]
225
-
226
- hadith_cleaned = isnad_info['Tarafs Cleaned'].apply(lambda x: taraf_num in x)
227
- isnad_hadith = isnad_info[hadith_cleaned]
228
- subtaraf_filter = isnad_info['Hadiths Cleaned'].apply(lambda x: any(i in x for i in taraf_hadith_split))
229
- isnad_hadith = isnad_hadith[subtaraf_filter]
230
 
231
- isnad_hadith['Teacher'] = isnad_hadith['Source'].apply(lambda x: narrator_bios[narrator_bios['Rawi ID'].astype(int) == int(x)]['Famous Name'].to_list())
232
- isnad_hadith['Student'] = isnad_hadith['Destination'].apply(lambda x: narrator_bios[narrator_bios['Rawi ID'].astype(int) == int(x)]['Famous Name'].to_list())
233
- isnad_hadith['Teacher'] = isnad_hadith['Teacher'].apply(lambda x: x[0] if len(x)==1 else 'ูู„ุงู†')
234
- isnad_hadith['Student'] = isnad_hadith['Student'].apply(lambda x: x[0] if len(x)==1 else 'ูู„ุงู†')
 
 
235
 
236
  G = nx.from_pandas_edgelist(isnad_hadith, source = 'Source', target = 'Destination', create_using = nx.DiGraph())
237
  isnad_pos = nx.nx_agraph.graphviz_layout(G, prog='dot')
@@ -267,13 +265,16 @@ def taraf_booknum(taraf_num):
267
  def visualize_hadith_isnad(df, yaxis):
268
  df['bookid_hadithid'] = df['Book_ID'].astype(str) + '_' + df['Hadith Number'].astype(str)
269
  hadith = matn_info[matn_info['bookid_hadithid'].isin(df['bookid_hadithid'])]
270
- taraf_hadith_split = [i.split('_') for i in hadith['bookid_hadithid'].to_list()]
271
- hadith_cleaned = isnad_info['Hadiths Cleaned'].apply(lambda x: any(i in x for i in taraf_hadith_split))
272
- isnad_hadith = isnad_info[hadith_cleaned]
273
- isnad_hadith['Teacher'] = isnad_hadith['Source'].apply(lambda x: narrator_bios[narrator_bios['Rawi ID'].astype(int) == int(x)]['Famous Name'].to_list())
274
- isnad_hadith['Student'] = isnad_hadith['Destination'].apply(lambda x: narrator_bios[narrator_bios['Rawi ID'].astype(int) == int(x)]['Famous Name'].to_list())
275
- isnad_hadith['Teacher'] = isnad_hadith['Teacher'].apply(lambda x: x[0] if len(x)==1 else 'ูู„ุงู†')
276
- isnad_hadith['Student'] = isnad_hadith['Student'].apply(lambda x: x[0] if len(x)==1 else 'ูู„ุงู†')
 
 
 
277
 
278
  G = nx.from_pandas_edgelist(isnad_hadith, source = 'Source', target = 'Destination', create_using = nx.DiGraph())
279
  isnad_pos = nx.nx_agraph.graphviz_layout(G, prog='dot')
@@ -302,64 +303,61 @@ def visualize_hadith_isnad(df, yaxis):
302
 
303
 
304
  def visualize_narrator_taraf(taraf_num, narrator, yaxis):
305
- taraf = matn_info[matn_info['taraf_ID'] == taraf_num]
306
- num_hadith = taraf.shape[0]
307
- taraf['Index'] = np.arange(num_hadith)
308
  hadith_cleaned = isnad_info['Tarafs Cleaned'].apply(lambda x: taraf_num in x)
309
  isnad_hadith = isnad_info[hadith_cleaned]
310
- isnad_hadith['Teacher'] = isnad_hadith['Source'].apply(lambda x: narrator_bios[narrator_bios['Rawi ID'].astype(int) == int(x)]['Famous Name'].to_list())
311
- isnad_hadith['Student'] = isnad_hadith['Destination'].apply(lambda x: narrator_bios[narrator_bios['Rawi ID'].astype(int) == int(x)]['Famous Name'].to_list())
312
- isnad_hadith['Teacher'] = isnad_hadith['Teacher'].apply(lambda x: x[0] if len(x)==1 else 'ูู„ุงู†')
313
- isnad_hadith['Student'] = isnad_hadith['Student'].apply(lambda x: x[0] if len(x)==1 else 'ูู„ุงู†')
 
314
  taraf_hadith = taraf['bookid_hadithid'].to_list()
315
- taraf_hadith_split = [i.split('_') for i in taraf_hadith]
316
 
 
317
  G = nx.from_pandas_edgelist(isnad_hadith, source = 'Source', target = 'Destination', create_using = nx.DiGraph())
318
  if narrator not in G.nodes():
319
  raise gr.Error('Narrator not in Isnad of Taraf!')
320
- # nodes = list(G.nodes)
321
- # nodes = [node for node in nodes if node not in list(nx.ancestors(G, narrator)) + list(nx.descendants(G, narrator)) and node !=narrator]
322
- # [G.remove_nodes_from(nodes)]
323
  matns_with_narrator = []
324
  end_node = {}
325
- for i in range(len(taraf_hadith_split)):
326
- isnad_in_hadith1 = isnad_hadith['Hadiths Cleaned'].apply(lambda x: taraf_hadith_split[i] in x )
327
- isnad_hadith1 = isnad_hadith[isnad_in_hadith1]
328
- G1 = nx.from_pandas_edgelist(isnad_hadith1, source = 'Source', target = 'Destination', create_using = nx.DiGraph())
329
- node = [n for n, d in G1.out_degree() if d == 0]
330
- if narrator in list(G1.nodes):
331
- matns_with_narrator.append(taraf_hadith[i])
332
- for n in node:
333
- if n not in list(end_node.keys()):
334
- end_node[n] = []
335
- end_node[n].append(str(i))
336
-
337
- matns_with_narrator_split = [i.split('_') for i in matns_with_narrator]
338
- hadith_cleaned = isnad_hadith['Hadiths Cleaned'].apply(lambda x: any(i in x for i in matns_with_narrator_split))
339
  isnad_hadith = isnad_hadith[hadith_cleaned]
340
  G = nx.from_pandas_edgelist(isnad_hadith, source = 'Source', target = 'Destination', create_using = nx.DiGraph())
341
  isnad_pos = nx.nx_agraph.graphviz_layout(G, prog='dot')
342
 
343
  narrator_matn_info = taraf[taraf['bookid_hadithid'].isin(matns_with_narrator)]
344
- num_hadith = narrator_matn_info.shape[0]
345
- narrator_matn_info['Subset Index'] = np.arange(num_hadith)
 
346
  x_stretch = 4
347
  y_stretch = 4
348
  net = Network(directed =True, select_menu=True, cdn_resources='remote')
349
 
350
  for node, pos in isnad_pos.items():
351
- node_info,student_narrations,student_gen, student_rank, node_name = get_node_info(node)
352
- if node == '99999':
353
- net.add_node(node, font = {'size':50, 'color': 'black'}, color = '#000000', label = f'{node_name} \n ID: {node} - Gen {student_gen}', x= pos[0]*x_stretch, y= -1*pos[1]*y_stretch, size= 70)
354
- elif node in list(end_node.keys()):
355
- net.add_node(node, font = {'size':30, 'color': 'red'}, color = value_to_hex(student_narrations), label = f'{node_name} \n {student_rank} \n ID: {node} - Gen {student_gen} \n Hadiths {", ".join(end_node[node])}', x= pos[0]*x_stretch, y= -1*pos[1]*y_stretch, size= 50)
356
- else:
357
- net.add_node(node, font = {'size':30, 'color': 'red'}, color = value_to_hex(student_narrations), label = f'{node_name} \n {student_rank} \n ID: {node} - Gen {student_gen}', x= pos[0]*x_stretch, y= -1*pos[1]*y_stretch, size= 50)
 
358
  for edge in G.edges:
359
  row = isnad_hadith[(isnad_hadith['Source'] == edge[0]) & (isnad_hadith['Destination'] == edge[1])].iloc[0]
360
- source = row['Source']
361
- target = row['Destination']
362
- net.add_edge(source, target, color = value_to_hex(int(row[f'{yaxis} Count'])), label = f"{row[f'{yaxis} Count']}")
363
  net.toggle_physics(False)
364
  html = net.generate_html()
365
  html = html.replace("'", "\"")
 
13
  from collections import defaultdict
14
  from huggingface_hub import hf_hub_download
15
  import json
16
+
17
 
18
 
19
  pattern = r'"(.*?)"'
 
144
  isnad_hadith = isnad_hadith.merge(narrator_bios[['Rawi ID', 'Famous Name']], left_on='Destination', right_on='Rawi ID', how='left').rename(columns={'Famous Name': 'Student'})
145
  isnad_hadith[['Source', 'Destination']] = isnad_hadith[['Source', 'Destination']].astype(str)
146
  # Fill missing values with 'ูู„ุงู†'
147
+ # isnad_hadith['Teacher'].fillna('ูู„ุงู†', inplace=True)
148
+ # isnad_hadith['Student'].fillna('ูู„ุงู†', inplace=True)
149
 
150
  end_nodes = df['End Transmitter ID'].tolist()
151
  G = nx.from_pandas_edgelist(isnad_hadith, source = 'Source', target = 'Destination', create_using = nx.DiGraph())
 
221
  sub_taraf = taraf[taraf['Index'].isin(hadiths)]
222
 
223
  taraf_hadith = sub_taraf['bookid_hadithid'].to_list()
224
+ isnad_hadith = isnad_info.iloc[np.array([hadith_lookup[i] for i in taraf_hadith]).reshape(-1)][['Source', 'Destination']]
225
+ isnad_hadith[['Source', 'Destination']] = isnad_hadith[['Source', 'Destination']].astype(int)
 
 
 
 
226
 
227
+ # Merge isnad_hadith with narrator_bios for Teacher and Student
228
+ isnad_hadith = isnad_hadith.merge(narrator_bios[['Rawi ID', 'Famous Name']], left_on='Source', right_on='Rawi ID', how='left').rename(columns={'Famous Name': 'Teacher'})
229
+ isnad_hadith = isnad_hadith.merge(narrator_bios[['Rawi ID', 'Famous Name']], left_on='Destination', right_on='Rawi ID', how='left').rename(columns={'Famous Name': 'Student'})
230
+ isnad_hadith[['Source', 'Destination']] = isnad_hadith[['Source', 'Destination']].astype(str)
231
+ # isnad_hadith['Teacher'].fillna('ูู„ุงู†', inplace=True)
232
+ # isnad_hadith['Student'].fillna('ูู„ุงู†', inplace=True)
233
 
234
  G = nx.from_pandas_edgelist(isnad_hadith, source = 'Source', target = 'Destination', create_using = nx.DiGraph())
235
  isnad_pos = nx.nx_agraph.graphviz_layout(G, prog='dot')
 
265
  def visualize_hadith_isnad(df, yaxis):
266
  df['bookid_hadithid'] = df['Book_ID'].astype(str) + '_' + df['Hadith Number'].astype(str)
267
  hadith = matn_info[matn_info['bookid_hadithid'].isin(df['bookid_hadithid'])]
268
+ taraf_hadith = df['bookid_hadithid'].to_list()
269
+ isnad_hadith = isnad_info.iloc[np.array([hadith_lookup[i] for i in taraf_hadith]).reshape(-1)][['Source', 'Destination']]
270
+ isnad_hadith[['Source', 'Destination']] = isnad_hadith[['Source', 'Destination']].astype(int)
271
+
272
+ # Merge isnad_hadith with narrator_bios for Teacher and Student
273
+ isnad_hadith = isnad_hadith.merge(narrator_bios[['Rawi ID', 'Famous Name']], left_on='Source', right_on='Rawi ID', how='left').rename(columns={'Famous Name': 'Teacher'})
274
+ isnad_hadith = isnad_hadith.merge(narrator_bios[['Rawi ID', 'Famous Name']], left_on='Destination', right_on='Rawi ID', how='left').rename(columns={'Famous Name': 'Student'})
275
+ isnad_hadith[['Source', 'Destination']] = isnad_hadith[['Source', 'Destination']].astype(str)
276
+ # isnad_hadith['Teacher'].fillna('ูู„ุงู†', inplace=True)
277
+ # isnad_hadith['Student'].fillna('ูู„ุงู†', inplace=True)
278
 
279
  G = nx.from_pandas_edgelist(isnad_hadith, source = 'Source', target = 'Destination', create_using = nx.DiGraph())
280
  isnad_pos = nx.nx_agraph.graphviz_layout(G, prog='dot')
 
303
 
304
 
305
  def visualize_narrator_taraf(taraf_num, narrator, yaxis):
306
+ taraf = matn_info[matn_info['taraf_ID'] == taraf_num].copy()
307
+ taraf['Index'] = np.arange(len(taraf))
 
308
  hadith_cleaned = isnad_info['Tarafs Cleaned'].apply(lambda x: taraf_num in x)
309
  isnad_hadith = isnad_info[hadith_cleaned]
310
+ isnad_hadith[['Source', 'Destination']] = isnad_hadith[['Source', 'Destination']].astype(int)
311
+ # Merge isnad_hadith with narrator_bios for Teacher and Student
312
+ isnad_hadith = isnad_hadith.merge(narrator_bios[['Rawi ID', 'Famous Name']], left_on='Source', right_on='Rawi ID', how='left').rename(columns={'Famous Name': 'Teacher'})
313
+ isnad_hadith = isnad_hadith.merge(narrator_bios[['Rawi ID', 'Famous Name']], left_on='Destination', right_on='Rawi ID', how='left').rename(columns={'Famous Name': 'Student'})
314
+ isnad_hadith[['Source', 'Destination']] = isnad_hadith[['Source', 'Destination']].astype(str)
315
  taraf_hadith = taraf['bookid_hadithid'].to_list()
 
316
 
317
+ # original graph of whole taraf
318
  G = nx.from_pandas_edgelist(isnad_hadith, source = 'Source', target = 'Destination', create_using = nx.DiGraph())
319
  if narrator not in G.nodes():
320
  raise gr.Error('Narrator not in Isnad of Taraf!')
321
+
 
 
322
  matns_with_narrator = []
323
  end_node = {}
324
+
325
+ # Process each hadith in taraf_hadith_split
326
+ for idx, split_hadith in enumerate(taraf_hadith):
327
+ isnad_hadith1 = isnad_info.iloc[hadith_lookup[taraf_hadith[i]]][['Source', 'Destination']]
328
+ G1 = nx.from_pandas_edgelist(isnad_hadith1, source='Source', target='Destination', create_using=nx.DiGraph())
329
+ if narrator in G1.nodes:
330
+ matns_with_narrator.append(taraf_hadith[idx])
331
+ for node in (n for n, d in G1.out_degree() if d == 0):
332
+ end_node.setdefault(node, []).append(str(idx))
333
+
334
+ # Update the graph
335
+ hadith_cleaned = isnad_info.iloc[np.array([hadith_lookup[i] for i in matns_with_narrator]).reshape(-1)][['Source', 'Destination']]
 
 
336
  isnad_hadith = isnad_hadith[hadith_cleaned]
337
  G = nx.from_pandas_edgelist(isnad_hadith, source = 'Source', target = 'Destination', create_using = nx.DiGraph())
338
  isnad_pos = nx.nx_agraph.graphviz_layout(G, prog='dot')
339
 
340
  narrator_matn_info = taraf[taraf['bookid_hadithid'].isin(matns_with_narrator)]
341
+ narrator_matn_info['Subset Index'] = np.arange(len(narrator_matn_info))
342
+
343
+ # Visualization with pyvis
344
  x_stretch = 4
345
  y_stretch = 4
346
  net = Network(directed =True, select_menu=True, cdn_resources='remote')
347
 
348
  for node, pos in isnad_pos.items():
349
+ node_info, student_narrations, student_gen, student_rank, node_name = get_node_info(node)
350
+ label = f'{node_name} \n ID: {node} - Gen {student_gen}'
351
+ size = 70 if node == '99999' else 50
352
+ font_color = 'black' if node == '99999' else 'red'
353
+ hadiths = f" \n Hadiths {', '.join(end_node[node])}" if node in end_node else ''
354
+ net.add_node(node, font={'size': 30, 'color': font_color}, color=value_to_hex(student_narrations),
355
+ label=f"{label} {hadiths}", x=pos[0] * x_stretch, y=-pos[1] * y_stretch, size=size)
356
+
357
  for edge in G.edges:
358
  row = isnad_hadith[(isnad_hadith['Source'] == edge[0]) & (isnad_hadith['Destination'] == edge[1])].iloc[0]
359
+ net.add_edge(edge[0], edge[1], color=value_to_hex(int(row[f'{yaxis} Count'])), label=f"{row[f'{yaxis} Count']}")
360
+
 
361
  net.toggle_physics(False)
362
  html = net.generate_html()
363
  html = html.replace("'", "\"")