CGPA_CALCULATOR / app.py
SIVAPRASATH's picture
app.py
d14ca54 verified
raw
history blame
1.83 kB
import gradio as gr
def calculate_cgpa(grades, credit_hours):
total_points = 0
total_credits = 0
for grade, credits in zip(grades, credit_hours):
# Convert grade to grade point based on the provided scale
if grade == 'O':
grade_point = 10
elif grade == 'A+':
grade_point = 9
elif grade == 'A':
grade_point = 8
elif grade == 'B+':
grade_point = 7
elif grade == 'B':
grade_point = 6
elif grade == 'C':
grade_point = 5
elif grade in ['F', 'W', 'I', 'Ab']:
grade_point = 0
else:
print(f"Unknown grade '{grade}' encountered. Skipping...")
continue
# Accumulate total points and total credits
total_points += grade_point * credits
total_credits += credits
# Calculate CGPA
if total_credits == 0:
return 0.0
cgpa = total_points / total_credits
return round(cgpa, 2)
def cgpa_calculator_interface(course_data):
try:
grades = []
credit_hours = []
for course in course_data:
grade = course['grade']
credits = int(course['credits'])
grades.append(grade)
credit_hours.append(credits)
cgpa = calculate_cgpa(grades, credit_hours)
return f"Your CGPA is: {cgpa}"
except ValueError:
return "Invalid input. Please ensure all credit hours are integers."
gr.Interface(
cgpa_calculator_interface,
inputs = [gr.Textbox(label="Grade", placeholder="Enter grade (O, A+, A, B+, etc.)"), gr.Textbox(label="Credits", placeholder="Enter credit hours", type="number")],
#gr.Label(label="Add Course"),
#debug=True,
#title="CGPA Calculator"
outputs = "textbox"
)
iface.launch()