FDSRashid commited on
Commit
02d71bd
·
verified ·
1 Parent(s): f7c5378

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +45 -5
app.py CHANGED
@@ -106,7 +106,7 @@ def visualize_isnad(taraf_num, yaxis):
106
  else:
107
  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]}')
108
  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]}')
109
- net.add_edge(source, target, color = value_to_hex(int(isnad['Hadith Count'].to_list()[0])), label = f"{isnad['Hadith Count'].to_list()[0]}")
110
  net.barnes_hut(gravity=-5000, central_gravity=0.3, spring_length=200)
111
  html = net.generate_html()
112
  html = html.replace("'", "\"")
@@ -120,14 +120,46 @@ def taraf_booknum(taraf_num):
120
  taraf = matn_info[matn_info['taraf_ID'] == taraf_num]
121
  return taraf[['matn', 'Book ID', 'Hadith Number']]
122
 
123
- # def visualize_subTaraf(df):
124
- # df['bookid_hadithid'] = df.apply(lambda x : )
125
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
126
 
127
 
128
  with gr.Blocks() as demo:
129
  with gr.Tab("Whole Taraf Visualizer"):
130
- Yaxis = gr.Dropdown(choices = ['Tarafs', 'Hadiths', 'Isnads', 'Books'], value = 'Tarafs', label = 'Variable to Display', info = 'Choose the variable to visualize.')
131
  taraf_number = gr.Slider(1,taraf_max , value=10000, label="Taraf", info="Choose the Taraf to Input", step = 1)
132
  btn = gr.Button('Submit')
133
  btn.click(fn = visualize_isnad, inputs = [taraf_number, Yaxis], outputs = [gr.HTML(), gr.DataFrame()])
@@ -135,4 +167,12 @@ with gr.Blocks() as demo:
135
  taraf_num = gr.Slider(1,taraf_max , value=10000, label="Taraf", info="Choose the Taraf to Input", step = 1)
136
  btn_num = gr.Button('Retrieve')
137
  btn_num.click(fn=taraf_booknum, inputs = [taraf_num], outputs= [gr.DataFrame()])
 
 
 
 
 
 
 
 
138
  demo.launch()
 
106
  else:
107
  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]}')
108
  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]}')
109
+ net.add_edge(source, target, color = value_to_hex(int(isnad[f'{yaxis} Count'].to_list()[0])), label = f"{isnad[f'{yaxis} Count'].to_list()[0]}")
110
  net.barnes_hut(gravity=-5000, central_gravity=0.3, spring_length=200)
111
  html = net.generate_html()
112
  html = html.replace("'", "\"")
 
120
  taraf = matn_info[matn_info['taraf_ID'] == taraf_num]
121
  return taraf[['matn', 'Book ID', 'Hadith Number']]
122
 
123
+ def visualize_subTaraf(df):
124
+ df['bookid_hadithid'] = df['Book ID'].astype(str) + '_' + df['Hadith Number'].astype(str)
125
+ hadith = matn_info[matn_info['bookid_hadithid'].isin(df['bookid_hadithid'])]
126
+ taraf_hadith_split = [i.split('_') for i in hadith['bookid_hadithid'].to_list()]
127
+ hadith_cleaned = isnad_info['Hadiths Cleaned'].apply(lambda x: any(i in x for i in taraf_hadith_split))
128
+ isnad_hadith = isnad_info[hadith_cleaned][['Source', 'Destination']]
129
+ narrators = isnad_hadith.applymap(lambda x: narrator_bios[narrator_bios['Rawi ID'] == int(x)]['Famous Name'].to_list()).rename(columns={"Source": "Teacher", "Destination": "Student"})
130
+ isnad_hadith["Student"] = narrators['Student']
131
+ isnad_hadith["Teacher"] = narrators['Teacher']
132
+ filtered = isnad_hadith[(isnad_hadith['Teacher'].apply(lambda x: len(x)) == 1) & (isnad_hadith['Student'].apply(lambda x: len(x)) == 1)]
133
+ filtered['Student'] = filtered['Student'].apply(lambda x: x[0])
134
+ filtered['Teacher'] = filtered['Teacher'].apply(lambda x: x[0])
135
+ net = Network(directed =True)
136
+ for _, row in filtered.iterrows():
137
+ source = row['Teacher']
138
+ target = row['Student']
139
+ teacher_info = narrator_bios[narrator_bios['Rawi ID'] == int(row['Source'])]
140
+ student_info = narrator_bios[narrator_bios['Rawi ID'] == int(row['Destination'])]
141
+ isnad = isnad_info[(isnad_info['Source'] == row['Source']) & (isnad_info['Destination'] == row['Destination'])]
142
+ teacher_narrations = teacher_info['Number of Narrations'].to_list()[0]
143
+ student_narrations = student_info['Number of Narrations'].to_list()[0]
144
+ if row['Source'] == '99999':
145
+ net.add_node(source, font = {'size':50, 'color': 'Black'}, color = '#000000')
146
+ else:
147
+ 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]}')
148
+ 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]}')
149
+ net.add_edge(source, target, color = value_to_hex(int(isnad[f'{yaxis} Count'].to_list()[0])), label = f"{isnad[f'{yaxis} Count'].to_list()[0]}")
150
+ net.barnes_hut(gravity=-5000, central_gravity=0.3, spring_length=200)
151
+ html = net.generate_html()
152
+ html = html.replace("'", "\"")
153
+ return f"""<iframe style="width: 100%; height: 600px;margin:0 auto" name="result" allow="midi; geolocation; microphone; camera;
154
+ display-capture; encrypted-media;" sandbox="allow-modals allow-forms
155
+ allow-scripts allow-same-origin allow-popups
156
+ allow-top-navigation-by-user-activation allow-downloads" allowfullscreen=""
157
+ allowpaymentrequest="" frameborder="0" srcdoc='{html}'></iframe>"""
158
 
159
 
160
  with gr.Blocks() as demo:
161
  with gr.Tab("Whole Taraf Visualizer"):
162
+ Yaxis = gr.Dropdown(choices = ['Taraf', 'Hadith', 'Isnad', 'Book'], value = 'Taraf', label = 'Variable to Display', info = 'Choose the variable to visualize.')
163
  taraf_number = gr.Slider(1,taraf_max , value=10000, label="Taraf", info="Choose the Taraf to Input", step = 1)
164
  btn = gr.Button('Submit')
165
  btn.click(fn = visualize_isnad, inputs = [taraf_number, Yaxis], outputs = [gr.HTML(), gr.DataFrame()])
 
167
  taraf_num = gr.Slider(1,taraf_max , value=10000, label="Taraf", info="Choose the Taraf to Input", step = 1)
168
  btn_num = gr.Button('Retrieve')
169
  btn_num.click(fn=taraf_booknum, inputs = [taraf_num], outputs= [gr.DataFrame()])
170
+ with gr.Tab('Select Hadith Isnad Visualizer'):
171
+ hadith_selection = gr.Dataframe(
172
+ headers=["Book ID", "Hadith Number"],
173
+ datatype=["number", "number"],
174
+ row_count=5,
175
+ col_count=(2, "fixed"))
176
+ btn_hadith = gr.Button('Search')
177
+ btn_hadith.click(fn=visualize_isnad, inputs=[hadith_selection], outputs=[gr.HTML()])
178
  demo.launch()