hbhamzabaig commited on
Commit
2cb9b91
·
verified ·
1 Parent(s): cd281b7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +71 -67
app.py CHANGED
@@ -1,93 +1,97 @@
1
  import gradio as gr
 
2
 
3
- # Map percentage to GPA and color
4
- def percentage_to_gpa(percent):
5
  if percent >= 90:
6
- return 4.00, "#00c853" # A+ Green
7
  elif percent >= 85:
8
- return 4.00, "#00c853" # A Green
9
  elif percent >= 80:
10
- return 3.66 + (percent - 80) * (3.99 - 3.66) / (84.99 - 80), "#64dd17" # A- Light Green
11
  elif percent >= 75:
12
- return 3.33 + (percent - 75) * (3.65 - 3.33) / (79.99 - 75), "#c6ff00" # B+ Yellow-Green
13
  elif percent >= 71:
14
- return 3.00 + (percent - 71) * (3.32 - 3.00) / (74.99 - 71), "#ffeb3b" # B Yellow
15
  elif percent >= 68:
16
- return 2.66 + (percent - 68) * (2.99 - 2.66) / (70.99 - 68), "#ffc107" # B- Orange
17
  elif percent >= 61:
18
- return 2.00 + (percent - 61) * (2.65 - 2.00) / (67.99 - 61), "#ff9800" # C Orange
19
  elif percent >= 50:
20
- return 1.00 + (percent - 50) * (1.99 - 1.00) / (60.99 - 50), "#f44336" # D Red
21
  else:
22
- return 0.00, "#b71c1c" # F Dark Red
23
 
24
- # GPA calculation
25
- def calculate_gpa(w_assign, w_quiz, w_gdb, w_mid, w_final,
26
- p_assign, p_quiz, p_gdb, p_mid, p_final):
27
- weights_sum = w_assign + w_quiz + w_gdb + w_mid + w_final
28
- if weights_sum != 100:
29
- return "⚠️ Total weightage must be 100%", "#000000"
 
 
 
 
 
 
 
 
 
 
 
 
 
30
 
31
- total_score = (
32
- w_assign * p_assign +
33
- w_quiz * p_quiz +
34
- w_gdb * p_gdb +
35
- w_mid * p_mid +
36
- w_final * p_final
37
- ) / 100
 
 
 
 
 
 
 
 
 
 
 
38
 
39
- gpa, color = percentage_to_gpa(total_score)
40
- return f"{gpa:.2f}", color
41
-
42
- # Build Gradio interface
43
- with gr.Blocks(title="GPA Calculator") as app:
 
 
 
 
 
 
 
 
 
 
44
 
45
- # Custom CSS for HUGE GPA display
46
  app.css = """
47
  #gpa_big textarea {
48
  font-size: 120px !important;
49
  font-weight: bold;
50
  text-align: center;
 
51
  }
52
  """
53
 
54
- gr.Markdown("## 📊 GPA Calculator (Out of 4)")
55
- gr.Markdown("Enter weightages and achieved percentages. Use the slider for Final Term. GPA updates live!")
56
-
57
- options = [5, 10, 15, 20, 30, 50, 60] # Drop-down options for achieved percentages
58
-
59
- with gr.Row():
60
- # Weightages column
61
- with gr.Column():
62
- gr.Markdown("### Weightage (%)")
63
- w_assign = gr.Number(value=10, label="Assignment")
64
- w_quiz = gr.Number(value=10, label="Quiz")
65
- w_gdb = gr.Number(value=10, label="GDB")
66
- w_mid = gr.Number(value=30, label="Mid Term")
67
- w_final = gr.Number(value=40, label="Final Term")
68
-
69
- # Achieved percentages column
70
- with gr.Column():
71
- gr.Markdown("### Achieved (%)")
72
- p_assign = gr.Dropdown(label="Assignment", choices=options, value=50)
73
- p_quiz = gr.Dropdown(label="Quiz", choices=options, value=50)
74
- p_gdb = gr.Dropdown(label="GDB", choices=options, value=50)
75
- p_mid = gr.Dropdown(label="Mid Term", choices=options, value=50)
76
- p_final = gr.Slider(minimum=0, maximum=100, value=50, step=1, label="Final Term %")
77
-
78
- # GPA column
79
- with gr.Column():
80
- gr.Markdown("### 🎓 GPA")
81
- gpa_display = gr.Textbox(label="", interactive=False, value="0.00", elem_id="gpa_big", placeholder="GPA", lines=1)
82
-
83
- inputs = [w_assign, w_quiz, w_gdb, w_mid, w_final,
84
- p_assign, p_quiz, p_gdb, p_mid, p_final]
85
-
86
- def update_gpa(*args):
87
- result, color = calculate_gpa(*args)
88
- return gr.update(value=result, style={"color": color})
89
 
90
- for inp in inputs:
91
- inp.change(update_gpa, inputs=inputs, outputs=gpa_display)
92
 
93
  app.launch()
 
1
  import gradio as gr
2
+ import pandas as pd
3
 
4
+ # Grading scheme mapping function
5
+ def percentage_to_gpa_letter(percent):
6
  if percent >= 90:
7
+ return 4.0, "A+"
8
  elif percent >= 85:
9
+ return 4.0, "A"
10
  elif percent >= 80:
11
+ return 3.66, "A-"
12
  elif percent >= 75:
13
+ return 3.33, "B+"
14
  elif percent >= 71:
15
+ return 3.0, "B"
16
  elif percent >= 68:
17
+ return 2.66, "B-"
18
  elif percent >= 61:
19
+ return 2.0, "C"
20
  elif percent >= 50:
21
+ return 1.0, "D"
22
  else:
23
+ return 0.0, "F"
24
 
25
+ # Calculate projected GPA based on weightage and achieved percentages
26
+ def calculate_projected_gpa(df):
27
+ total_score = 0
28
+ total_weight = 0
29
+ for _, row in df.iterrows():
30
+ # Calculate total score based on weightage and achieved %
31
+ total_weight_row = row['Assignment Weight'] + row['Quiz Weight'] + row['GDB Weight'] + row['Midterm Weight'] + row['Final Weight']
32
+ achieved = (row['Assignment Weight']*row['Assignment Achieved'] +
33
+ row['Quiz Weight']*row['Quiz Achieved'] +
34
+ row['GDB Weight']*row['GDB Achieved'] +
35
+ row['Midterm Weight']*row['Midterm Achieved'] +
36
+ row['Final Weight']*row['Final Achieved']) / total_weight_row
37
+ gpa, grade = percentage_to_gpa_letter(achieved)
38
+ df.at[_, 'GPA'] = gpa
39
+ df.at[_, 'Grade'] = grade
40
+ total_score += gpa
41
+ total_weight += 1
42
+ projected_gpa = total_score / total_weight if total_weight != 0 else 0
43
+ return df, f"{projected_gpa:.2f}"
44
 
45
+ # Default course table
46
+ data = {
47
+ "Include": [True]*5,
48
+ "Course Code": ["CS401","CS403","CS502","CS604","MTH501"],
49
+ "Assignment Weight": [10,10,10,10,10],
50
+ "Quiz Weight": [10,10,10,10,10],
51
+ "GDB Weight": [10,10,10,10,10],
52
+ "Midterm Weight": [30,30,30,30,30],
53
+ "Final Weight": [40,40,40,40,40],
54
+ "Assignment Achieved": [80,70,90,85,75],
55
+ "Quiz Achieved": [85,75,95,80,70],
56
+ "GDB Achieved": [90,80,85,90,75],
57
+ "Midterm Achieved": [75,70,80,85,70],
58
+ "Final Achieved": [50,50,50,50,50], # Default slider value
59
+ "GPA": [0]*5,
60
+ "Grade": [""]*5
61
+ }
62
+ df = pd.DataFrame(data)
63
 
64
+ # Gradio app
65
+ with gr.Blocks() as app:
66
+ gr.Markdown("## 📊 Projected CGPA Calculator (Weighted Scores)")
67
+ with gr.Row():
68
+ with gr.Column(scale=3):
69
+ table = gr.Dataframe(
70
+ value=df,
71
+ headers=list(df.columns),
72
+ datatype=["bool","str"] + ["number"]*10 + ["number","str"],
73
+ row_count=(5,"dynamic"),
74
+ interactive=True
75
+ )
76
+ with gr.Column(scale=1):
77
+ gr.Markdown("### 🎓 Projected CGPA")
78
+ cgpa_display = gr.Textbox(value="0.00", interactive=False, elem_id="gpa_big", lines=1)
79
 
80
+ # Slider style for Final Achieved
81
  app.css = """
82
  #gpa_big textarea {
83
  font-size: 120px !important;
84
  font-weight: bold;
85
  text-align: center;
86
+ color: #1a73e8;
87
  }
88
  """
89
 
90
+ # Update GPA function
91
+ def update_table(df):
92
+ df, projected_gpa = calculate_projected_gpa(df)
93
+ return df, projected_gpa
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
94
 
95
+ table.change(update_table, inputs=table, outputs=[table,cgpa_display])
 
96
 
97
  app.launch()