import gradio as gr from pyvis.network import Network import networkx as nx import numpy as np import pandas as pd from datasets import load_dataset 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()) def subsetEdges(city, year): info = taraf_info[(taraf_info['Year'] == year) & (taraf_info['City'] == city)] 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(city, year): edge_15 = splitIsnad(subsetEdges(city, year)).sample(300) nt = Network() for _, row in edge_15.iterrows(): source = row['Teacher'] target = row['Student'] attribute_value = row['Hadiths'] edge_color = value_to_hex(attribute_value) net.add_node(source, color=value_to_hex(attribute_value), font = {'size':30, 'color': 'orange'}) net.add_node(target, color=value_to_hex(attribute_value) , font = {'size': 20, 'color': 'red'}) net.add_edge(source, target, color=edge_color, value=attribute_value) net.barnes_hut(gravity=-5000, central_gravity=0.3, spring_length=200) html = net.generate_html() html = html.replace("'", "\"") return f"""""" with gr.Blocks() as demo: Places = gr.Dropdown(choices = cities, value = 'المدينه', label = 'Location') Last_Year = gr.Slider(min_year, max_year, value = 9, label = 'End', info = 'Choose the year to display Narrators') btn = gr.Button('Submit') btn.click(fn = network_visualizer, inputs = [Places, Last_Year], outputs = gr.HTML()) demo.launch()