File size: 5,770 Bytes
bfd8fda
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
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):
    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)

# CGPA calculation
def calculate_cgpa(current_cgpa, current_semesters, new_sem_gpa):
    try:
        current_total = current_cgpa * current_semesters
        new_total = current_total + new_sem_gpa
        new_cgpa = round(new_total / (current_semesters + 1), 2)
        return f"πŸ“Š Updated CGPA after this semester: {new_cgpa}"
    except Exception:
        return "❌ Error in CGPA Calculation. Please check inputs."

# Show/hide fields dynamically
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 & CGPA Calculator") as demo:
    gr.Markdown("""
    # πŸ“˜ GPA & CGPA Calculator
    
    ### πŸ“ Instructions:
    - Select the subject type (With Practical / Without Practical).
    - Enter marks:
      - **Sessionals**: out of 50 β†’ scaled to 17.5
      - **Practicals/Lab/Project**: out of 100 β†’ auto scaled
      - **End Sem**: out of 100 β†’ scaled to 35 or 50
    - βž• **Add Subject** for each subject.
    - βœ… **Calculate GPA** to see semester GPA.
    - πŸŽ“ **Calculate CGPA** to include this sem into your cumulative GPA.
    """)

    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="πŸŽ“ Semester GPA", interactive=False)

    calc_btn.click(fn=calculate_gpa, inputs=[], outputs=[gpa_out])
    clear_btn.click(fn=clear_data, inputs=[], outputs=[result, subject_table])

    gr.Markdown("---")
    gr.Markdown("## 🎯 Cumulative CGPA Calculator (Till Current Semester)")

    with gr.Row():
        prev_cgpa = gr.Number(label="Current CGPA (till last semester)", value=0)
        prev_sems = gr.Number(label="Number of completed semesters", value=0)
        current_gpa = gr.Number(label="This Semester GPA (enter manually or copy from above)", value=0)

    cgpa_btn = gr.Button("πŸŽ“ Calculate Updated CGPA")
    cgpa_out = gr.Textbox(label="πŸ“Š Final CGPA", interactive=False)

    cgpa_btn.click(fn=calculate_cgpa, inputs=[prev_cgpa, prev_sems, current_gpa], outputs=[cgpa_out])

demo.launch()