# -*- 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]) 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()