Update app.py
Browse files
app.py
CHANGED
|
@@ -1,5 +1,4 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from pyvis.network import Network
|
| 3 |
import pyarabic.araby as araby
|
| 4 |
import numpy as np
|
| 5 |
import pandas as pd
|
|
@@ -15,7 +14,6 @@ import matplotlib.pyplot as plt
|
|
| 15 |
Secret_token = os.getenv('HF_Token')
|
| 16 |
|
| 17 |
dataset = load_dataset('FDSRashid/hadith_info',data_files = 'Basic_Edge_Information.csv', token = Secret_token, split = 'train')
|
| 18 |
-
dataset2 = load_dataset('FDSRashid/hadith_info',data_files = 'Taraf_Info.csv', token = Secret_token, split = 'train')
|
| 19 |
|
| 20 |
lst = ['Rawi ID',
|
| 21 |
'Gender',
|
|
@@ -52,27 +50,19 @@ narrator_bios = narrator_bios['train'].to_pandas()
|
|
| 52 |
narrator_bios.loc[49845, 'Narrator Rank'] = 'رسول الله'
|
| 53 |
narrator_bios.loc[49845, 'Number of Narrations'] = 0
|
| 54 |
narrator_bios['Number of Narrations'] = narrator_bios['Number of Narrations'].astype(int)
|
| 55 |
-
narrator_bios.loc[49845, 'Number of Narrations'] =
|
| 56 |
narrator_bios['Generation'] = narrator_bios['Generation'].replace([None], [-1])
|
| 57 |
narrator_bios['Generation'] = narrator_bios['Generation'].astype(int)
|
| 58 |
|
| 59 |
|
| 60 |
edge_info = dataset.to_pandas()
|
| 61 |
-
taraf_info = dataset2.to_pandas()
|
| 62 |
-
min_year = int(taraf_info['Year'].min())
|
| 63 |
-
max_year = int(taraf_info['Year'].max())
|
| 64 |
-
cmap = plt.colormaps['cool']
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
-
def
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
def subsetEdges(fstyear, lstyear):
|
| 73 |
-
info = taraf_info[(taraf_info['Year'] >= fstyear)& (taraf_info['Year'] <= lstyear)]
|
| 74 |
-
narrators = edge_info[edge_info['Edge_ID'].isin(info['ID'].unique())]
|
| 75 |
-
return narrators
|
| 76 |
def splitIsnad(dataframe):
|
| 77 |
teacher_student =dataframe['Edge_Name'].str.split(' TO ')
|
| 78 |
dataframe['Teacher'] = teacher_student.apply(lambda x: x[0])
|
|
@@ -80,36 +70,13 @@ def splitIsnad(dataframe):
|
|
| 80 |
return dataframe
|
| 81 |
|
| 82 |
|
| 83 |
-
def network_narrator(narrator_id
|
| 84 |
-
edges = subsetEdges(fst_year, lst_year)
|
| 85 |
-
edges_single = edges[(edges['Teacher_ID']==narrator_id) | (edges['Student_ID']==narrator_id)]
|
| 86 |
-
edges_prepped = splitIsnad(edges_single)
|
| 87 |
-
net = Network(directed =True)
|
| 88 |
-
for _, row in edges_prepped.iterrows():
|
| 89 |
-
source = row['Teacher']
|
| 90 |
-
target = row['Student']
|
| 91 |
-
attribute_value = row[yaxis]
|
| 92 |
-
edge_color = value_to_hex(attribute_value)
|
| 93 |
-
teacher_info = narrator_bios[narrator_bios['Rawi ID'] == row['Teacher_ID']]
|
| 94 |
-
student_info = narrator_bios[narrator_bios['Rawi ID'] == row['Student_ID']]
|
| 95 |
-
teacher_narrations = teacher_info['Number of Narrations'].to_list()[0]
|
| 96 |
-
student_narrations = student_info['Number of Narrations'].to_list()[0]
|
| 97 |
-
net.add_node(source, color=value_to_hex(teacher_narrations), font = {'size':30, 'color': 'orange'}, label = f"{source}\n{teacher_narrations}")
|
| 98 |
-
net.add_node(target, color=value_to_hex(student_narrations), font = {'size': 20, 'color': 'red'}, label = f"{target}\n{student_narrations}")
|
| 99 |
-
net.add_edge(source, target, color=edge_color, value=attribute_value, label = f"{yaxis}:{attribute_value}")
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
net.barnes_hut(gravity=-3000, central_gravity=0.3, spring_length=200)
|
| 103 |
-
html = net.generate_html()
|
| 104 |
-
html = html.replace("'", "\"")
|
| 105 |
-
|
| 106 |
edge_narrator = edge_info[(edge_info['Teacher_ID'] == narrator_id) | (edge_info['Student_ID'] == narrator_id)]
|
| 107 |
edge_full = splitIsnad(edge_narrator[['Tarafs', 'Hadiths', 'Isnads', 'Edge_Name', 'Books']]).drop(['Edge_Name'], axis=1)
|
| 108 |
-
return
|
| 109 |
-
display-capture; encrypted-media;" sandbox="allow-modals allow-forms
|
| 110 |
-
allow-scripts allow-same-origin allow-popups
|
| 111 |
-
allow-top-navigation-by-user-activation allow-downloads" allowfullscreen=""
|
| 112 |
-
allowpaymentrequest="" frameborder="0" srcdoc='{html}'></iframe>""", edge_full
|
| 113 |
|
| 114 |
def narrator_retriever(name):
|
| 115 |
return narrator_bios[(narrator_bios['Official Name'].apply(lambda x: araby.strip_diacritics(x)).str.contains(araby.strip_diacritics(name))) | (narrator_bios['Famous Name'].apply(lambda x: araby.strip_diacritics(x)).str.contains(araby.strip_diacritics(name))) | (narrator_bios['Rawi ID'].astype(str) == name)]
|
|
@@ -117,7 +84,7 @@ def narrator_retriever(name):
|
|
| 117 |
|
| 118 |
|
| 119 |
with gr.Blocks() as demo:
|
| 120 |
-
gr.Markdown("Search Narrators using this tool or
|
| 121 |
with gr.Tab("Search Narrator"):
|
| 122 |
text_input = gr.Textbox()
|
| 123 |
text_output = gr.DataFrame()
|
|
@@ -125,14 +92,9 @@ with gr.Blocks() as demo:
|
|
| 125 |
text_button.click(narrator_retriever, inputs=text_input, outputs=text_output)
|
| 126 |
|
| 127 |
with gr.Tab("Visualize Network"):
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
Last_Year = gr.Slider(min_year, max_year, value = 9, label = 'End', info = 'Choose the last year to display Narrators')
|
| 132 |
-
Yaxis = gr.Dropdown(choices = ['Tarafs', 'Hadiths', 'Isnads', 'Books'], value = 'Tarafs', label = 'Variable to Display', info = 'Choose the variable to visualize.')
|
| 133 |
-
image_output = gr.HTML()
|
| 134 |
-
image_button = gr.Button("Visualize!")
|
| 135 |
-
image_button.click(network_narrator, inputs=[image_input, FirstYear, Last_Year, Yaxis], outputs=[image_output, gr.DataFrame()])
|
| 136 |
|
| 137 |
|
| 138 |
|
|
|
|
| 1 |
import gradio as gr
|
|
|
|
| 2 |
import pyarabic.araby as araby
|
| 3 |
import numpy as np
|
| 4 |
import pandas as pd
|
|
|
|
| 14 |
Secret_token = os.getenv('HF_Token')
|
| 15 |
|
| 16 |
dataset = load_dataset('FDSRashid/hadith_info',data_files = 'Basic_Edge_Information.csv', token = Secret_token, split = 'train')
|
|
|
|
| 17 |
|
| 18 |
lst = ['Rawi ID',
|
| 19 |
'Gender',
|
|
|
|
| 50 |
narrator_bios.loc[49845, 'Narrator Rank'] = 'رسول الله'
|
| 51 |
narrator_bios.loc[49845, 'Number of Narrations'] = 0
|
| 52 |
narrator_bios['Number of Narrations'] = narrator_bios['Number of Narrations'].astype(int)
|
| 53 |
+
narrator_bios.loc[49845, 'Number of Narrations'] = 22000
|
| 54 |
narrator_bios['Generation'] = narrator_bios['Generation'].replace([None], [-1])
|
| 55 |
narrator_bios['Generation'] = narrator_bios['Generation'].astype(int)
|
| 56 |
|
| 57 |
|
| 58 |
edge_info = dataset.to_pandas()
|
|
|
|
|
|
|
|
|
|
|
|
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
+
# def subsetEdges(fstyear, lstyear):
|
| 63 |
+
# info = taraf_info[(taraf_info['Year'] >= fstyear)& (taraf_info['Year'] <= lstyear)]
|
| 64 |
+
# narrators = edge_info[edge_info['Edge_ID'].isin(info['ID'].unique())]
|
| 65 |
+
# return narrators
|
|
|
|
|
|
|
|
|
|
|
|
|
| 66 |
def splitIsnad(dataframe):
|
| 67 |
teacher_student =dataframe['Edge_Name'].str.split(' TO ')
|
| 68 |
dataframe['Teacher'] = teacher_student.apply(lambda x: x[0])
|
|
|
|
| 70 |
return dataframe
|
| 71 |
|
| 72 |
|
| 73 |
+
def network_narrator(narrator_id):
|
| 74 |
+
# edges = subsetEdges(fst_year, lst_year)
|
| 75 |
+
# edges_single = edges[(edges['Teacher_ID']==narrator_id) | (edges['Student_ID']==narrator_id)]
|
| 76 |
+
# edges_prepped = splitIsnad(edges_single)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 77 |
edge_narrator = edge_info[(edge_info['Teacher_ID'] == narrator_id) | (edge_info['Student_ID'] == narrator_id)]
|
| 78 |
edge_full = splitIsnad(edge_narrator[['Tarafs', 'Hadiths', 'Isnads', 'Edge_Name', 'Books']]).drop(['Edge_Name'], axis=1)
|
| 79 |
+
return edge_full
|
|
|
|
|
|
|
|
|
|
|
|
|
| 80 |
|
| 81 |
def narrator_retriever(name):
|
| 82 |
return narrator_bios[(narrator_bios['Official Name'].apply(lambda x: araby.strip_diacritics(x)).str.contains(araby.strip_diacritics(name))) | (narrator_bios['Famous Name'].apply(lambda x: araby.strip_diacritics(x)).str.contains(araby.strip_diacritics(name))) | (narrator_bios['Rawi ID'].astype(str) == name)]
|
|
|
|
| 84 |
|
| 85 |
|
| 86 |
with gr.Blocks() as demo:
|
| 87 |
+
gr.Markdown("Search Narrators using this tool or Retrieve Transmissions involving Narrator")
|
| 88 |
with gr.Tab("Search Narrator"):
|
| 89 |
text_input = gr.Textbox()
|
| 90 |
text_output = gr.DataFrame()
|
|
|
|
| 92 |
text_button.click(narrator_retriever, inputs=text_input, outputs=text_output)
|
| 93 |
|
| 94 |
with gr.Tab("Visualize Network"):
|
| 95 |
+
image_input = gr.Number()
|
| 96 |
+
image_button = gr.Button("Retrieve!")
|
| 97 |
+
image_button.click(network_narrator, inputs=[image_input], outputs=[gr.DataFrame(wrap=True)])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 98 |
|
| 99 |
|
| 100 |
|