FDSRashid commited on
Commit
3f8ecd9
ยท
verified ยท
1 Parent(s): 1e1f917

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +43 -27
app.py CHANGED
@@ -13,6 +13,9 @@ import re
13
  from collections import defaultdict
14
  from huggingface_hub import hf_hub_download
15
  import json
 
 
 
16
  pattern = r'"(.*?)"'
17
  # this pattern captures anything in a double quotes.
18
 
@@ -57,9 +60,8 @@ books = load_dataset('FDSRashid/Hadith_info', data_files='Books.csv', token = Se
57
  matn_info['Book_ID'] = matn_info['bookid_hadithid'].apply(lambda x: int(x.split('_')[0]))
58
  matn_info['Hadith Number'] = matn_info['bookid_hadithid'].apply(lambda x: int(x.split('_')[1]))
59
  matn_info = pd.merge(matn_info, books, on='Book_ID')
60
-
61
-
62
- from huggingface_hub import hf_hub_download
63
 
64
  # Download and read a file
65
  file_path = hf_hub_download(
@@ -80,14 +82,14 @@ def value_to_hex(value):
80
 
81
 
82
  def get_node_info(node):
83
- node_info = narrator_bios[narrator_bios['Rawi ID'] == int(node)]
84
- # Extract values with defaults, using `.iloc` if available, otherwise default
85
- student_narrations = node_info['Number of Narrations'].iloc[0] if not node_info.empty else 1
86
- student_gen = node_info['Generation'].iloc[0] if not node_info.empty else -1
87
- student_rank = node_info['Narrator Rank'].iloc[0] if not node_info.empty else 'ูู„ุงู†'
88
- node_name = node_info['Famous Name'].iloc[0] if not node_info.empty else 'ูู„ุงู†'
89
- return node_info,student_narrations,student_gen, student_rank, node_name
90
-
91
 
92
  def visualize_isnad(taraf_num, yaxis):
93
  # Precompute filtered dataframes
@@ -128,18 +130,22 @@ def visualize_isnad(taraf_num, yaxis):
128
  taraf.iloc[i]['Book_Name'],
129
  taraf.iloc[i]['Author'],
130
  taraf.iloc[i]['Hadith Number'],
131
- str(n),
132
  i
133
  ])
134
 
135
  # Convert to DataFrame
136
  df = pd.DataFrame(lst_hadith, columns=['Matn', 'Generation', 'Name', 'Book_Name', 'Author', 'Book Hadith Number', 'End Transmitter ID', 'Hadith Number'])
 
137
 
138
 
139
- isnad_hadith['Teacher'] = isnad_hadith['Source'].apply(lambda x: narrator_bios[narrator_bios['Rawi ID'].astype(int) == int(x)]['Famous Name'].to_list())
140
- isnad_hadith['Student'] = isnad_hadith['Destination'].apply(lambda x: narrator_bios[narrator_bios['Rawi ID'].astype(int) == int(x)]['Famous Name'].to_list())
141
- isnad_hadith['Teacher'] = isnad_hadith['Teacher'].apply(lambda x: x[0] if len(x)==1 else 'ูู„ุงู†')
142
- isnad_hadith['Student'] = isnad_hadith['Student'].apply(lambda x: x[0] if len(x)==1 else 'ูู„ุงู†')
 
 
 
143
 
144
  end_nodes = df['End Transmitter ID'].tolist()
145
  G = nx.from_pandas_edgelist(isnad_hadith, source = 'Source', target = 'Destination', create_using = nx.DiGraph())
@@ -148,19 +154,29 @@ def visualize_isnad(taraf_num, yaxis):
148
  y_stretch = 4
149
  net = Network(directed =True, select_menu=True, cdn_resources='remote')
150
 
 
 
 
 
151
  for node, pos in isnad_pos.items():
152
- node_info,student_narrations,student_gen, student_rank, node_name = get_node_info(node)
153
- if node == '99999':
154
- 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)
 
 
 
 
 
155
  elif node in end_nodes:
156
- end_matn_info = df[df["End Transmitter ID"] == node]
157
- 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 Hadith {" ".join(end_matn_info["Hadith Number"].astype("string").tolist())}', x= pos[0]*x_stretch, y= -1*pos[1]*y_stretch, size= 50)
158
- else:
159
- 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)
160
- for _, row in isnad_hadith.iterrows():
161
- source = row['Source']
162
- target = row['Destination']
163
- net.add_edge(source, target, color = value_to_hex(int(row[f'{yaxis} Count'])), label = f"{row[f'{yaxis} Count']}")
 
164
  net.toggle_physics(False)
165
  html = net.generate_html()
166
  html = html.replace("'", "\"")
 
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'"(.*?)"'
20
  # this pattern captures anything in a double quotes.
21
 
 
60
  matn_info['Book_ID'] = matn_info['bookid_hadithid'].apply(lambda x: int(x.split('_')[0]))
61
  matn_info['Hadith Number'] = matn_info['bookid_hadithid'].apply(lambda x: int(x.split('_')[1]))
62
  matn_info = pd.merge(matn_info, books, on='Book_ID')
63
+ # Preprocess narrator_bios into a dictionary
64
+ narrator_info = narrator_bios.set_index('Rawi ID').to_dict(orient='index')
 
65
 
66
  # Download and read a file
67
  file_path = hf_hub_download(
 
82
 
83
 
84
  def get_node_info(node):
85
+ node = int(node) # Ensure node is an integer
86
+ info = narrator_info.get(node, {})
87
+ student_narrations = info.get('Number of Narrations', 1)
88
+ student_gen = info.get('Generation', -1)
89
+ student_rank = info.get('Narrator Rank', 'ูู„ุงู†')
90
+ node_name = info.get('Famous Name', 'ูู„ุงู†')
91
+ return info, student_narrations, student_gen, student_rank, node_name
92
+
93
 
94
  def visualize_isnad(taraf_num, yaxis):
95
  # Precompute filtered dataframes
 
130
  taraf.iloc[i]['Book_Name'],
131
  taraf.iloc[i]['Author'],
132
  taraf.iloc[i]['Hadith Number'],
133
+ n,
134
  i
135
  ])
136
 
137
  # Convert to DataFrame
138
  df = pd.DataFrame(lst_hadith, columns=['Matn', 'Generation', 'Name', 'Book_Name', 'Author', 'Book Hadith Number', 'End Transmitter ID', 'Hadith Number'])
139
+ isnad_hadith[['Source', 'Destination']] = isnad_hadith[['Source', 'Destination']].astype(int)
140
 
141
 
142
+ # Merge isnad_hadith with narrator_bios for Teacher and Student
143
+ 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'})
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
+
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())
 
154
  y_stretch = 4
155
  net = Network(directed =True, select_menu=True, cdn_resources='remote')
156
 
157
+ # Precompute end_matn_info for each end node
158
+ end_node_data = df.groupby('End Transmitter ID').apply(lambda x: " ".join(x["Hadith Number"].astype("string"))).to_dict()
159
+
160
+ # Loop over isnad_pos
161
  for node, pos in isnad_pos.items():
162
+ node_info, student_narrations, student_gen, student_rank, node_name = get_node_info(node)
163
+ label = f'{node_name} \n {student_rank} \n ID: {node} - Gen {student_gen}'
164
+ size = 50
165
+ font_color = 'red'
166
+ if node == 99999:
167
+ label = f'{node_name} \n ID: {node} - Gen {student_gen}'
168
+ size = 70
169
+ font_color = 'black'
170
  elif node in end_nodes:
171
+ hadith_numbers = end_node_data.get(node, '')
172
+ label += f' \n Hadith {hadith_numbers}'
173
+ net.add_node(node, font={'size': 30, 'color': font_color}, color=value_to_hex(student_narrations), label=label, x=pos[0] * x_stretch, y=-pos[1] * y_stretch, size=size)
174
+
175
+ # Add edges efficiently
176
+ edge_data = isnad_hadith[['Source', 'Destination', f'{yaxis} Count']].values
177
+ for source, target, count in edge_data:
178
+ net.add_edge(source, target, color=value_to_hex(int(count)), label=f"{count}")
179
+
180
  net.toggle_physics(False)
181
  html = net.generate_html()
182
  html = html.replace("'", "\"")