FDSRashid commited on
Commit
d0e2dd3
1 Parent(s): b4a7f30

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +105 -0
app.py ADDED
@@ -0,0 +1,105 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from pyvis.network import Network
3
+
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
+
14
+
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
+
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'] = 327512
29
+
30
+
31
+
32
+ edge_info = dataset.to_pandas()
33
+ taraf_info = dataset2.to_pandas()
34
+ min_year = int(taraf_info['Year'].min())
35
+ max_year = int(taraf_info['Year'].max())
36
+ cmap = plt.colormaps['cool']
37
+ def value_to_hex(value):
38
+ rgba_color = cmap(value)
39
+ return "#{:02X}{:02X}{:02X}".format(int(rgba_color[0] * 255), int(rgba_color[1] * 255), int(rgba_color[2] * 255))
40
+
41
+ def subsetEdges(fstyear, lstyear):
42
+ info = taraf_info[(taraf_info['Year'] >= fstyear)& (taraf_info['Year'] <= lstyear)]
43
+ narrators = edge_info[edge_info['Edge_ID'].isin(info['ID'].unique())]
44
+ return narrators
45
+ def splitIsnad(dataframe):
46
+ teacher_student =dataframe['Edge_Name'].str.split(' TO ')
47
+ dataframe['Teacher'] = teacher_student.apply(lambda x: x[0])
48
+ dataframe['Student'] = teacher_student.apply(lambda x: x[1])
49
+ return dataframe
50
+
51
+
52
+ def network_narrator(narrator_id, fst_year, lst_year, yaxis):
53
+ edges = subsetEdges(fst_year, lst_year)
54
+ edges_single = edges[edges['Teacher_ID']==narrator_id | edges['Student_ID']==narrator_id]
55
+ edges_prepped = splitIsnad(edges_single)
56
+ net = Network()
57
+ for _, row in edges_prepped.iterrows():
58
+ source = row['Teacher']
59
+ target = row['Student']
60
+ attribute_value = row[yaxis]
61
+ edge_color = value_to_hex(attribute_value)
62
+ teacher_info = narrator_bios[narrator_bios['Rawi ID'] == row['Teacher_ID']]
63
+ student_info = narrator_bios[narrator_bios['Rawi ID'] == row['Student_ID']]
64
+ teacher_narrations = teacher_info['Number of Narrations'].to_list()[0]
65
+ student_narrations = student_info['Number of Narrations'].to_list()[0]
66
+ net.add_node(source, color=value_to_hex(teacher_narrations), font = {'size':30, 'color': 'orange'}, label = f"{source}\n{teacher_narrations}")
67
+ net.add_node(target, color=value_to_hex(student_narrations), font = {'size': 20, 'color': 'red'}, label = f"{target}\n{student_narrations}")
68
+ net.add_edge(source, target, color=edge_color, value=attribute_value, label = f"{yaxis}:{attribute_value}")
69
+
70
+
71
+ net.barnes_hut(gravity=-3000, central_gravity=0.3, spring_length=200)
72
+ html = net.generate_html()
73
+ html = html.replace("'", "\"")
74
+
75
+ return f"""<iframe style="width: 100%; height: 600px;margin:0 auto" name="result" allow="midi; geolocation; microphone; camera;
76
+ display-capture; encrypted-media;" sandbox="allow-modals allow-forms
77
+ allow-scripts allow-same-origin allow-popups
78
+ allow-top-navigation-by-user-activation allow-downloads" allowfullscreen=""
79
+ allowpaymentrequest="" frameborder="0" srcdoc='{html}'></iframe>"""
80
+
81
+ def narrator_retriever(name):
82
+ return narrator_bios[narrator_bios['Official Name'] == name | narrator_bios['Famous Name'] == name][['Rawi ID', 'Title Name', 'Official Name', 'Famous Name', 'Number of Narrations' ]]
83
+
84
+
85
+
86
+ with gr.Blocks() as demo:
87
+ gr.Markdown("Search Narrators using this tool or Visualize Network of a Narrator")
88
+ with gr.Tab("Search Narrator"):
89
+ text_input = gr.Textbox()
90
+ text_output = gr.DataFrame()
91
+ text_button = gr.Button("Search")
92
+ with gr.Tab("Visualize Network"):
93
+ with gr.Row():
94
+ image_input = gr.Number()
95
+ FirstYear = gr.Slider(min_year, max_year, value = -11, label = 'Begining', info = 'Choose the first year to display Narrators')
96
+ Last_Year = gr.Slider(min_year, max_year, value = 9, label = 'End', info = 'Choose the last year to display Narrators')
97
+ Yaxis = gr.Dropdown(choices = ['Tarafs', 'Hadiths', 'Isnads', 'Books'], value = 'Tarafs', label = 'Variable to Display', info = 'Choose the variable to visualize.')
98
+ image_output = gr.HTML()
99
+ image_button = gr.Button("Visualize!")
100
+
101
+ text_button.click(narrator_retriever, inputs=text_input, outputs=text_output)
102
+ image_button.click(network_narrator, inputs=[image_input, FirstYear, Last_Year, Yaxis], outputs=image_output)
103
+
104
+ demo.launch()
105
+