Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,89 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from pyvis.network import Network
|
| 3 |
+
import networkx as nx
|
| 4 |
+
import numpy as np
|
| 5 |
+
import pandas as pd
|
| 6 |
+
import os
|
| 7 |
+
from datasets import load_dataset
|
| 8 |
+
from datasets import Features
|
| 9 |
+
from datasets import Value
|
| 10 |
+
from datasets import Dataset
|
| 11 |
+
import matplotlib.pyplot as plt
|
| 12 |
+
|
| 13 |
+
import re
|
| 14 |
+
|
| 15 |
+
pattern = r'"(.*?)"'
|
| 16 |
+
|
| 17 |
+
dataset = load_dataset('FDSRashid/hadith_info',data_files = 'Basic_Edge_Information.csv', token = Secret_token, split = 'train')
|
| 18 |
+
|
| 19 |
+
edge_info = dataset.to_pandas()
|
| 20 |
+
|
| 21 |
+
features = Features({'Rawi ID': Value('int32'), 'Famous Name': Value('string'), 'Narrator Rank': Value('string'), 'Number of Narrations': Value('string')})
|
| 22 |
+
narrator_bios = load_dataset("FDSRashid/hadith_info", data_files = 'Teacher_Bios.csv', token = Secret_token,features=features )
|
| 23 |
+
narrator_bios = narrator_bios['train'].to_pandas()
|
| 24 |
+
narrator_bios.loc[49845, 'Narrator Rank'] = 'رسول الله'
|
| 25 |
+
narrator_bios.loc[49845, 'Number of Narrations'] = 0
|
| 26 |
+
narrator_bios['Number of Narrations'] = narrator_bios['Number of Narrations'].astype(int)
|
| 27 |
+
narrator_bios.loc[49845, 'Number of Narrations'] = 443471
|
| 28 |
+
|
| 29 |
+
|
| 30 |
+
features = Features({'matn': Value('string'), 'taraf_ID': Value('string'), 'bookid_hadithid': Value('string')})
|
| 31 |
+
|
| 32 |
+
dataset = load_dataset("FDSRashid/hadith_info", data_files = 'All_Matns.csv', features = features)
|
| 33 |
+
matn_info = dataset['train'].to_pandas()
|
| 34 |
+
isnad_info = load_dataset('FDSRashid/hadith_info', data_files = 'isnad_info.csv', split = 'train').to_pandas()
|
| 35 |
+
isnad_info['Hadiths Cleaned'] = isnad_info['Hadiths'].apply(lambda x: [re.findall(pattern, string)[0].split("_") for string in x[1:-1].split(',')])
|
| 36 |
+
|
| 37 |
+
tarafs = matn_info.unique().tolist()
|
| 38 |
+
|
| 39 |
+
cmap = plt.colormaps['cool']
|
| 40 |
+
def value_to_hex(value):
|
| 41 |
+
rgba_color = cmap(value)
|
| 42 |
+
return "#{:02X}{:02X}{:02X}".format(int(rgba_color[0] * 255), int(rgba_color[1] * 255), int(rgba_color[2] * 255))
|
| 43 |
+
|
| 44 |
+
#edge_info, matn_info, narrator_bios, isnad_info
|
| 45 |
+
|
| 46 |
+
def visualize_isnad(taraf_num, yaxis):
|
| 47 |
+
taraf_hadith = matn_info[matn_info['taraf_ID'] == taraf_num]['bookid_hadithid'].to_list()
|
| 48 |
+
taraf_hadith_split = [i.split('_') for i in taraf_hadith]
|
| 49 |
+
hadith_cleaned = isnad_info['Hadiths Cleaned'].apply(lambda x: any(i in x for i in taraf_hadith_split) )
|
| 50 |
+
isnad_hadith = isnad_info[hadith_cleaned][['Source', 'Destination']]
|
| 51 |
+
narrators = isnad_hadith.applymap(lambda x: narrator_bios[narrator_bios['Rawi ID'] == int(x)]['Famous Name'].to_list()).rename(columns={"Source": "Teacher", "Destination": "Student"})
|
| 52 |
+
isnad_hadith["Student"] = narrators['Student']
|
| 53 |
+
isnad_hadith["Teacher"] = narrators['Teacher']
|
| 54 |
+
filtered = isnad_hadith[(isnad_hadith['Teacher'].apply(lambda x: len(x)) == 1) & (isnad_hadith['Student'].apply(lambda x: len(x)) == 1)]
|
| 55 |
+
filtered['Student'] = filtered['Student'].apply(lambda x: x[0])
|
| 56 |
+
filtered['Teacher'] = filtered['Teacher'].apply(lambda x: x[0])
|
| 57 |
+
net = Network(directed =True)
|
| 58 |
+
for _, row in filtered.iterrows():
|
| 59 |
+
source = row['Teacher']
|
| 60 |
+
target = row['Student']
|
| 61 |
+
teacher_info = narrator_bios[narrator_bios['Rawi ID'] == int(row['Source'])]
|
| 62 |
+
student_info = narrator_bios[narrator_bios['Rawi ID'] == int(row['Destination'])]
|
| 63 |
+
isnad = isnad_info[(isnad_info['Source'] == row['Source']) & (isnad_info['Destination'] == row['Destination'])]
|
| 64 |
+
teacher_narrations = teacher_info['Number of Narrations'].to_list()[0]
|
| 65 |
+
student_narrations = student_info['Number of Narrations'].to_list()[0]
|
| 66 |
+
if row['Source'] == '99999':
|
| 67 |
+
net.add_node(source, font = {'size':50, 'color': 'Black'}, color = '#000000')
|
| 68 |
+
else:
|
| 69 |
+
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]}')
|
| 70 |
+
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]}')
|
| 71 |
+
net.add_edge(source, target, color = value_to_hex(int(isnad['Hadith Count'].to_list()[0])), label = f"{isnad['Hadith Count'].to_list()[0]}")
|
| 72 |
+
net.barnes_hut(gravity=-5000, central_gravity=0.3, spring_length=200)
|
| 73 |
+
html = net.generate_html()
|
| 74 |
+
html = html.replace("'", "\"")
|
| 75 |
+
|
| 76 |
+
|
| 77 |
+
return f"""<iframe style="width: 100%; height: 600px;margin:0 auto" name="result" allow="midi; geolocation; microphone; camera;
|
| 78 |
+
display-capture; encrypted-media;" sandbox="allow-modals allow-forms
|
| 79 |
+
allow-scripts allow-same-origin allow-popups
|
| 80 |
+
allow-top-navigation-by-user-activation allow-downloads" allowfullscreen=""
|
| 81 |
+
allowpaymentrequest="" frameborder="0" srcdoc='{html}'></iframe>"""
|
| 82 |
+
|
| 83 |
+
|
| 84 |
+
with gr.Blocks() as demo:
|
| 85 |
+
Yaxis = gr.Dropdown(choices = ['Tarafs', 'Hadiths', 'Isnads', 'Books'], value = 'Tarafs', label = 'Variable to Display', info = 'Choose the variable to visualize.')
|
| 86 |
+
taraf_number = gr.Dropdown(choices = tarafs, value = 10000, label = 'Taraf Number')
|
| 87 |
+
btn = gr.Button('Submit')
|
| 88 |
+
btn.click(fn = visualize_isnad, inputs = [taraf_number, Yaxis], outputs = gr.HTML())
|
| 89 |
+
demo.launch()
|