Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,4 +1,4 @@
|
|
1 |
-
import
|
2 |
import pandas as pd
|
3 |
|
4 |
# Grade to grade point mapping
|
@@ -9,96 +9,69 @@ grade_points = {
|
|
9 |
"D+": 1.3, "D": 1.0, "F": 0.0
|
10 |
}
|
11 |
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
# Title and description
|
16 |
-
st.markdown("""
|
17 |
-
<h1 style='text-align: center; color: #4CAF50;'>π GPA / CGPA Calculator</h1>
|
18 |
-
<p style='text-align: center;'>Add your current subjects and optionally previous semester GPA to calculate your CGPA</p>
|
19 |
-
""", unsafe_allow_html=True)
|
20 |
-
|
21 |
-
# Initialize session state
|
22 |
-
if "subjects" not in st.session_state:
|
23 |
-
st.session_state.subjects = []
|
24 |
-
|
25 |
-
# Optional previous semester data
|
26 |
-
with st.expander("π Add Previous Semester Data (Optional)", expanded=False):
|
27 |
-
prev_credits = st.number_input("Total Credit Hours from Previous Semesters",
|
28 |
-
min_value=0.0, step=0.5, value=0.0)
|
29 |
-
prev_gpa = st.number_input("Cumulative GPA from Previous Semesters",
|
30 |
-
min_value=0.0, max_value=4.0, step=0.01, value=0.0)
|
31 |
-
|
32 |
-
# Subject form
|
33 |
-
with st.form("subject_form", clear_on_submit=True):
|
34 |
-
col1, col2, col3 = st.columns(3)
|
35 |
-
with col1:
|
36 |
-
subject = st.text_input("Subject Name", placeholder="e.g., Mathematics")
|
37 |
-
with col2:
|
38 |
-
grade = st.selectbox("Grade", list(grade_points.keys()))
|
39 |
-
with col3:
|
40 |
-
credit = st.number_input("Credit Hours", min_value=0.5, max_value=6.0, step=0.5, value=3.0)
|
41 |
-
|
42 |
-
submitted = st.form_submit_button("β Add Subject")
|
43 |
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
else:
|
50 |
-
st.session_state.subjects.append({
|
51 |
-
"Subject": subject.strip(),
|
52 |
-
"Grade": grade,
|
53 |
-
"Credit": credit
|
54 |
-
})
|
55 |
-
st.success(f"Added {subject.strip()} ({grade}, {credit} credits)")
|
56 |
-
|
57 |
-
# Display current subjects
|
58 |
-
if st.session_state.subjects:
|
59 |
-
st.markdown("### π§Ύ Current Semester Subjects")
|
60 |
-
df = pd.DataFrame(st.session_state.subjects)
|
61 |
-
st.dataframe(df, use_container_width=True, hide_index=True)
|
62 |
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
if prev_credits > 0:
|
71 |
-
total_credits = prev_credits + current_credits
|
72 |
-
total_points = (prev_gpa * prev_credits) + current_points
|
73 |
-
cgpa = round(total_points / total_credits, 2)
|
74 |
-
else:
|
75 |
-
cgpa = current_gpa
|
76 |
-
|
77 |
-
# Display results in a nice card
|
78 |
-
st.markdown(f"""
|
79 |
-
<div style="background-color:#f0f2f6;padding:20px;border-radius:10px;margin-top:20px;">
|
80 |
-
<h3>π Current Semester GPA: <span style="color:#2196F3;">{current_gpa:.2f}</span></h3>
|
81 |
-
<h3>π Cumulative GPA (CGPA): <span style="color:#4CAF50;">{cgpa:.2f}</span></h3>
|
82 |
-
<p>Total Credits: {current_credits + prev_credits}</p>
|
83 |
-
</div>
|
84 |
-
""", unsafe_allow_html=True)
|
85 |
-
|
86 |
-
except ZeroDivisionError:
|
87 |
-
st.error("Error: Cannot calculate GPA with zero credit hours")
|
88 |
-
|
89 |
-
else:
|
90 |
-
st.info("βΉοΈ Add at least one subject to calculate your GPA")
|
91 |
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
97 |
|
98 |
-
|
99 |
-
st.markdown("---")
|
100 |
-
st.markdown("""
|
101 |
-
<div style="text-align: center; color: #777; font-size: 0.9em;">
|
102 |
-
<p>GPA Calculator β’ Grades: A (4.0) to F (0.0)</p>
|
103 |
-
</div>
|
104 |
-
""", unsafe_allow_html=True)
|
|
|
1 |
+
import gradio as gr
|
2 |
import pandas as pd
|
3 |
|
4 |
# Grade to grade point mapping
|
|
|
9 |
"D+": 1.3, "D": 1.0, "F": 0.0
|
10 |
}
|
11 |
|
12 |
+
def calculate_gpa(subjects, prev_credits=0, prev_gpa=0):
|
13 |
+
if not subjects:
|
14 |
+
return 0, 0
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
15 |
|
16 |
+
df = pd.DataFrame(subjects)
|
17 |
+
df['Points'] = df['Grade'].map(grade_points) * df['Credit']
|
18 |
+
current_points = df['Points'].sum()
|
19 |
+
current_credits = df['Credit'].sum()
|
20 |
+
current_gpa = round(current_points / current_credits, 2) if current_credits > 0 else 0
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
21 |
|
22 |
+
if prev_credits > 0:
|
23 |
+
total_credits = prev_credits + current_credits
|
24 |
+
cgpa = round((prev_gpa * prev_credits + current_points) / total_credits, 2)
|
25 |
+
else:
|
26 |
+
cgpa = current_gpa
|
27 |
+
|
28 |
+
return current_gpa, cgpa
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
29 |
|
30 |
+
with gr.Blocks(title="GPA Calculator") as demo:
|
31 |
+
gr.Markdown("# π GPA/CGPA Calculator")
|
32 |
+
|
33 |
+
with gr.Row():
|
34 |
+
with gr.Column():
|
35 |
+
subject = gr.Textbox(label="Subject Name")
|
36 |
+
grade = gr.Dropdown(list(grade_points.keys()), label="Grade")
|
37 |
+
credit = gr.Number(3.0, label="Credit Hours", minimum=0.5, step=0.5)
|
38 |
+
add_btn = gr.Button("Add Subject")
|
39 |
+
|
40 |
+
with gr.Column():
|
41 |
+
subjects_table = gr.Dataframe(headers=["Subject", "Grade", "Credit"])
|
42 |
+
clear_btn = gr.Button("Clear All")
|
43 |
+
|
44 |
+
with gr.Accordion("Previous Semester Data (Optional)", open=False):
|
45 |
+
prev_credits = gr.Number(0, label="Previous Total Credits")
|
46 |
+
prev_gpa = gr.Number(0, label="Previous GPA", minimum=0, maximum=4.0)
|
47 |
+
|
48 |
+
calculate_btn = gr.Button("Calculate GPA")
|
49 |
+
|
50 |
+
with gr.Row():
|
51 |
+
current_gpa = gr.Number(label="Current Semester GPA")
|
52 |
+
overall_cgpa = gr.Number(label="Overall CGPA")
|
53 |
+
|
54 |
+
subjects = gr.State([])
|
55 |
+
|
56 |
+
def add_subject(subject, grade, credit, existing_subjects):
|
57 |
+
new_subject = {"Subject": subject, "Grade": grade, "Credit": credit}
|
58 |
+
return existing_subjects + [new_subject], pd.DataFrame(existing_subjects + [new_subject])
|
59 |
+
|
60 |
+
add_btn.click(
|
61 |
+
add_subject,
|
62 |
+
inputs=[subject, grade, credit, subjects],
|
63 |
+
outputs=[subjects, subjects_table]
|
64 |
+
)
|
65 |
+
|
66 |
+
clear_btn.click(
|
67 |
+
lambda: ([], pd.DataFrame(columns=["Subject", "Grade", "Credit"])),
|
68 |
+
outputs=[subjects, subjects_table]
|
69 |
+
)
|
70 |
+
|
71 |
+
calculate_btn.click(
|
72 |
+
calculate_gpa,
|
73 |
+
inputs=[subjects, prev_credits, prev_gpa],
|
74 |
+
outputs=[current_gpa, overall_cgpa]
|
75 |
+
)
|
76 |
|
77 |
+
demo.launch()
|
|
|
|
|
|
|
|
|
|
|
|