FDSRashid commited on
Commit
87bc78a
·
verified ·
1 Parent(s): 9bc4ab4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +14 -52
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'] = 327512
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 value_to_hex(value):
69
- rgba_color = cmap(value)
70
- return "#{:02X}{:02X}{:02X}".format(int(rgba_color[0] * 255), int(rgba_color[1] * 255), int(rgba_color[2] * 255))
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, fst_year, lst_year, yaxis):
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 f"""<iframe style="width: 100%; height: 600px;margin:0 auto" name="result" allow="midi; geolocation; microphone; camera;
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 Visualize Network of a Narrator")
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
- with gr.Row():
129
- image_input = gr.Number()
130
- FirstYear = gr.Slider(min_year, max_year, value = -11, label = 'Begining', info = 'Choose the first year to display Narrators')
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