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 from datasets import Features from datasets import Value from datasets import 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') features = Features({'Rawi ID': Value('int32'), 'Famous Name': Value('string'), 'Narrator Rank': Value('string'), 'Number of Narrations': Value('string'), 'Generation': Value('string')}) narrator_bios = load_dataset("FDSRashid/hadith_info", data_files = 'Teacher_Bios.csv', token = Secret_token,features=features ) narrator_bios = narrator_bios['train'].to_pandas() narrator_bios.loc[49845, 'Narrator Rank'] = 'رسول الله' narrator_bios.loc[49845, 'Number of Narrations'] = 0 narrator_bios['Number of Narrations'] = narrator_bios['Number of Narrations'].astype(int) narrator_bios.loc[49845, 'Number of Narrations'] = 443471 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 get_node_info(node): node_info = narrator_bios[narrator_bios['Rawi ID'] == int(node)] student_narrations = node_info['Number of Narrations'].to_list() if len(student_narrations): student_narrations = student_narrations[0] else: student_narrations = 1 student_gen = node_info['Generation'].to_list() if len(student_gen): student_gen = student_gen[0] else: student_gen = -1 student_rank = node_info["Narrator Rank"].to_list() if len(student_rank): student_rank = student_rank[0] else: student_rank = 'فلان' node_name = node_info['Famous Name'].to_list() if len(node_name): node_name = node_name[0] else: node_name = 'فلان' return node_info,student_narrations,student_gen, student_rank, node_name def network_visualizer(yaxis, city, fstyear,lastyr, num_nodes): edges = subsetEdges(city, fstyear, lastyr).sort_values(yaxis, ascending=False).head(num_nodes) G = nx.from_pandas_edgelist(edges, source = 'Teacher_ID', target = 'Student_ID', create_using = nx.DiGraph()) # nodes = list(G.nodes) # node_reports = [narrator_bios[narrator_bios['Rawi ID'].astype(int) == int(x)]['Number of Narrations'].to_list()[0] for x in nodes] # nodes_df = pd.DataFrame({'Node': nodes, 'Report': node_reports}).sort_values('Report', ascending=False).head(num_nodes) # nodes_remove = list(set(nodes) - set(nodes_df['Node'].to_list())) # [G.remove_nodes_from(nodes_remove)] #.groupby(['Teacher', 'Student']).sum() # if edges_split.shape[0] > num_nodes: # edge_15 = edges_split.sort_values(by=yaxis, ascending=False).head(num_nodes) # else: # edge_15 = edges_split.copy() net = Network(directed =True, select_menu=True, cdn_resources='remote') for node in G.nodes: node_info,student_narrations,student_gen, student_rank, node_name = get_node_info(node) if node == 99999: net.add_node(int(node), font = {'size':50, 'color': 'black'}, color = '#000000', label = f'{node_name} \n ID: {node} - Gen {student_gen}', size= 70) else: net.add_node(int(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}', size= 50) for edge in G.edges: row = edges[(edges['Teacher_ID'] == edge[0]) & (edges['Student_ID'] == edge[1])].iloc[0] source = row['Teacher_ID'] target = row['Student_ID'] net.add_edge(int(source), int(target), color = value_to_hex(int(row[yaxis])), label = f"{row[yaxis]}", value= int(row[yaxis])) # for _, row in edge_15.iterrows(): # source = row['Teacher'] # target = row['Student'] # attribute_value = row[yaxis] # edge_color = value_to_hex(attribute_value) # teacher_info = narrator_bios[narrator_bios['Rawi ID'] == row['Teacher_ID']] # student_info = narrator_bios[narrator_bios['Rawi ID'] == row['Student_ID']] # teacher_narrations = teacher_info['Number of Narrations'].to_list()[0] # student_narrations = student_info['Number of Narrations'].to_list()[0] # net.add_node(source, color=value_to_hex(teacher_narrations), font = {'size':30, 'color': 'orange'}, label = f"{source}\n{teacher_narrations}") # net.add_node(target, color=value_to_hex(student_narrations), font = {'size': 20, 'color': 'red'}, label = f"{target}\n{student_narrations}") # net.add_edge(source, target, color=edge_color, value=attribute_value, label = f"{yaxis}:{attribute_value}") net.barnes_hut(gravity=-5000, central_gravity=0.1, spring_length=200) html = net.generate_html() html = html.replace("'", "\"") return f"""""" 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, 2000, value = 400, label = 'Transmissions', info = 'Choose the number of Transmissions to display') btn = gr.Button('Submit') btn.click(fn = network_visualizer, inputs = [Yaxis, Places, FirstYear, Last_Year, num_narrators], outputs = gr.HTML()) demo.launch()