Taraf_timeline / app.py
FDSRashid's picture
Update app.py
51fc172
raw
history blame
2.33 kB
# -*- 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)]
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
# app = gradio.Interface(plot_timeline,
# [gradio.Dropdown(choices = ['Taraf', 'Hadith', 'Isnad', 'Ranking'], value = 'Taraf', label = 'Y Axis'), gradio.Dropdown(choices = cities, value = ['المدينه', 'بغداد', 'كوفة', 'بصرة'], multiselect=True),
# gradio.Slider(min_year, max_year, value = 0, label = 'Begining', info = 'Choose the first year to display Tarafs'),
# gradio.Slider(min_year, max_year, value = 400, label = 'End', info = 'Choose the last year to display Tarafs')
# ],
# gradio.Plot()).launch()
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, value = ['المدينه', 'بغداد', 'كوفة', 'بصرة'], multiselect=True)
begining = gr.Slider(min_year, max_year, value = 0, label = 'Begining', info = 'Choose the first year to display Tarafs')
end = gr.Slider(min_year, max_year, value = 400, label = 'End', 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()