Spaces:
Sleeping
Sleeping
File size: 4,150 Bytes
aa7a63c 4dfc1a8 596f960 4dfc1a8 0448c54 aa7a63c 4dfc1a8 aa7a63c 4dfc1a8 0448c54 aa7a63c 261a8a2 4dfc1a8 261a8a2 3529e1d 79b3349 93afc3e 62129f1 6f9365f 3529e1d 87fff89 3529e1d f382723 4dfc1a8 4500205 3529e1d 4500205 3529e1d 488e2a0 a1af215 d062be7 4dfc1a8 aa7a63c 4dfc1a8 aa7a63c 4dfc1a8 97e51d0 4dfc1a8 261a8a2 e0175dc bd960a6 4dfc1a8 261a8a2 4dfc1a8 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
import gradio as gr
from pyvis.network import Network
import networkx as nx
import numpy as np
import pandas as pd
import os
from datasets import load_dataset
import matplotlib.pyplot as plt
Secret_token = os.getenv('token')
dataset = load_dataset('FDSRashid/hadith_info',data_files = 'Basic_Edge_Information.csv', token = Secret_token, split = 'train')
dataset2 = load_dataset('FDSRashid/hadith_info',data_files = 'Taraf_Info.csv', token = Secret_token, split = 'train')
edge_info = dataset.to_pandas()
taraf_info = dataset2.to_pandas()
cities = taraf_info['City'].unique().tolist()
min_year = int(taraf_info['Year'].min())
max_year = int(taraf_info['Year'].max())
cmap = plt.colormaps['cool']
def value_to_hex(value):
rgba_color = cmap(value)
return "#{:02X}{:02X}{:02X}".format(int(rgba_color[0] * 255), int(rgba_color[1] * 255), int(rgba_color[2] * 255))
def subsetEdges(city, fstyear, lstyear):
info = taraf_info[(taraf_info['Year'] >= fstyear) & (taraf_info['City'] == city) & (taraf_info['Year'] <= lstyear)]
narrators = edge_info[edge_info['Edge_ID'].isin(info['ID'].unique())]
return narrators
def splitIsnad(dataframe):
teacher_student =dataframe['Edge_Name'].str.split(' TO ')
dataframe['Teacher'] = teacher_student.apply(lambda x: x[0])
dataframe['Student'] = teacher_student.apply(lambda x: x[1])
return dataframe
def network_visualizer(yaxis, city, fstyear,lastyr, num_nodes):
edges = splitIsnad(subsetEdges(city, fstyear, lastyr))[['Teacher', 'Student', yaxis]].reset_index()
#.groupby(['Teacher', 'Student']).sum()
if edges.shape[0] > num_nodes:
edge_15 = edges.sort_values(by=yaxis, ascending=False).head(num_nodes)
else:
edge_15 = edges.copy()
teacher_hadiths = edge_15[['Teacher', yaxis]].groupby('Teacher').sum().reset_index()
student_hadiths = edge_15[['Student', yaxis]].groupby('Student').sum().reset_index()
net = Network()
for _, row in edge_15.iterrows():
source = row['Teacher']
target = row['Student']
attribute_value = row[yaxis]
edge_color = value_to_hex(attribute_value)
hadith_count = teacher_hadiths[teacher_hadiths['Teacher'] == source][yaxis].to_list()[0]
student_count = student_hadiths[student_hadiths['Student'] == target][yaxis].to_list()[0]
net.add_node(source, color=value_to_hex(hadith_count), font = {'size':30, 'color': 'orange'}, label = f"{source}\n{yaxis}: {hadith_count}")
net.add_node(target, color=value_to_hex(student_count) , font = {'size': 20, 'color': 'red'}, label = f"{target}\n{yaxis}: {student_count}")
net.add_edge(source, target, color=edge_color, value=attribute_value, label = f"{source} to {target}\n{yaxis}: {attribute_value}")
net.barnes_hut(gravity=-5000, central_gravity=0.3, spring_length=200)
html = net.generate_html()
html = html.replace("'", "\"")
return f"""<iframe style="width: 100%; height: 600px;margin:0 auto" name="result" allow="midi; geolocation; microphone; camera;
display-capture; encrypted-media;" sandbox="allow-modals allow-forms
allow-scripts allow-same-origin allow-popups
allow-top-navigation-by-user-activation allow-downloads" allowfullscreen=""
allowpaymentrequest="" frameborder="0" srcdoc='{html}'></iframe>"""
with gr.Blocks() as demo:
Yaxis = gr.Dropdown(choices = ['Tarafs', 'Hadiths', 'Isnads', 'Books'], value = 'Tarafs', label = 'Variable to Display', info = 'Choose the variable to visualize.')
Places = gr.Dropdown(choices = cities, value = 'المدينه', label = 'Location')
FirstYear = gr.Slider(min_year, max_year, value = -11, label = 'Begining', info = 'Choose the first year to display Narrators')
Last_Year = gr.Slider(min_year, max_year, value = 9, label = 'End', info = 'Choose the last year to display Narrators')
num_narrators = gr.Slider(0, 1200, value = 400, label = 'Narrators', info = 'Choose the number of Narrators to display')
btn = gr.Button('Submit')
btn.click(fn = network_visualizer, inputs = [Yaxis, Places, FirstYear, Last_Year, num_narrators], outputs = gr.HTML())
demo.launch() |