Gpapredictor / app.py
lohith29's picture
Update app.py
032eaa2 verified
import gradio as gr
import pandas as pd
# Data store for subjects
subject_data = []
# Utility to scale marks from input total to given weight
def scale(raw_marks, weight):
return (raw_marks / 100) * weight
# Grade logic
def get_grade_point(total):
if 90 <= total <= 100:
return 10, "S"
elif 80 <= total < 90:
return 9, "A"
elif 70 <= total < 80:
return 8, "B"
elif 60 <= total < 70:
return 7, "C"
elif 50 <= total < 60:
return 6, "D"
elif 40 <= total < 50:
return 5, "E"
else:
return 0, "F"
# Add subject and calculate its grade
def add_subject(sub_type, s1, s2, mid=0, lab=0, prac=0, theory=0, open_proj=0, end_sem=0):
# Scale sessionals (entered out of 50, so multiply by 2 to get out of 100)
s1_scaled = scale(s1 * 2, 17.5)
s2_scaled = scale(s2 * 2, 17.5)
if sub_type == "With Practical":
mid_scaled = scale(mid, 10)
lab_scaled = scale(lab, 5)
prac_scaled = scale(prac, 15)
theory_scaled = scale(theory, 35)
total = s1_scaled + s2_scaled + mid_scaled + lab_scaled + prac_scaled + theory_scaled
else:
proj_scaled = scale(open_proj, 15)
end_scaled = scale(end_sem, 50)
total = s1_scaled + s2_scaled + proj_scaled + end_scaled
grade_point, grade = get_grade_point(total)
subject_data.append({
"Type": sub_type,
"Total Marks": round(total, 2),
"Grade": grade,
"Points": grade_point
})
return (
f"βœ… Subject Added: {sub_type} | Total: {round(total, 2)} | Grade: {grade}",
pd.DataFrame(subject_data)
)
# GPA calculation
def calculate_gpa():
if not subject_data:
return "⚠️ No subjects added yet."
total_points = sum(s["Points"] for s in subject_data)
gpa = round(total_points / len(subject_data), 2)
return f"πŸŽ“ Semester GPA: {gpa}"
# Reset all data
def clear_data():
subject_data.clear()
return "🧹 All subjects cleared!", pd.DataFrame(subject_data)
# Show/hide inputs based on subject type
def toggle_fields(subject_type):
show_practical = subject_type == "With Practical"
show_theory = subject_type == "Without Practical"
return (
gr.update(visible=show_practical), # mid
gr.update(visible=show_practical), # lab
gr.update(visible=show_practical), # prac
gr.update(visible=show_practical), # theory
gr.update(visible=show_theory), # open_proj
gr.update(visible=show_theory) # end_sem
)
# Gradio UI
with gr.Blocks(title="GPA Predictor") as demo:
gr.Markdown("""
# πŸ“˜ GPA Predictor
### πŸ“ Instructions:
- Select the subject type (With Practical / Without Practical).
- Enter marks **as per instructions**:
- **Sessionals**: out of 50 (scaled to 17.5 each)
- **Practical/Lab/Projects**: out of 100 (auto scaled)
- **End Sem**: out of 100 (scaled to 35 or 50)
- Press βž• **Add Subject**
- Press βœ… **Calculate GPA** after adding all subjects.
""")
subject_type = gr.Dropdown(["With Practical", "Without Practical"], label="Subject Type", value="With Practical")
with gr.Column():
s1 = gr.Number(label="Sessional-I (out of 50)", value=0)
s2 = gr.Number(label="Sessional-II (out of 50)", value=0)
mid = gr.Number(label="Mid-Sem Practical (out of 100)", value=0, visible=True)
lab = gr.Number(label="Regular Lab Performance (out of 100)", value=0, visible=True)
prac = gr.Number(label="End Sem Practical (out of 100)", value=0, visible=True)
theory = gr.Number(label="End Sem Theory (out of 100)", value=0, visible=True)
open_proj = gr.Number(label="Open-ended Project (out of 100)", value=0, visible=False)
end_sem = gr.Number(label="End Sem Theory (out of 100)", value=0, visible=False)
subject_type.change(
fn=toggle_fields,
inputs=[subject_type],
outputs=[mid, lab, prac, theory, open_proj, end_sem]
)
submit_btn = gr.Button("βž• Add Subject")
result = gr.Textbox(label="Status", interactive=False)
subject_table = gr.Dataframe(label="πŸ“Š Subjects Added", interactive=False)
submit_btn.click(
fn=add_subject,
inputs=[subject_type, s1, s2, mid, lab, prac, theory, open_proj, end_sem],
outputs=[result, subject_table]
)
with gr.Row():
calc_btn = gr.Button("βœ… Calculate GPA")
clear_btn = gr.Button("🧹 Clear All")
gpa_out = gr.Textbox(label="Final Predicted GPA", interactive=False)
calc_btn.click(fn=calculate_gpa, inputs=[], outputs=[gpa_out])
clear_btn.click(fn=clear_data, inputs=[], outputs=[result, subject_table])
demo.launch()