FDSRashid commited on
Commit
261d935
·
verified ·
1 Parent(s): 69e22b7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +46 -34
app.py CHANGED
@@ -19,13 +19,15 @@ dataset = load_dataset('FDSRashid/hadith_info',data_files = 'Basic_Edge_Informat
19
 
20
  edge_info = dataset.to_pandas()
21
 
22
- features = Features({'Rawi ID': Value('int32'), 'Famous Name': Value('string'), 'Narrator Rank': Value('string'), 'Number of Narrations': Value('string')})
23
  narrator_bios = load_dataset("FDSRashid/hadith_info", data_files = 'Teacher_Bios.csv', token = Secret_token,features=features )
24
  narrator_bios = narrator_bios['train'].to_pandas()
25
  narrator_bios.loc[49845, 'Narrator Rank'] = 'رسول الله'
26
  narrator_bios.loc[49845, 'Number of Narrations'] = 0
27
  narrator_bios['Number of Narrations'] = narrator_bios['Number of Narrations'].astype(int)
28
  narrator_bios.loc[49845, 'Number of Narrations'] = 443471
 
 
29
 
30
 
31
  features = Features({'matn': Value('string'), 'taraf_ID': Value('string'), 'bookid_hadithid': Value('string')})
@@ -41,7 +43,7 @@ matn_info['taraf_ID'] = matn_info['taraf_ID'].astype(int)
41
  matn_info = matn_info.sort_values('taraf_ID')
42
  tarafs = matn_info['taraf_ID'].unique()
43
  for i, taraf in enumerate(tarafs):
44
- matn_info.loc[matn_info['taraf_ID'] == taraf, 'taraf_ID_New'] = i + 1 # Replace 'a' with 'e' in column 'C' where the condition is met
45
  matn_info['taraf_ID_New'] = matn_info['taraf_ID_New'].astype(int)
46
 
47
 
@@ -59,46 +61,56 @@ def value_to_hex(value):
59
  #edge_info, matn_info, narrator_bios, isnad_info
60
 
61
  def visualize_isnad(taraf_num, yaxis):
62
- taraf_hadith = matn_info[matn_info['taraf_ID_New'] == taraf_num]['bookid_hadithid'].to_list()
63
- taraf_hadith_split = [i.split('_') for i in taraf_hadith]
64
- hadith_cleaned = isnad_info['Hadiths Cleaned'].apply(lambda x: any(i in x for i in taraf_hadith_split) )
65
- isnad_hadith = isnad_info[hadith_cleaned][['Source', 'Destination']]
66
- narrators = isnad_hadith.applymap(lambda x: narrator_bios[narrator_bios['Rawi ID'] == int(x)]['Famous Name'].to_list()).rename(columns={"Source": "Teacher", "Destination": "Student"})
67
- isnad_hadith["Student"] = narrators['Student']
68
- isnad_hadith["Teacher"] = narrators['Teacher']
69
- filtered = isnad_hadith[(isnad_hadith['Teacher'].apply(lambda x: len(x)) == 1) & (isnad_hadith['Student'].apply(lambda x: len(x)) == 1)]
70
- filtered['Student'] = filtered['Student'].apply(lambda x: x[0])
71
- filtered['Teacher'] = filtered['Teacher'].apply(lambda x: x[0])
72
- net = Network(directed =True)
73
- for _, row in filtered.iterrows():
74
- source = row['Teacher']
75
- target = row['Student']
76
- teacher_info = narrator_bios[narrator_bios['Rawi ID'] == int(row['Source'])]
77
- student_info = narrator_bios[narrator_bios['Rawi ID'] == int(row['Destination'])]
78
- isnad = isnad_info[(isnad_info['Source'] == row['Source']) & (isnad_info['Destination'] == row['Destination'])]
79
- teacher_narrations = teacher_info['Number of Narrations'].to_list()[0]
80
- student_narrations = student_info['Number of Narrations'].to_list()[0]
81
- if row['Source'] == '99999':
82
- net.add_node(source, font = {'size':50, 'color': 'Black'}, color = '#000000')
83
- else:
84
- net.add_node(source, font = {'size':30, 'color': 'red'}, color = value_to_hex(teacher_narrations), label = f'{source} \n {teacher_info["Narrator Rank"].to_list()[0]}')
85
- net.add_node(target, font = {'size': 30, 'color': 'red'}, color = value_to_hex(student_narrations), label = f'{target} \n{student_info["Narrator Rank"].to_list()[0]}')
86
- net.add_edge(source, target, color = value_to_hex(int(isnad['Hadith Count'].to_list()[0])), label = f"{isnad['Hadith Count'].to_list()[0]}")
87
- net.barnes_hut(gravity=-5000, central_gravity=0.3, spring_length=200)
88
- html = net.generate_html()
89
- html = html.replace("'", "\"")
90
-
91
 
92
- return f"""<iframe style="width: 100%; height: 600px;margin:0 auto" name="result" allow="midi; geolocation; microphone; camera;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
93
  display-capture; encrypted-media;" sandbox="allow-modals allow-forms
94
  allow-scripts allow-same-origin allow-popups
95
  allow-top-navigation-by-user-activation allow-downloads" allowfullscreen=""
96
- allowpaymentrequest="" frameborder="0" srcdoc='{html}'></iframe>"""
97
 
98
 
99
  with gr.Blocks() as demo:
100
  Yaxis = gr.Dropdown(choices = ['Tarafs', 'Hadiths', 'Isnads', 'Books'], value = 'Tarafs', label = 'Variable to Display', info = 'Choose the variable to visualize.')
101
  taraf_number = gr.Slider(1,taraf_max , value=10000, label="Taraf", info="Choose the Taraf to Input", step = 1)
102
  btn = gr.Button('Submit')
103
- btn.click(fn = visualize_isnad, inputs = [taraf_number, Yaxis], outputs = gr.HTML())
104
  demo.launch()
 
19
 
20
  edge_info = dataset.to_pandas()
21
 
22
+ features = Features({'Rawi ID': Value('int32'), 'Famous Name': Value('string'), 'Narrator Rank': Value('string'), 'Number of Narrations': Value('string'), 'Generation': Value('string')})
23
  narrator_bios = load_dataset("FDSRashid/hadith_info", data_files = 'Teacher_Bios.csv', token = Secret_token,features=features )
24
  narrator_bios = narrator_bios['train'].to_pandas()
25
  narrator_bios.loc[49845, 'Narrator Rank'] = 'رسول الله'
26
  narrator_bios.loc[49845, 'Number of Narrations'] = 0
27
  narrator_bios['Number of Narrations'] = narrator_bios['Number of Narrations'].astype(int)
28
  narrator_bios.loc[49845, 'Number of Narrations'] = 443471
29
+ narrator_bios['Generation'] = narrator_bios['Generation'].replace([None], [-1])
30
+ narrator_bios['Generation'] = narrator_bios['Generation'].astype(int)
31
 
32
 
33
  features = Features({'matn': Value('string'), 'taraf_ID': Value('string'), 'bookid_hadithid': Value('string')})
 
43
  matn_info = matn_info.sort_values('taraf_ID')
44
  tarafs = matn_info['taraf_ID'].unique()
45
  for i, taraf in enumerate(tarafs):
46
+ matn_info.loc[matn_info['taraf_ID'] == taraf, 'taraf_ID_New'] = i + 1 # Replace 'a' with 'e' in column 'C' where the condition is met
47
  matn_info['taraf_ID_New'] = matn_info['taraf_ID_New'].astype(int)
48
 
49
 
 
61
  #edge_info, matn_info, narrator_bios, isnad_info
62
 
63
  def visualize_isnad(taraf_num, yaxis):
64
+ taraf_hadith = matn_info[matn_info['taraf_ID_New'] == taraf_num]['bookid_hadithid'].to_list()
65
+ taraf_matns = matn_info[matn_info['taraf_ID_New'] == taraf_num]['matn'].to_list()
66
+ taraf_hadith_split = [i.split('_') for i in taraf_hadith]
67
+ lst_hadith = []
68
+ for i in range(len(taraf_hadith_split)):
69
+ isnad_in_hadith1 = isnad_info['Hadiths Cleaned'].apply(lambda x: list_hadith_split[i] in x )
70
+ isnad_hadith1 = isnad_info[isnad_in_hadith1][['Source', 'Destination']]
71
+ G = nx.from_pandas_edgelist(isnad_hadith1, source = 'Source', target = 'Destination', create_using = nx.DiGraph())
72
+ node = [int(n) for n, d in G.out_degree() if d == 0][0]
73
+ gen_node = narrator_bios[narrator_bios['Rawi ID']==node]['Generation'].iloc[0]
74
+ name_node = narrator_bios[narrator_bios['Rawi ID']==node]['Famous Name'].iloc[0]
75
+ lst_hadith.append([taraf_matns[i], gen_node, name_node])
76
+ df = pd.DataFrame(lst_hadith, columns = ['Matn', 'Generation', 'Name'])
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
77
 
78
+ hadith_cleaned = isnad_info['Hadiths Cleaned'].apply(lambda x: any(i in x for i in taraf_hadith_split) )
79
+ isnad_hadith = isnad_info[hadith_cleaned][['Source', 'Destination']]
80
+ narrators = isnad_hadith.applymap(lambda x: narrator_bios[narrator_bios['Rawi ID'] == int(x)]['Famous Name'].to_list()).rename(columns={"Source": "Teacher", "Destination": "Student"})
81
+ isnad_hadith["Student"] = narrators['Student']
82
+ isnad_hadith["Teacher"] = narrators['Teacher']
83
+ filtered = isnad_hadith[(isnad_hadith['Teacher'].apply(lambda x: len(x)) == 1) & (isnad_hadith['Student'].apply(lambda x: len(x)) == 1)]
84
+ filtered['Student'] = filtered['Student'].apply(lambda x: x[0])
85
+ filtered['Teacher'] = filtered['Teacher'].apply(lambda x: x[0])
86
+ net = Network(directed =True)
87
+ for _, row in filtered.iterrows():
88
+ source = row['Teacher']
89
+ target = row['Student']
90
+ teacher_info = narrator_bios[narrator_bios['Rawi ID'] == int(row['Source'])]
91
+ student_info = narrator_bios[narrator_bios['Rawi ID'] == int(row['Destination'])]
92
+ isnad = isnad_info[(isnad_info['Source'] == row['Source']) & (isnad_info['Destination'] == row['Destination'])]
93
+ teacher_narrations = teacher_info['Number of Narrations'].to_list()[0]
94
+ student_narrations = student_info['Number of Narrations'].to_list()[0]
95
+ if row['Source'] == '99999':
96
+ net.add_node(source, font = {'size':50, 'color': 'Black'}, color = '#000000')
97
+ else:
98
+ net.add_node(source, font = {'size':30, 'color': 'red'}, color = value_to_hex(teacher_narrations), label = f'{source} \n {teacher_info["Narrator Rank"].to_list()[0]}')
99
+ net.add_node(target, font = {'size': 30, 'color': 'red'}, color = value_to_hex(student_narrations), label = f'{target} \n{student_info["Narrator Rank"].to_list()[0]}')
100
+ net.add_edge(source, target, color = value_to_hex(int(isnad['Hadith Count'].to_list()[0])), label = f"{isnad['Hadith Count'].to_list()[0]}")
101
+ net.barnes_hut(gravity=-5000, central_gravity=0.3, spring_length=200)
102
+ html = net.generate_html()
103
+ html = html.replace("'", "\"")
104
+ return f"""<iframe style="width: 100%; height: 600px;margin:0 auto" name="result" allow="midi; geolocation; microphone; camera;
105
  display-capture; encrypted-media;" sandbox="allow-modals allow-forms
106
  allow-scripts allow-same-origin allow-popups
107
  allow-top-navigation-by-user-activation allow-downloads" allowfullscreen=""
108
+ allowpaymentrequest="" frameborder="0" srcdoc='{html}'></iframe>""" , df
109
 
110
 
111
  with gr.Blocks() as demo:
112
  Yaxis = gr.Dropdown(choices = ['Tarafs', 'Hadiths', 'Isnads', 'Books'], value = 'Tarafs', label = 'Variable to Display', info = 'Choose the variable to visualize.')
113
  taraf_number = gr.Slider(1,taraf_max , value=10000, label="Taraf", info="Choose the Taraf to Input", step = 1)
114
  btn = gr.Button('Submit')
115
+ btn.click(fn = visualize_isnad, inputs = [taraf_number, Yaxis], outputs = [gr.HTML(), gr.DataFrame()])
116
  demo.launch()