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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import pandas as pd
|
| 3 |
+
import io
|
| 4 |
+
import plotly.express as px
|
| 5 |
+
import plotly.graph_objects as go
|
| 6 |
+
import os
|
| 7 |
+
|
| 8 |
+
# Define admin credentials
|
| 9 |
+
ADMIN_EMAIL = os.environ["ACCOUNT"]
|
| 10 |
+
ADMIN_PASSWORD = os.environ["PASSWORD"]
|
| 11 |
+
|
| 12 |
+
# Variable to store uploaded grades file in memory
|
| 13 |
+
grades_df = None
|
| 14 |
+
|
| 15 |
+
# Admin login function
|
| 16 |
+
def admin_login_function(email, password):
|
| 17 |
+
if (email == ADMIN_EMAIL and password == ADMIN_PASSWORD):
|
| 18 |
+
return gr.update(visible=False), gr.update(visible=True),
|
| 19 |
+
else:
|
| 20 |
+
raise gr.Error("wrong password 💥!", duration=5)
|
| 21 |
+
|
| 22 |
+
|
| 23 |
+
# Intern login function
|
| 24 |
+
def intern_login(email):
|
| 25 |
+
results = intern_view(email)
|
| 26 |
+
return gr.update(visible=False), gr.update(visible=True), results[0], results[1]
|
| 27 |
+
|
| 28 |
+
# Admin upload function
|
| 29 |
+
def admin_upload(file):
|
| 30 |
+
global grades_df
|
| 31 |
+
if file is not None:
|
| 32 |
+
grades_df = pd.read_excel(file.name)
|
| 33 |
+
return "File uploaded and processed successfully."
|
| 34 |
+
else:
|
| 35 |
+
return "Please upload a valid Excel file."
|
| 36 |
+
|
| 37 |
+
# Function to generate performance analysis charts
|
| 38 |
+
def performance_analysis():
|
| 39 |
+
if grades_df is not None:
|
| 40 |
+
# Ensure 'Created At' is datetime
|
| 41 |
+
grades_df['Created At'] = pd.to_datetime(grades_df['Created At'])
|
| 42 |
+
grades_df['Date'] = grades_df['Created At'].dt.date
|
| 43 |
+
|
| 44 |
+
# Group by date and calculate sum of grades
|
| 45 |
+
daily_performance = grades_df.groupby('Date')['Grade'].sum().reset_index()
|
| 46 |
+
|
| 47 |
+
# Line chart for daily performance
|
| 48 |
+
line_chart = px.line(daily_performance, x='Date', y='Grade', title='Daily Performance (Total Grades)')
|
| 49 |
+
|
| 50 |
+
email_performance = grades_df.groupby('Email')['Grade'].sum().reset_index()
|
| 51 |
+
# Histogram of grades
|
| 52 |
+
histogram = px.histogram(email_performance, x='Grade', nbins=20, title='Grade Distribution')
|
| 53 |
+
|
| 54 |
+
# Box plot for grade statistics
|
| 55 |
+
box_plot = px.box(email_performance, y='Grade', title='Grade Statistics')
|
| 56 |
+
|
| 57 |
+
# Create a summary table
|
| 58 |
+
summary_stats = email_performance['Grade'].describe().reset_index()
|
| 59 |
+
summary_table = go.Figure(data=[go.Table(
|
| 60 |
+
header=dict(values=['Statistic', 'Value']),
|
| 61 |
+
cells=dict(values=[summary_stats['index'], summary_stats['Grade']])
|
| 62 |
+
)])
|
| 63 |
+
|
| 64 |
+
return line_chart, histogram, box_plot, summary_table
|
| 65 |
+
else:
|
| 66 |
+
return None, None, None, None
|
| 67 |
+
|
| 68 |
+
MAX_SCORE = 25*2 # Assuming the maximum possible score is 100
|
| 69 |
+
|
| 70 |
+
def intern_view(email):
|
| 71 |
+
if grades_df is not None:
|
| 72 |
+
intern_grades = grades_df[grades_df['Email'] == email]
|
| 73 |
+
daily_performance = intern_grades.groupby('Date')['Grade'].sum().reset_index()
|
| 74 |
+
line_chart = px.line(daily_performance, x='Date', y='Grade', title='Daily Performance (Total Grades)')
|
| 75 |
+
if not intern_grades.empty:
|
| 76 |
+
total_grade = intern_grades['Grade'].sum()
|
| 77 |
+
average_grade = intern_grades['Grade'].mean()
|
| 78 |
+
num_records = intern_grades.shape[0]
|
| 79 |
+
|
| 80 |
+
# Normalize the score to MAX_SCORE
|
| 81 |
+
normalized_score = min(total_grade / MAX_SCORE, 1)
|
| 82 |
+
|
| 83 |
+
# Determine the color level
|
| 84 |
+
if normalized_score <= 0.8:
|
| 85 |
+
color = "red"
|
| 86 |
+
else:
|
| 87 |
+
color = "green"
|
| 88 |
+
# <div style="background-color:{color}; padding:10px; border-radius:5px;">
|
| 89 |
+
# **Performance Level**: {color.capitalize()}
|
| 90 |
+
# </div>
|
| 91 |
+
|
| 92 |
+
# - Quá 3 assignment đạt level đỏ sẽ phải liên hệ manager để giải trình
|
| 93 |
+
# Return as a Markdown formatted string
|
| 94 |
+
return f"""
|
| 95 |
+
### Grade Analysis for {email}
|
| 96 |
+
|
| 97 |
+
- **Tổng điểm**: {normalized_score}
|
| 98 |
+
- **số lần upload**: {num_records}
|
| 99 |
+
""", line_chart
|
| 100 |
+
else:
|
| 101 |
+
return gr.Error("No records found for this email. 💥!", duration=5)
|
| 102 |
+
else:
|
| 103 |
+
return gr.Error("No grades file uploaded. Please contact the admin.", duration=5)
|
| 104 |
+
|
| 105 |
+
# Create the Gradio interface
|
| 106 |
+
with gr.Blocks() as demo:
|
| 107 |
+
with gr.Tab("Admin Login"):
|
| 108 |
+
with gr.Column(visible=True) as admin_login_section:
|
| 109 |
+
admin_email = gr.Textbox(label="Admin Email")
|
| 110 |
+
admin_password = gr.Textbox(label="Admin Password", type="password")
|
| 111 |
+
admin_login_button = gr.Button("Login")
|
| 112 |
+
admin_login_output = gr.Textbox(label="Admin Output")
|
| 113 |
+
|
| 114 |
+
with gr.Column(visible=False) as admin_upload_section:
|
| 115 |
+
with gr.Column() as update_file_section:
|
| 116 |
+
admin_file = gr.File(label="Upload Grades Excel File")
|
| 117 |
+
admin_submit = gr.Button("Submit")
|
| 118 |
+
admin_file_output = gr.Textbox(label="File Upload Output")
|
| 119 |
+
|
| 120 |
+
admin_analyze = gr.Button("Analyze")
|
| 121 |
+
admin_charts = gr.Plot()
|
| 122 |
+
admin_histogram = gr.Plot()
|
| 123 |
+
admin_box_plot = gr.Plot()
|
| 124 |
+
admin_summary_table = gr.Plot()
|
| 125 |
+
|
| 126 |
+
admin_login_button.click(
|
| 127 |
+
admin_login_function,
|
| 128 |
+
inputs=[admin_email, admin_password],
|
| 129 |
+
outputs=[admin_login_section, admin_upload_section],
|
| 130 |
+
show_progress=True,
|
| 131 |
+
)
|
| 132 |
+
admin_submit.click(
|
| 133 |
+
admin_upload,
|
| 134 |
+
inputs=[admin_file],
|
| 135 |
+
outputs=admin_file_output,
|
| 136 |
+
)
|
| 137 |
+
admin_analyze.click(
|
| 138 |
+
performance_analysis,
|
| 139 |
+
outputs=[admin_charts, admin_histogram, admin_box_plot, admin_summary_table],
|
| 140 |
+
)
|
| 141 |
+
|
| 142 |
+
with gr.Tab("Intern Login"):
|
| 143 |
+
with gr.Column(visible=True) as intern_login_section:
|
| 144 |
+
intern_email = gr.Textbox(label="Intern Email")
|
| 145 |
+
intern_login_button = gr.Button("Find")
|
| 146 |
+
intern_login_output = gr.Textbox(label="Intern Output")
|
| 147 |
+
|
| 148 |
+
with gr.Column(visible=False) as intern_info_section:
|
| 149 |
+
intern_output = gr.Markdown(label="Grade Analysis")
|
| 150 |
+
intern_charts = gr.Plot()
|
| 151 |
+
|
| 152 |
+
intern_login_button.click(
|
| 153 |
+
intern_login,
|
| 154 |
+
inputs=[intern_email],
|
| 155 |
+
outputs=[intern_login_section, intern_info_section, intern_output, intern_charts],
|
| 156 |
+
show_progress=True,
|
| 157 |
+
)
|
| 158 |
+
|
| 159 |
+
# intern_submit.click(
|
| 160 |
+
# intern_view,
|
| 161 |
+
# inputs=[intern_email],
|
| 162 |
+
# outputs=intern_output,
|
| 163 |
+
# )
|
| 164 |
+
|
| 165 |
+
# Launch the app
|
| 166 |
+
demo.launch(debug=True)
|