File size: 2,069 Bytes
768a4d5
 
 
 
051ae15
8fc0051
768a4d5
1936185
60a00d7
768a4d5
2814f62
5906973
6485685
1936185
768a4d5
 
 
 
 
 
 
 
 
 
ec43a3b
7520ae0
 
d74f67f
 
 
 
 
 
a5afeb3
d74f67f
51fc172
22c5a57
768a4d5
051ae15
 
 
 
300ac99
c5ed808
9b5ce7a
 
dbc2f2d
 
051ae15
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
# -*- coding: utf-8 -*-


import numpy as np
import gradio as gr
import os
import pandas as pd
from datasets import load_dataset


import plotly.express as px
Secret_token = os.getenv('token')
dataset = load_dataset("FDSRashid/taraf_by_year", token = Secret_token,split="train")
taraf_s = dataset.to_pandas()


taraf_s = taraf_s.sort_values(['City', 'Year'], ascending=True)

cities = taraf_s['City'].unique().tolist()
min_year = int(taraf_s['Year'].min())
max_year = int(taraf_s['Year'].max())



def plot_timeline(yaxis, citi = ['المدينه', 'بغداد', 'كوفة', 'بصرة'], min_year = 0, max_year = 400):
    if min_year >= max_year:
        raise gr.error('Min Year Cannot be Bigger than Max Year!')
    
    filtered = taraf_s[taraf_s['City'].isin(citi) & (taraf_s['Year'] >= min_year) & (taraf_s['Year'] <= max_year)].drop(['Unnamed: 0', 'Ranking'], axis = 1)
    if 'All' in citi:
        tot = taraf_s.groupby(['Year']).sum().reset_index().drop(['Unnamed: 0', 'Ranking'], axis = 1)
        tot['City'] = 'All'
        filtered = pd.concat([filtered, tot])
        filtered = filtered[(filtered['Year'] >= min_year) & (filtered['Year'] <= max_year)]
    fig = px.line(data_frame = filtered, x = 'Year', y = yaxis, title = f"{yaxis} per Year", color = 'City', template = 'plotly_dark' )
    fig.update_layout(legend_x=1, legend_y=0, title = {'x':.5})
    return fig


demo = gr.Blocks()
with demo:
    with gr.Row():
        y_value = gr.Dropdown(choices = ['Taraf', 'Hadith', 'Isnad', 'Ranking'], value = 'Taraf', label = 'Y Axis')
        city = gr.Dropdown(choices = cities + ['All'], value = ['المدينه', 'بغداد', 'كوفة', 'بصرة'], multiselect=True, label = 'City')
    begining = gr.Slider(min_year, max_year, value = 0, label = 'First Year', info = 'Choose the first year to display Tarafs') 
    end = gr.Slider(min_year, max_year, value = 400, label = 'Last Year', info = 'Choose the last year to display Tarafs')
    btn = gr.Button("Plot")
    btn.click(plot_timeline, [y_value,city, begining, end], gr.Plot())
demo.launch()