|
import gradio as gr |
|
import pandas as pd |
|
import io |
|
import plotly.express as px |
|
import plotly.graph_objects as go |
|
import os |
|
|
|
|
|
ADMIN_EMAIL = os.environ["ACCOUNT"] |
|
ADMIN_PASSWORD = os.environ["PASSWORD"] |
|
|
|
|
|
grades_df = None |
|
|
|
|
|
def admin_login_function(email, password): |
|
if (email == ADMIN_EMAIL and password == ADMIN_PASSWORD): |
|
return gr.update(visible=False), gr.update(visible=True), |
|
else: |
|
raise gr.Error("wrong password 💥!", duration=5) |
|
|
|
|
|
|
|
def intern_login(email): |
|
results = intern_view(email) |
|
return gr.update(visible=False), gr.update(visible=True), results[0], results[1] |
|
|
|
|
|
def admin_upload(file): |
|
global grades_df |
|
if file is not None: |
|
grades_df = pd.read_excel(file.name) |
|
return "File uploaded and processed successfully." |
|
else: |
|
return "Please upload a valid Excel file." |
|
|
|
|
|
def performance_analysis(): |
|
if grades_df is not None: |
|
|
|
grades_df['Created At'] = pd.to_datetime(grades_df['Created At']) |
|
grades_df['Date'] = grades_df['Created At'].dt.date |
|
|
|
|
|
daily_performance = grades_df.groupby('Date')['Grade'].sum().reset_index() |
|
|
|
|
|
line_chart = px.line(daily_performance, x='Date', y='Grade', title='Daily Performance (Total Grades)') |
|
|
|
email_performance = grades_df.groupby('Email')['Grade'].sum().reset_index() |
|
|
|
histogram = px.histogram(email_performance, x='Grade', nbins=20, title='Grade Distribution') |
|
|
|
|
|
box_plot = px.box(email_performance, y='Grade', title='Grade Statistics') |
|
|
|
|
|
summary_stats = email_performance['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 |
|
|
|
MAX_SCORE = 25*2 |
|
|
|
def intern_view(email): |
|
if grades_df is not None: |
|
intern_grades = grades_df[grades_df['Email'] == email] |
|
daily_performance = intern_grades.groupby('Date')['Grade'].sum().reset_index() |
|
line_chart = px.line(daily_performance, x='Date', y='Grade', title='Daily Performance (Total Grades)') |
|
if not intern_grades.empty: |
|
total_grade = intern_grades['Grade'].sum() |
|
average_grade = intern_grades['Grade'].mean() |
|
num_records = intern_grades.shape[0] |
|
|
|
|
|
normalized_score = min(total_grade / MAX_SCORE, 1) |
|
|
|
|
|
if normalized_score <= 0.8: |
|
color = "red" |
|
else: |
|
color = "green" |
|
|
|
|
|
|
|
|
|
|
|
|
|
return f""" |
|
### Grade Analysis for {email} |
|
|
|
- **Tổng điểm**: {normalized_score} |
|
- **số lần upload**: {num_records} |
|
""", line_chart |
|
else: |
|
return gr.Error("No records found for this email. 💥!", duration=5) |
|
else: |
|
return gr.Error("No grades file uploaded. Please contact the admin.", duration=5) |
|
|
|
|
|
with gr.Blocks() as demo: |
|
with gr.Tab("Admin Login"): |
|
with gr.Column(visible=True) as admin_login_section: |
|
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") |
|
|
|
with gr.Column(visible=False) as admin_upload_section: |
|
with gr.Column() as update_file_section: |
|
admin_file = gr.File(label="Upload Grades Excel File") |
|
admin_submit = gr.Button("Submit") |
|
admin_file_output = gr.Textbox(label="File Upload Output") |
|
|
|
admin_analyze = gr.Button("Analyze") |
|
admin_charts = gr.Plot() |
|
admin_histogram = gr.Plot() |
|
admin_box_plot = gr.Plot() |
|
admin_summary_table = gr.Plot() |
|
|
|
admin_login_button.click( |
|
admin_login_function, |
|
inputs=[admin_email, admin_password], |
|
outputs=[admin_login_section, admin_upload_section], |
|
show_progress=True, |
|
) |
|
admin_submit.click( |
|
admin_upload, |
|
inputs=[admin_file], |
|
outputs=admin_file_output, |
|
) |
|
admin_analyze.click( |
|
performance_analysis, |
|
outputs=[admin_charts, admin_histogram, admin_box_plot, admin_summary_table], |
|
) |
|
|
|
with gr.Tab("Intern Login"): |
|
with gr.Column(visible=True) as intern_login_section: |
|
intern_email = gr.Textbox(label="Intern Email") |
|
intern_login_button = gr.Button("Find") |
|
intern_login_output = gr.Textbox(label="Intern Output") |
|
|
|
with gr.Column(visible=False) as intern_info_section: |
|
intern_output = gr.Markdown(label="Grade Analysis") |
|
intern_charts = gr.Plot() |
|
|
|
intern_login_button.click( |
|
intern_login, |
|
inputs=[intern_email], |
|
outputs=[intern_login_section, intern_info_section, intern_output, intern_charts], |
|
show_progress=True, |
|
) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
demo.launch(debug=True) |
|
|