vietdata's picture
Update app.py
0b4a8f9 verified
raw
history blame
4.04 kB
import gradio as gr
import pandas as pd
import io
import plotly.express as px
import plotly.graph_objects as go
# Define admin credentials
ADMIN_EMAIL = "[email protected]"
ADMIN_PASSWORD = "123456"
# Variable to store uploaded grades file in memory
grades_df = None
# Admin login function
def admin_login(email, password):
if email == ADMIN_EMAIL and password == ADMIN_PASSWORD:
return (True, "", "Logged in successfully.", True)
else:
return (False, "", "Invalid credentials.", False)
# Admin upload function
def admin_upload(file):
global grades_df
if file is not None:
grades_df = pd.read_excel(io.BytesIO(file.read()))
return "File uploaded and processed successfully."
else:
return "Please upload a valid Excel file."
# Function to generate performance analysis charts
def performance_analysis():
if grades_df is not None:
# Ensure 'Created At' is datetime
grades_df['Created At'] = pd.to_datetime(grades_df['Created At'])
grades_df['Date'] = grades_df['Created At'].dt.date
# Group by date and calculate sum of grades
daily_performance = grades_df.groupby('Date')['Grade'].sum().reset_index()
# Line chart for daily performance
line_chart = px.line(daily_performance, x='Date', y='Grade', title='Daily Performance (Total Grades)')
# Histogram of grades
histogram = px.histogram(grades_df, x='Grade', nbins=20, title='Grade Distribution')
# Box plot for grade statistics
box_plot = px.box(grades_df, y='Grade', title='Grade Statistics')
# Create a summary table
summary_stats = grades_df['Grade'].describe().reset_index()
summary_table = go.Figure(data=[go.Table(
header=dict(values=['Statistic', 'Value']),
cells=dict(values=[summary_stats['index'], summary_stats['Grade']])
)])
return line_chart, histogram, box_plot, summary_table
else:
return None, None, None, None
# Create the Gradio interface
with gr.Blocks() as demo:
with gr.Tab("Admin Login"):
admin_email = gr.Textbox(label="Admin Email")
admin_password = gr.Textbox(label="Admin Password", type="password")
admin_login_button = gr.Button("Login")
admin_login_output = gr.Textbox(label="Admin Output")
admin_file = gr.File(label="Upload Grades Excel File", visible=False)
admin_submit = gr.Button("Submit", visible=False)
admin_file_output = gr.Textbox(label="File Upload Output", visible=False)
admin_charts = gr.Plot(visible=False)
admin_histogram = gr.Plot(visible=False)
admin_box_plot = gr.Plot(visible=False)
admin_summary_table = gr.Plot(visible=False)
admin_login_button.click(
admin_login,
inputs=[admin_email, admin_password],
outputs=[admin_login_output, admin_file, admin_submit, admin_file_output],
show_progress=False,
)
admin_submit.click(
admin_upload,
inputs=[admin_file],
outputs=admin_file_output,
)
admin_submit.click(
performance_analysis,
outputs=[admin_charts, admin_histogram, admin_box_plot, admin_summary_table],
)
with gr.Tab("Intern Login"):
intern_email = gr.Textbox(label="Intern Email")
intern_login_button = gr.Button("Login")
intern_login_output = gr.Textbox(label="Intern Output")
intern_submit = gr.Button("View Grades", visible=False)
intern_output = gr.Textbox(label="Grade Analysis", visible=False)
intern_login_button.click(
intern_login,
inputs=[intern_email],
outputs=[intern_login_output, intern_submit, intern_output],
show_progress=False,
)
intern_submit.click(
intern_view,
inputs=[intern_email],
outputs=intern_output,
)
# Launch the app
demo.launch()