Update app.py
Browse files
app.py
CHANGED
@@ -9,7 +9,6 @@ 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'"(.*?)"'
|
@@ -61,7 +60,6 @@ def value_to_hex(value):
|
|
61 |
rgba_color = cmap(value)
|
62 |
return "#{:02X}{:02X}{:02X}".format(int(rgba_color[0] * 255), int(rgba_color[1] * 255), int(rgba_color[2] * 255))
|
63 |
|
64 |
-
#edge_info, matn_info, narrator_bios, isnad_info
|
65 |
|
66 |
def visualize_isnad(taraf_num, yaxis):
|
67 |
taraf_hadith = matn_info[matn_info['taraf_ID'] == taraf_num]['bookid_hadithid'].to_list()
|
@@ -86,8 +84,6 @@ def visualize_isnad(taraf_num, yaxis):
|
|
86 |
lst_hadith.append([taraf_matns[i], gen_node, name_node, taraf_book[i], taraf_author[i], taraf_hadith_number[i], str(n), str(i)])
|
87 |
df = pd.DataFrame(lst_hadith, columns = ['Matn', 'Generation', 'Name', 'Book_Name', 'Author', 'Book Hadith Number', 'End Transmitter ID', 'Hadith Number'])
|
88 |
|
89 |
-
#hadith_cleaned = isnad_info['Hadiths Cleaned'].apply(lambda x: any(i in x for i in taraf_hadith_split) )
|
90 |
-
|
91 |
isnad_hadith['Teacher'] = isnad_hadith['Source'].apply(lambda x: narrator_bios[narrator_bios['Rawi ID'].astype(int) == int(x)]['Famous Name'].to_list())
|
92 |
isnad_hadith['Student'] = isnad_hadith['Destination'].apply(lambda x: narrator_bios[narrator_bios['Rawi ID'].astype(int) == int(x)]['Famous Name'].to_list())
|
93 |
isnad_hadith['Teacher'] = isnad_hadith['Teacher'].apply(lambda x: x[0] if len(x)==1 else 'ููุงู')
|
@@ -142,65 +138,6 @@ def visualize_isnad(taraf_num, yaxis):
|
|
142 |
allow-top-navigation-by-user-activation allow-downloads" allowfullscreen=""
|
143 |
allowpaymentrequest="" frameborder="0" srcdoc='{html}'></iframe>""" , df
|
144 |
|
145 |
-
# for _, row in isnad_hadith.iterrows():
|
146 |
-
# source = row['Source']
|
147 |
-
# target = row['Destination']
|
148 |
-
# teacher_info = narrator_bios[narrator_bios['Rawi ID'] == int(row['Source'])]
|
149 |
-
# student_info = narrator_bios[narrator_bios['Rawi ID'] == int(row['Destination'])]
|
150 |
-
|
151 |
-
# teacher_narrations = teacher_info['Number of Narrations'].to_list()
|
152 |
-
# if len(teacher_narrations):
|
153 |
-
# teacher_narrations = teacher_narrations[0]
|
154 |
-
# else:
|
155 |
-
# teacher_narrations = row['Hadith Count']
|
156 |
-
# student_narrations = student_info['Number of Narrations'].to_list()
|
157 |
-
# if len(student_narrations):
|
158 |
-
# student_narrations = student_narrations[0]
|
159 |
-
# else:
|
160 |
-
# student_narrations = row['Hadith Count']
|
161 |
-
|
162 |
-
# teacher_gen = teacher_info['Generation'].to_list()
|
163 |
-
# if len(teacher_gen):
|
164 |
-
# teacher_gen = teacher_gen[0]
|
165 |
-
# else:
|
166 |
-
# teacher_gen = -1
|
167 |
-
# student_gen = student_info['Generation'].to_list()
|
168 |
-
# if len(student_gen):
|
169 |
-
# student_gen = student_gen[0]
|
170 |
-
# else:
|
171 |
-
# student_gen = -1
|
172 |
-
# teacher_rank = teacher_info["Narrator Rank"].to_list()
|
173 |
-
# if len(teacher_rank):
|
174 |
-
# teacher_rank = teacher_rank[0]
|
175 |
-
# else:
|
176 |
-
# teacher_rank = 'ููุงู'
|
177 |
-
|
178 |
-
# student_rank = student_info["Narrator Rank"].to_list()
|
179 |
-
# if len(student_rank):
|
180 |
-
# student_rank = student_rank[0]
|
181 |
-
# else:
|
182 |
-
# student_rank = 'ููุงู'
|
183 |
-
# if row['Source'] == '99999':
|
184 |
-
# net.add_node(source, font = {'size':50, 'color': 'Black'}, color = '#000000', label = f'{row["Teacher"]}')
|
185 |
-
# elif source in end_nodes:
|
186 |
-
# end_matn_info = df[df["End Transmitter ID"] == source]
|
187 |
-
# net.add_node(source, font = {'size':30, 'color': 'red'}, color = value_to_hex(teacher_narrations), label = f'{row["Teacher"]} \n {teacher_rank} \n ID: {row["Source"]} - Gen {teacher_gen} \n Hadith {" ".join(end_matn_info["Hadith Number"].tolist())}')
|
188 |
-
# else:
|
189 |
-
# net.add_node(source, font = {'size':30, 'color': 'red'}, color = value_to_hex(teacher_narrations), label = f'{row["Teacher"]} \n {teacher_rank} \n ID: {row["Source"]} - Gen {teacher_gen}')
|
190 |
-
# if target in end_nodes:
|
191 |
-
# end_matn_info = df[df["End Transmitter ID"] == target]
|
192 |
-
# net.add_node(target, font = {'size': 30, 'color': 'red'}, color = value_to_hex(student_narrations), label = f'{row["Student"]} \n{student_rank} \n ID: {row["Destination"]} - Gen {student_gen} \n Hadith {" ".join(end_matn_info["Hadith Number"].tolist())}')
|
193 |
-
# else:
|
194 |
-
# net.add_node(target, font = {'size': 30, 'color': 'red'}, color = value_to_hex(student_narrations), label = f'{row["Student"]} \n{student_rank} \n ID: {row["Destination"]} - Gen {student_gen}')
|
195 |
-
# net.add_edge(source, target, color = value_to_hex(int(row[f'{yaxis} Count'])), label = f"{row[f'{yaxis} Count']}")
|
196 |
-
# net.barnes_hut(gravity=-5000, central_gravity=0.3, spring_length=200)
|
197 |
-
# html = net.generate_html()
|
198 |
-
# html = html.replace("'", "\"")
|
199 |
-
# return f"""<iframe style="width: 100%; height: 600px;margin:0 auto" name="result" allow="midi; geolocation; microphone; camera;
|
200 |
-
# display-capture; encrypted-media;" sandbox="allow-modals allow-forms
|
201 |
-
# allow-scripts allow-same-origin allow-popups
|
202 |
-
# allow-top-navigation-by-user-activation allow-downloads" allowfullscreen=""
|
203 |
-
# allowpaymentrequest="" frameborder="0" srcdoc='{html}'></iframe>""" , df
|
204 |
|
205 |
def taraf_booknum(taraf_num):
|
206 |
taraf = matn_info[matn_info['taraf_ID'] == taraf_num]
|
@@ -216,59 +153,55 @@ def visualize_subTaraf(df, yaxis):
|
|
216 |
isnad_hadith['Student'] = isnad_hadith['Destination'].apply(lambda x: narrator_bios[narrator_bios['Rawi ID'].astype(int) == int(x)]['Famous Name'].to_list())
|
217 |
isnad_hadith['Teacher'] = isnad_hadith['Teacher'].apply(lambda x: x[0] if len(x)==1 else 'ููุงู')
|
218 |
isnad_hadith['Student'] = isnad_hadith['Student'].apply(lambda x: x[0] if len(x)==1 else 'ููุงู')
|
|
|
|
|
|
|
|
|
|
|
|
|
219 |
net = Network(directed =True)
|
220 |
-
|
221 |
-
|
222 |
-
|
223 |
-
|
224 |
-
student_info = narrator_bios[narrator_bios['Rawi ID'] == int(row['Destination'])]
|
225 |
-
|
226 |
-
teacher_narrations = teacher_info['Number of Narrations'].to_list()
|
227 |
-
if len(teacher_narrations):
|
228 |
-
teacher_narrations = teacher_narrations[0]
|
229 |
-
else:
|
230 |
-
teacher_narrations = row['Hadith Count']
|
231 |
-
student_narrations = student_info['Number of Narrations'].to_list()
|
232 |
if len(student_narrations):
|
233 |
student_narrations = student_narrations[0]
|
234 |
else:
|
235 |
-
student_narrations =
|
236 |
-
|
237 |
-
teacher_gen = teacher_info['Generation'].to_list()
|
238 |
-
if len(teacher_gen):
|
239 |
-
teacher_gen = teacher_gen[0]
|
240 |
-
else:
|
241 |
-
teacher_gen = -1
|
242 |
-
student_gen = student_info['Generation'].to_list()
|
243 |
if len(student_gen):
|
244 |
student_gen = student_gen[0]
|
245 |
else:
|
246 |
student_gen = -1
|
247 |
-
|
248 |
-
if len(teacher_rank):
|
249 |
-
teacher_rank = teacher_rank[0]
|
250 |
-
else:
|
251 |
-
teacher_rank = 'ููุงู'
|
252 |
-
|
253 |
-
student_rank = student_info["Narrator Rank"].to_list()
|
254 |
if len(student_rank):
|
255 |
student_rank = student_rank[0]
|
256 |
else:
|
257 |
student_rank = 'ููุงู'
|
258 |
-
|
259 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
260 |
else:
|
261 |
-
net.add_node(
|
262 |
-
|
|
|
|
|
263 |
net.add_edge(source, target, color = value_to_hex(int(row[f'{yaxis} Count'])), label = f"{row[f'{yaxis} Count']}")
|
264 |
-
net.
|
265 |
html = net.generate_html()
|
266 |
html = html.replace("'", "\"")
|
267 |
return f"""<iframe style="width: 100%; height: 600px;margin:0 auto" name="result" allow="midi; geolocation; microphone; camera;
|
268 |
-
|
269 |
-
|
270 |
-
|
271 |
-
|
272 |
|
273 |
|
274 |
with gr.Blocks() as demo:
|
|
|
9 |
from datasets import Value
|
10 |
from datasets import Dataset
|
11 |
import matplotlib.pyplot as plt
|
|
|
12 |
import re
|
13 |
|
14 |
pattern = r'"(.*?)"'
|
|
|
60 |
rgba_color = cmap(value)
|
61 |
return "#{:02X}{:02X}{:02X}".format(int(rgba_color[0] * 255), int(rgba_color[1] * 255), int(rgba_color[2] * 255))
|
62 |
|
|
|
63 |
|
64 |
def visualize_isnad(taraf_num, yaxis):
|
65 |
taraf_hadith = matn_info[matn_info['taraf_ID'] == taraf_num]['bookid_hadithid'].to_list()
|
|
|
84 |
lst_hadith.append([taraf_matns[i], gen_node, name_node, taraf_book[i], taraf_author[i], taraf_hadith_number[i], str(n), str(i)])
|
85 |
df = pd.DataFrame(lst_hadith, columns = ['Matn', 'Generation', 'Name', 'Book_Name', 'Author', 'Book Hadith Number', 'End Transmitter ID', 'Hadith Number'])
|
86 |
|
|
|
|
|
87 |
isnad_hadith['Teacher'] = isnad_hadith['Source'].apply(lambda x: narrator_bios[narrator_bios['Rawi ID'].astype(int) == int(x)]['Famous Name'].to_list())
|
88 |
isnad_hadith['Student'] = isnad_hadith['Destination'].apply(lambda x: narrator_bios[narrator_bios['Rawi ID'].astype(int) == int(x)]['Famous Name'].to_list())
|
89 |
isnad_hadith['Teacher'] = isnad_hadith['Teacher'].apply(lambda x: x[0] if len(x)==1 else 'ููุงู')
|
|
|
138 |
allow-top-navigation-by-user-activation allow-downloads" allowfullscreen=""
|
139 |
allowpaymentrequest="" frameborder="0" srcdoc='{html}'></iframe>""" , df
|
140 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
141 |
|
142 |
def taraf_booknum(taraf_num):
|
143 |
taraf = matn_info[matn_info['taraf_ID'] == taraf_num]
|
|
|
153 |
isnad_hadith['Student'] = isnad_hadith['Destination'].apply(lambda x: narrator_bios[narrator_bios['Rawi ID'].astype(int) == int(x)]['Famous Name'].to_list())
|
154 |
isnad_hadith['Teacher'] = isnad_hadith['Teacher'].apply(lambda x: x[0] if len(x)==1 else 'ููุงู')
|
155 |
isnad_hadith['Student'] = isnad_hadith['Student'].apply(lambda x: x[0] if len(x)==1 else 'ููุงู')
|
156 |
+
|
157 |
+
end_nodes = df['End Transmitter ID'].tolist()
|
158 |
+
G = nx.from_pandas_edgelist(isnad_hadith, source = 'Source', target = 'Destination', create_using = nx.DiGraph())
|
159 |
+
isnad_pos = nx.nx_agraph.graphviz_layout(G, prog='dot')
|
160 |
+
x_stretch = 4
|
161 |
+
y_stretch = 4
|
162 |
net = Network(directed =True)
|
163 |
+
|
164 |
+
for node, pos in isnad_pos.items():
|
165 |
+
node_info = narrator_bios[narrator_bios['Rawi ID'] == int(node)]
|
166 |
+
student_narrations = node_info['Number of Narrations'].to_list()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
167 |
if len(student_narrations):
|
168 |
student_narrations = student_narrations[0]
|
169 |
else:
|
170 |
+
student_narrations = 1
|
171 |
+
student_gen = node_info['Generation'].to_list()
|
|
|
|
|
|
|
|
|
|
|
|
|
172 |
if len(student_gen):
|
173 |
student_gen = student_gen[0]
|
174 |
else:
|
175 |
student_gen = -1
|
176 |
+
student_rank = node_info["Narrator Rank"].to_list()
|
|
|
|
|
|
|
|
|
|
|
|
|
177 |
if len(student_rank):
|
178 |
student_rank = student_rank[0]
|
179 |
else:
|
180 |
student_rank = 'ููุงู'
|
181 |
+
node_name = node_info['Famous Name'].to_list()
|
182 |
+
if len(node_name):
|
183 |
+
node_name = node_name[0]
|
184 |
+
else:
|
185 |
+
node_name = 'ููุงู'
|
186 |
+
if node == '99999':
|
187 |
+
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)
|
188 |
+
elif node in end_nodes:
|
189 |
+
end_matn_info = df[df["End Transmitter ID"] == node]
|
190 |
+
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} \n Hadith {" ".join(end_matn_info["Hadith Number"].tolist())}', x= pos[0]*x_stretch, y= -1*pos[1]*y_stretch, size= 50)
|
191 |
else:
|
192 |
+
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)
|
193 |
+
for _, row in isnad_hadith.iterrows():
|
194 |
+
source = row['Source']
|
195 |
+
target = row['Destination']
|
196 |
net.add_edge(source, target, color = value_to_hex(int(row[f'{yaxis} Count'])), label = f"{row[f'{yaxis} Count']}")
|
197 |
+
net.toggle_physics(False)
|
198 |
html = net.generate_html()
|
199 |
html = html.replace("'", "\"")
|
200 |
return f"""<iframe style="width: 100%; height: 600px;margin:0 auto" name="result" allow="midi; geolocation; microphone; camera;
|
201 |
+
display-capture; encrypted-media;" sandbox="allow-modals allow-forms
|
202 |
+
allow-scripts allow-same-origin allow-popups
|
203 |
+
allow-top-navigation-by-user-activation allow-downloads" allowfullscreen=""
|
204 |
+
allowpaymentrequest="" frameborder="0" srcdoc='{html}'></iframe>"""
|
205 |
|
206 |
|
207 |
with gr.Blocks() as demo:
|