File size: 4,752 Bytes
14c5feb
bf7cd8b
14c5feb
6e53b35
497c85b
 
6e53b35
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
14c5feb
6e53b35
14c5feb
6e53b35
497c85b
bf7cd8b
 
6e53b35
b26964e
497c85b
b26964e
 
 
 
 
497c85b
b26964e
 
 
497c85b
 
 
 
bf7cd8b
497c85b
 
 
bf7cd8b
 
 
 
497c85b
6e53b35
497c85b
 
6e53b35
 
497c85b
6e53b35
497c85b
6e53b35
497c85b
 
bf7cd8b
497c85b
6e53b35
00ca8b5
 
 
 
 
 
 
 
 
 
 
 
6e53b35
032eaa2
497c85b
032eaa2
b26964e
6e53b35
 
 
 
 
 
 
 
497c85b
 
6e53b35
497c85b
 
b26964e
 
00ca8b5
6e53b35
00ca8b5
 
 
6e53b35
00ca8b5
 
 
 
 
 
497c85b
 
 
bf7cd8b
497c85b
 
 
 
bf7cd8b
497c85b
 
 
 
 
 
032eaa2
497c85b
 
bf7cd8b
497c85b
 
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
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()