Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,30 +1,50 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from pyvis.network import Network
|
| 3 |
import networkx as nx
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
nx_graph.nodes[3]['title'] = 'I belong to a different group!'
|
| 8 |
-
nx_graph.nodes[3]['group'] = 10
|
| 9 |
-
nx_graph.add_node(20, size=20, title='couple', group=2)
|
| 10 |
-
nx_graph.add_node(21, size=15, title='couple', group=2)
|
| 11 |
-
nx_graph.add_edge(20, 21, weight=5)
|
| 12 |
-
nx_graph.add_node(25, size=25, label='lonely', title='lonely node', group=3)
|
| 13 |
|
| 14 |
-
# graph above refer to: https://pyvis.readthedocs.io/en/latest/tutorial.html#networkx-integration
|
| 15 |
|
| 16 |
-
|
| 17 |
-
# populates the nodes and edges data structures
|
| 18 |
-
# nt_notebook.from_nx(nx_graph)
|
| 19 |
-
# nt_notebook.show('nx.html')
|
| 20 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
|
| 22 |
-
def
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
nt = Network()
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
html = html.replace("'", "\"")
|
|
|
|
| 28 |
|
| 29 |
return f"""<iframe style="width: 100%; height: 600px;margin:0 auto" name="result" allow="midi; geolocation; microphone; camera;
|
| 30 |
display-capture; encrypted-media;" sandbox="allow-modals allow-forms
|
|
@@ -33,12 +53,9 @@ def needs_analysis():
|
|
| 33 |
allowpaymentrequest="" frameborder="0" srcdoc='{html}'></iframe>"""
|
| 34 |
|
| 35 |
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
)
|
| 43 |
-
|
| 44 |
-
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from pyvis.network import Network
|
| 3 |
import networkx as nx
|
| 4 |
+
import numpy as np
|
| 5 |
+
import pandas as pd
|
| 6 |
+
from datasets import load_dataset
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
|
|
|
|
| 8 |
|
| 9 |
+
Secret_token = os.getenv('token')
|
|
|
|
|
|
|
|
|
|
| 10 |
|
| 11 |
+
dataset = load_dataset('FDSRashid/hadith_info',data_files = 'Basic_Edge_Information.csv', token = Secret_token, split = 'train')
|
| 12 |
+
dataset2 = load_dataset('FDSRashid/hadith_info',data_files = 'Taraf_Info.csv', token = Secret_token, split = 'train')
|
| 13 |
+
edge_info = dataset.to_pandas()
|
| 14 |
+
taraf_info = dataset2.to_pandas()
|
| 15 |
+
cities = taraf_info['City'].unique().tolist()
|
| 16 |
+
min_year = int(taraf_info['Year'].min())
|
| 17 |
+
max_year = int(taraf_info['Year'].max())
|
| 18 |
|
| 19 |
+
def subsetEdges(city, year):
|
| 20 |
+
info = taraf_info[(taraf_info['Year'] == year) & (taraf_info['City'] == city)]
|
| 21 |
+
narrators = edge_info[edge_info['Edge_ID'].isin(info['ID'].unique())]
|
| 22 |
+
return narrators
|
| 23 |
+
def splitIsnad(dataframe):
|
| 24 |
+
teacher_student =dataframe['Edge_Name'].str.split(' TO ')
|
| 25 |
+
dataframe['Teacher'] = teacher_student.apply(lambda x: x[0])
|
| 26 |
+
dataframe['Student'] = teacher_student.apply(lambda x: x[1])
|
| 27 |
+
return dataframe
|
| 28 |
+
|
| 29 |
+
|
| 30 |
+
def network_visualizer(city, year):
|
| 31 |
+
edge_15 = splitIsnad(subsetEdges(city, year)).sample(300)
|
| 32 |
nt = Network()
|
| 33 |
+
for _, row in edge_15.iterrows():
|
| 34 |
+
source = row['Teacher']
|
| 35 |
+
target = row['Student']
|
| 36 |
+
attribute_value = row['Hadiths']
|
| 37 |
+
edge_color = value_to_hex(attribute_value)
|
| 38 |
+
|
| 39 |
+
net.add_node(source, color=value_to_hex(attribute_value), font = {'size':30, 'color': 'orange'})
|
| 40 |
+
net.add_node(target, color=value_to_hex(attribute_value) , font = {'size': 20, 'color': 'red'})
|
| 41 |
+
net.add_edge(source, target, color=edge_color, value=attribute_value)
|
| 42 |
+
|
| 43 |
+
|
| 44 |
+
net.barnes_hut(gravity=-5000, central_gravity=0.3, spring_length=200)
|
| 45 |
+
html = net.generate_html()
|
| 46 |
html = html.replace("'", "\"")
|
| 47 |
+
|
| 48 |
|
| 49 |
return f"""<iframe style="width: 100%; height: 600px;margin:0 auto" name="result" allow="midi; geolocation; microphone; camera;
|
| 50 |
display-capture; encrypted-media;" sandbox="allow-modals allow-forms
|
|
|
|
| 53 |
allowpaymentrequest="" frameborder="0" srcdoc='{html}'></iframe>"""
|
| 54 |
|
| 55 |
|
| 56 |
+
with gr.Blocks() as demo:
|
| 57 |
+
Places = gr.Dropdown(choices = cities, value = 'المدينه', label = 'Location')
|
| 58 |
+
Last_Year = gr.Slider(min_year, max_year, value = 9, label = 'End', info = 'Choose the year to display Narrators')
|
| 59 |
+
btn = gr.Button('Submit')
|
| 60 |
+
btn.click(fn = network_visualizer, inputs = [Places, Last_Year], outputs = gr.HTML())
|
| 61 |
+
demo.launch()
|
|
|
|
|
|
|
|
|