SIVAPRASATH commited on
Commit
5441d7b
1 Parent(s): c41aec9
Files changed (1) hide show
  1. app.py +63 -0
app.py ADDED
@@ -0,0 +1,63 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+
3
+ def calculate_cgpa(grades, credit_hours):
4
+ total_points = 0
5
+ total_credits = 0
6
+
7
+ for grade, credits in zip(grades, credit_hours):
8
+ # Convert grade to grade point based on the provided scale
9
+ if grade == 'O':
10
+ grade_point = 10
11
+ elif grade == 'A+':
12
+ grade_point = 9
13
+ elif grade == 'A':
14
+ grade_point = 8
15
+ elif grade == 'B+':
16
+ grade_point = 7
17
+ elif grade == 'B':
18
+ grade_point = 6
19
+ elif grade == 'C':
20
+ grade_point = 5
21
+ elif grade in ['F', 'W', 'I', 'Ab']:
22
+ grade_point = 0
23
+ else:
24
+ print(f"Unknown grade '{grade}' encountered. Skipping...")
25
+ continue
26
+
27
+ # Accumulate total points and total credits
28
+ total_points += grade_point * credits
29
+ total_credits += credits
30
+
31
+ # Calculate CGPA
32
+ if total_credits == 0:
33
+ return 0.0
34
+
35
+ cgpa = total_points / total_credits
36
+ return round(cgpa, 2)
37
+
38
+ def cgpa_calculator_interface(course_data):
39
+ try:
40
+ grades = []
41
+ credit_hours = []
42
+ for course in course_data:
43
+ grade = course['grade']
44
+ credits = int(course['credits'])
45
+ grades.append(grade)
46
+ credit_hours.append(credits)
47
+
48
+ cgpa = calculate_cgpa(grades, credit_hours)
49
+ return f"Your CGPA is: {cgpa}"
50
+ except ValueError:
51
+ return "Invalid input. Please ensure all credit hours are integers."
52
+
53
+ with gr.Interface(
54
+ cgpa_calculator_interface,
55
+ [
56
+ gr.Interface.Textbox(label="Grade", placeholder="Enter grade (O, A+, A, B+, etc.)"),
57
+ gr.Interface.Textbox(label="Credits", placeholder="Enter credit hours", type="number"),
58
+ ],
59
+ gr.Interface.Label(label="Add Course"),
60
+ debug=True,
61
+ title="CGPA Calculator"
62
+ ) as iface:
63
+ iface.launch(share=True)