added tool to view isnad of taraf that involves a narrator
Browse files
app.py
CHANGED
@@ -304,6 +304,85 @@ def visualize_hadith_isnad(df, yaxis):
|
|
304 |
allowpaymentrequest="" frameborder="0" srcdoc='{html}'></iframe>"""
|
305 |
|
306 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
307 |
with gr.Blocks() as demo:
|
308 |
with gr.Tab("Whole Taraf Visualizer"):
|
309 |
Yaxis = gr.Dropdown(choices = ['Taraf', 'Hadith', 'Isnad', 'Book'], value = 'Taraf', label = 'Variable to Display', info = 'Choose the variable to visualize.')
|
@@ -329,4 +408,10 @@ with gr.Blocks() as demo:
|
|
329 |
col_count=(2, "fixed"))
|
330 |
btn_hadith = gr.Button('Visualize')
|
331 |
btn_hadith.click(fn=visualize_hadith_isnad, inputs=[hadith_selection, yyaxis], outputs=[gr.HTML()])
|
|
|
|
|
|
|
|
|
|
|
|
|
332 |
demo.launch()
|
|
|
304 |
allowpaymentrequest="" frameborder="0" srcdoc='{html}'></iframe>"""
|
305 |
|
306 |
|
307 |
+
def visualize_narrator_taraf(taraf_num, narrator, Yaxis):
|
308 |
+
taraf = matn_info[matn_info['taraf_ID'] == taraf_num]
|
309 |
+
hadith_cleaned = isnad_info['Tarafs Cleaned'].apply(lambda x: taraf_num in x)
|
310 |
+
isnad_hadith = isnad_info[hadith_cleaned]
|
311 |
+
isnad_hadith['Teacher'] = isnad_hadith['Source'].apply(lambda x: narrator_bios[narrator_bios['Rawi ID'].astype(int) == int(x)]['Famous Name'].to_list())
|
312 |
+
isnad_hadith['Student'] = isnad_hadith['Destination'].apply(lambda x: narrator_bios[narrator_bios['Rawi ID'].astype(int) == int(x)]['Famous Name'].to_list())
|
313 |
+
isnad_hadith['Teacher'] = isnad_hadith['Teacher'].apply(lambda x: x[0] if len(x)==1 else 'ููุงู')
|
314 |
+
isnad_hadith['Student'] = isnad_hadith['Student'].apply(lambda x: x[0] if len(x)==1 else 'ููุงู')
|
315 |
+
taraf_hadith = taraf['bookid_hadithid'].to_list()
|
316 |
+
taraf_hadith_split = [i.split('_') for i in taraf_hadith]
|
317 |
+
|
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 |
+
nodes = list(G.nodes)
|
322 |
+
nodes = [node for node in nodes if node not in list(nx.ancestors(G, narrator)) + list(nx.descendants(G, narrator)) and node !=narrator]
|
323 |
+
isnad_pos = nx.nx_agraph.graphviz_layout(G, prog='dot')
|
324 |
+
|
325 |
+
matns_with_narrator = []
|
326 |
+
for i in range(len(taraf_hadith_split)):
|
327 |
+
isnad_in_hadith1 = isnad_hadith['Hadiths Cleaned'].apply(lambda x: taraf_hadith_split[i] in x )
|
328 |
+
isnad_hadith1 = isnad_hadith[isnad_in_hadith1]
|
329 |
+
G = nx.from_pandas_edgelist(isnad_hadith1, source = 'Source', target = 'Destination', create_using = nx.DiGraph())
|
330 |
+
if narrator in list(G.nodes):
|
331 |
+
matns_with_narrator.append(taraf_hadith[i])
|
332 |
+
|
333 |
+
narrator_matn_info = taraf[taraf['bookid_hadithid'].isin(matns_with_narrator)]
|
334 |
+
|
335 |
+
x_stretch = 4
|
336 |
+
y_stretch = 4
|
337 |
+
net = Network(directed =True)
|
338 |
+
|
339 |
+
for node, pos in isnad_pos.items():
|
340 |
+
node_info = narrator_bios[narrator_bios['Rawi ID'] == int(node)]
|
341 |
+
|
342 |
+
student_narrations = node_info['Number of Narrations'].to_list()
|
343 |
+
if len(student_narrations):
|
344 |
+
student_narrations = student_narrations[0]
|
345 |
+
else:
|
346 |
+
student_narrations = 1
|
347 |
+
|
348 |
+
student_gen = node_info['Generation'].to_list()
|
349 |
+
if len(student_gen):
|
350 |
+
student_gen = student_gen[0]
|
351 |
+
else:
|
352 |
+
student_gen = -1
|
353 |
+
|
354 |
+
student_rank = node_info["Narrator Rank"].to_list()
|
355 |
+
if len(student_rank):
|
356 |
+
student_rank = student_rank[0]
|
357 |
+
else:
|
358 |
+
student_rank = 'ููุงู'
|
359 |
+
|
360 |
+
node_name = node_info['Famous Name'].to_list()
|
361 |
+
if len(node_name):
|
362 |
+
node_name = node_name[0]
|
363 |
+
else:
|
364 |
+
node_name = 'ููุงู'
|
365 |
+
|
366 |
+
if node == '99999':
|
367 |
+
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)
|
368 |
+
else:
|
369 |
+
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)
|
370 |
+
for edge in G.edges:
|
371 |
+
row = isnad_hadith[(isnad_hadith['Source'] == edge[0]) & (isnad_hadith['Destination'] == edge[1])]
|
372 |
+
source = row['Source']
|
373 |
+
target = row['Destination']
|
374 |
+
net.add_edge(source, target, color = value_to_hex(int(row[f'{yaxis} Count'])), label = f"{row[f'{yaxis} Count']}")
|
375 |
+
net.toggle_physics(False)
|
376 |
+
html = net.generate_html()
|
377 |
+
html = html.replace("'", "\"")
|
378 |
+
return f"""<iframe style="width: 100%; height: 600px;margin:0 auto" name="result" allow="midi; geolocation; microphone; camera;
|
379 |
+
display-capture; encrypted-media;" sandbox="allow-modals allow-forms
|
380 |
+
allow-scripts allow-same-origin allow-popups
|
381 |
+
allow-top-navigation-by-user-activation allow-downloads" allowfullscreen=""
|
382 |
+
allowpaymentrequest="" frameborder="0" srcdoc='{html}'></iframe>""" , narrator_matn_info[['matn', 'Book_Name', 'Author', 'Book_ID', 'Hadith Number']]
|
383 |
+
|
384 |
+
|
385 |
+
|
386 |
with gr.Blocks() as demo:
|
387 |
with gr.Tab("Whole Taraf Visualizer"):
|
388 |
Yaxis = gr.Dropdown(choices = ['Taraf', 'Hadith', 'Isnad', 'Book'], value = 'Taraf', label = 'Variable to Display', info = 'Choose the variable to visualize.')
|
|
|
408 |
col_count=(2, "fixed"))
|
409 |
btn_hadith = gr.Button('Visualize')
|
410 |
btn_hadith.click(fn=visualize_hadith_isnad, inputs=[hadith_selection, yyaxis], outputs=[gr.HTML()])
|
411 |
+
with gr.Tab('Taraf Narrator Isnad Visualizer'):
|
412 |
+
Yaxis = gr.Dropdown(choices = ['Taraf', 'Hadith', 'Isnad', 'Book'], value = 'Taraf', label = 'Variable to Display', info = 'Choose the variable to visualize.')
|
413 |
+
taraf_number = gr.Slider(1,taraf_max , value=10000, label="Taraf", info="Choose the Taraf to Input", step = 1)
|
414 |
+
narr = gr.Textbox(label='Narrator', info='Choose a Narrator (Refer to full isnad from previous tab)')
|
415 |
+
btn_narr = gr.Button('Visualize')
|
416 |
+
btn_narr.click(fn=visualize_narrator_taraf, inputs=[taraf_number, narr, Yaxis], outputs=[gr.HTML(), gr.DataFrame(wrap=True)])
|
417 |
demo.launch()
|