vietdata commited on
Commit
1967c28
·
verified ·
1 Parent(s): 94d74f3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +91 -38
app.py CHANGED
@@ -6,26 +6,28 @@ import plotly.graph_objects as go
6
 
7
  # Define admin credentials
8
  ADMIN_EMAIL = "[email protected]"
9
- ADMIN_PASSWORD = "123456"
10
 
11
  # Variable to store uploaded grades file in memory
12
  grades_df = None
13
 
14
  # Admin login function
15
- def admin_login(email, password):
16
- if email == ADMIN_EMAIL and password == ADMIN_PASSWORD:
17
- return (True, "", "Logged in successfully.", True)
18
  else:
19
- return (False, "", "Invalid credentials.", False)
20
 
 
 
21
  def intern_login(email):
22
- return (True, "", f"Welcome {email}. You can now view your grades.", True)
23
 
24
  # Admin upload function
25
  def admin_upload(file):
26
  global grades_df
27
  if file is not None:
28
- grades_df = pd.read_excel(io.BytesIO(file.read()))
29
  return "File uploaded and processed successfully."
30
  else:
31
  return "Please upload a valid Excel file."
@@ -43,14 +45,15 @@ def performance_analysis():
43
  # Line chart for daily performance
44
  line_chart = px.line(daily_performance, x='Date', y='Grade', title='Daily Performance (Total Grades)')
45
 
 
46
  # Histogram of grades
47
- histogram = px.histogram(grades_df, x='Grade', nbins=20, title='Grade Distribution')
48
 
49
  # Box plot for grade statistics
50
- box_plot = px.box(grades_df, y='Grade', title='Grade Statistics')
51
 
52
  # Create a summary table
53
- summary_stats = grades_df['Grade'].describe().reset_index()
54
  summary_table = go.Figure(data=[go.Table(
55
  header=dict(values=['Statistic', 'Value']),
56
  cells=dict(values=[summary_stats['index'], summary_stats['Grade']])
@@ -60,56 +63,106 @@ def performance_analysis():
60
  else:
61
  return None, None, None, None
62
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
63
  # Create the Gradio interface
64
  with gr.Blocks() as demo:
65
  with gr.Tab("Admin Login"):
66
- admin_email = gr.Textbox(label="Admin Email")
67
- admin_password = gr.Textbox(label="Admin Password", type="password")
68
- admin_login_button = gr.Button("Login")
69
- admin_login_output = gr.Textbox(label="Admin Output")
70
- admin_file = gr.File(label="Upload Grades Excel File", visible=False)
71
- admin_submit = gr.Button("Submit", visible=False)
72
- admin_file_output = gr.Textbox(label="File Upload Output", visible=False)
73
 
74
- admin_charts = gr.Plot(visible=False)
75
- admin_histogram = gr.Plot(visible=False)
76
- admin_box_plot = gr.Plot(visible=False)
77
- admin_summary_table = gr.Plot(visible=False)
 
 
 
 
 
 
 
78
 
79
  admin_login_button.click(
80
- admin_login,
81
  inputs=[admin_email, admin_password],
82
- outputs=[admin_login_output, admin_file, admin_submit, admin_file_output],
83
- show_progress=False,
84
  )
85
  admin_submit.click(
86
  admin_upload,
87
  inputs=[admin_file],
88
  outputs=admin_file_output,
89
  )
90
- admin_submit.click(
91
  performance_analysis,
92
  outputs=[admin_charts, admin_histogram, admin_box_plot, admin_summary_table],
93
  )
94
 
95
  with gr.Tab("Intern Login"):
96
- intern_email = gr.Textbox(label="Intern Email")
97
- intern_login_button = gr.Button("Login")
98
- intern_login_output = gr.Textbox(label="Intern Output")
99
- intern_submit = gr.Button("View Grades", visible=False)
100
- intern_output = gr.Textbox(label="Grade Analysis", visible=False)
 
 
101
 
102
  intern_login_button.click(
103
  intern_login,
104
  inputs=[intern_email],
105
- outputs=[intern_login_output, intern_submit, intern_output],
106
- show_progress=False,
107
- )
108
- intern_submit.click(
109
- intern_view,
110
- inputs=[intern_email],
111
- outputs=intern_output,
112
  )
 
 
 
 
 
 
113
 
114
  # Launch the app
115
- demo.launch()
 
6
 
7
  # Define admin credentials
8
  ADMIN_EMAIL = "[email protected]"
9
+ ADMIN_PASSWORD = "admin"
10
 
11
  # Variable to store uploaded grades file in memory
12
  grades_df = None
13
 
14
  # Admin login function
15
+ def admin_login_function(email, password):
16
+ if True or (email == ADMIN_EMAIL and password == ADMIN_PASSWORD):
17
+ return gr.update(visible=False), gr.update(visible=True),
18
  else:
19
+ raise gr.Error("wrong password 💥!", duration=5)
20
 
21
+
22
+ # Intern login function
23
  def intern_login(email):
24
+ return gr.update(visible=False), gr.update(visible=True), intern_view(email)
25
 
26
  # Admin upload function
27
  def admin_upload(file):
28
  global grades_df
29
  if file is not None:
30
+ grades_df = pd.read_excel(file.name)
31
  return "File uploaded and processed successfully."
32
  else:
33
  return "Please upload a valid Excel file."
 
45
  # Line chart for daily performance
46
  line_chart = px.line(daily_performance, x='Date', y='Grade', title='Daily Performance (Total Grades)')
47
 
48
+ email_performance = grades_df.groupby('Email')['Grade'].sum().reset_index()
49
  # Histogram of grades
50
+ histogram = px.histogram(email_performance, x='Grade', nbins=20, title='Grade Distribution')
51
 
52
  # Box plot for grade statistics
53
+ box_plot = px.box(email_performance, y='Grade', title='Grade Statistics')
54
 
55
  # Create a summary table
56
+ summary_stats = email_performance['Grade'].describe().reset_index()
57
  summary_table = go.Figure(data=[go.Table(
58
  header=dict(values=['Statistic', 'Value']),
59
  cells=dict(values=[summary_stats['index'], summary_stats['Grade']])
 
63
  else:
64
  return None, None, None, None
65
 
66
+ MAX_SCORE = 100 # Assuming the maximum possible score is 100
67
+
68
+ def intern_view(email):
69
+ if grades_df is not None:
70
+ intern_grades = grades_df[grades_df['Email'] == email]
71
+ if not intern_grades.empty:
72
+ total_grade = intern_grades['Grade'].sum()
73
+ average_grade = intern_grades['Grade'].mean()
74
+ num_records = intern_grades.shape[0]
75
+
76
+ # Normalize the score to MAX_SCORE
77
+ normalized_score = min(total_grade / MAX_SCORE, 1)
78
+
79
+ # Determine the color level
80
+ if normalized_score <= 0.2:
81
+ color = "red"
82
+ elif normalized_score <= 0.4:
83
+ color = "orange"
84
+ elif normalized_score <= 0.6:
85
+ color = "yellow"
86
+ elif normalized_score <= 0.8:
87
+ color = "lightgreen"
88
+ else:
89
+ color = "green"
90
+
91
+ # Return as a Markdown formatted string
92
+ return f"""
93
+ ### Grade Analysis for {email}
94
+
95
+ - **Total Grade**: {normalized_score}
96
+ - **Number of Records**: {num_records}
97
+
98
+ <div style="background-color:{color}; padding:10px; border-radius:5px;">
99
+ **Performance Level**: {color.capitalize()}
100
+ </div>
101
+ """
102
+ else:
103
+ return "No records found for this email."
104
+ else:
105
+ return "No grades file uploaded. Please contact the admin."
106
+
107
+
108
  # Create the Gradio interface
109
  with gr.Blocks() as demo:
110
  with gr.Tab("Admin Login"):
111
+ with gr.Column(visible=True) as admin_login_section:
112
+ admin_email = gr.Textbox(label="Admin Email")
113
+ admin_password = gr.Textbox(label="Admin Password", type="password")
114
+ admin_login_button = gr.Button("Login")
115
+ admin_login_output = gr.Textbox(label="Admin Output")
 
 
116
 
117
+ with gr.Column(visible=False) as admin_upload_section:
118
+ with gr.Column() as update_file_section:
119
+ admin_file = gr.File(label="Upload Grades Excel File")
120
+ admin_submit = gr.Button("Submit")
121
+ admin_file_output = gr.Textbox(label="File Upload Output")
122
+
123
+ admin_analyze = gr.Button("Analyze")
124
+ admin_charts = gr.Plot()
125
+ admin_histogram = gr.Plot()
126
+ admin_box_plot = gr.Plot()
127
+ admin_summary_table = gr.Plot()
128
 
129
  admin_login_button.click(
130
+ admin_login_function,
131
  inputs=[admin_email, admin_password],
132
+ outputs=[admin_login_section, admin_upload_section],
133
+ show_progress=True,
134
  )
135
  admin_submit.click(
136
  admin_upload,
137
  inputs=[admin_file],
138
  outputs=admin_file_output,
139
  )
140
+ admin_analyze.click(
141
  performance_analysis,
142
  outputs=[admin_charts, admin_histogram, admin_box_plot, admin_summary_table],
143
  )
144
 
145
  with gr.Tab("Intern Login"):
146
+ with gr.Column(visible=True) as intern_login_section:
147
+ intern_email = gr.Textbox(label="Intern Email")
148
+ intern_login_button = gr.Button("Find")
149
+ intern_login_output = gr.Textbox(label="Intern Output")
150
+
151
+ with gr.Column(visible=False) as intern_info_section:
152
+ intern_output = gr.Markdown(label="Grade Analysis")
153
 
154
  intern_login_button.click(
155
  intern_login,
156
  inputs=[intern_email],
157
+ outputs=[intern_login_section, intern_info_section, intern_output],
158
+ show_progress=True,
 
 
 
 
 
159
  )
160
+
161
+ # intern_submit.click(
162
+ # intern_view,
163
+ # inputs=[intern_email],
164
+ # outputs=intern_output,
165
+ # )
166
 
167
  # Launch the app
168
+ demo.launch(debug=True)