GradeDistVis / app.py
gsoni's picture
Update app.py
c93f217 verified
raw
history blame
9.45 kB
import gradio as gr
import pandas as pd
pd.plotting.register_matplotlib_converters()
import matplotlib.pyplot as plt
import seaborn as sns
def greet(subject, course_number):
# modify the inputs
subject = subject.upper()
# import data as csv file
grades = pd.read_csv("gradedist2025.csv")
grades = grades.rename(columns={'Course No.': 'Course_Number',
'Course Title': 'Course_Title',
'Graded Enrollment': 'Enrollment',
'A (%)': 'A',
'A- (%)': 'A-',
'B+ (%)': 'B+',
'B (%)': 'B',
'B- (%)': 'B-',
'C+ (%)': 'C+',
'C (%)': 'C',
'C- (%)': 'C-',
'D+ (%)': 'D+',
'D (%)': 'D',
'D- (%)': 'D-',
'F (%)': 'F'})
grades = grades.sort_values(by='Course_Number')
internal_grades = grades
# user interface
while True:
# filter the csv file by the input subject and course number/title
internal_grades = grades.loc[(grades.Subject == subject) & (grades.Course_Number == course_number)]
internal_grades = internal_grades.sort_values(by='GPA', ascending=False)
# class information label
print('')
print( subject + ' ' + str(internal_grades.iloc[0]['Course_Number']) + ' - ' + internal_grades.iloc[0]['Course_Title'] + ' (' + str(internal_grades.iloc[0]['Credits']) + ' Credits)')
# gpa calculation
print('')
GPA = 0.00
i = 0
while i < internal_grades.GPA.size:
GPA = GPA + internal_grades.iloc[i]['GPA']
i += 1
GPA = GPA / internal_grades.GPA.size
print("Course Average GPA: {:.3}".format(GPA))
gradedistribution = ''
print('')
# print("Grade distribution:")
A = 0
Aminus = 0
Bplus = 0
B = 0
Bminus = 0
Cplus = 0
C = 0
Cminus = 0
Dplus = 0
D = 0
Dminus = 0
F = 0
j = 0
letterchar = ['A', 'A-', 'B+', 'B', 'B-', 'C+', 'C', 'C-', 'D+', 'D', 'D-', 'F']
lettergrades = [A, Aminus, Bplus, B, Bminus, Cplus, C, Cminus, Dplus, D, Dminus, F]
val = []
for letter in (lettergrades):
i = 0
while i < internal_grades.GPA.size:
letter = letter + internal_grades.iloc[i][letterchar[j]]
i += 1
letter = letter / internal_grades.GPA.size
val.append(letter)
gradedistribution = gradedistribution + str("{}: {:.3}%".format(letterchar[j], letter))
gradedistribution = gradedistribution + str(', ')
j += 1
data = {'labels': ['A', 'A-', 'B+', 'B', 'B-', 'C+', 'C', 'C-', 'D+', 'D', 'D-', 'F'],
'values': val}
df = pd.DataFrame(data)
# plt.style.use('dark_background')
sns.set_style("whitegrid")
plt.figure(figsize=(10,10))
plt.pie(df['values'], labels=df['labels'], autopct='%1.1f%%', textprops={'fontsize': 20})
plt.title('Course Average Grade Distribution for ' + subject + ' ' + str(internal_grades.iloc[0]['Course_Number']) + ' - ' + internal_grades.iloc[0]['Course_Title'])
#plt.show()
plot = plt
# sort professors
sortedprofgradedist = ''
print('')
print("Top Professors by GPA:")
profnames = internal_grades.Instructor.unique()
profaveragegpa = []
profdict = {}
for prof in profnames:
internal_prof = internal_grades.loc[internal_grades.Instructor == prof]
GPAinternal = 0.00
i = 0
while i < internal_prof.GPA.size:
GPAinternal = GPAinternal + internal_prof.iloc[i]['GPA']
i += 1
GPAinternal = GPAinternal / internal_prof.GPA.size
profaveragegpa.append(GPAinternal)
profdict[prof] = GPAinternal
# sort profs by highest avg gpa
sortedprofs = {key: val for key, val in sorted(profdict.items(), key = lambda ele: ele[1], reverse = True)}
print(sortedprofs)
profkeys = sortedprofs.keys()
for key in profkeys:
internal_prof = internal_grades.loc[internal_grades.Instructor == key]
enrollment = 0
withdraws = 0
A = 0
Aminus = 0
Bplus = 0
B = 0
Bminus = 0
Cplus = 0
C = 0
Cminus = 0
Dplus = 0
D = 0
Dminus = 0
F = 0
i = 0
while i < internal_prof.GPA.size:
A = A + internal_prof.iloc[i]['A']
Aminus = Aminus + internal_prof.iloc[i]['A-']
Bplus = Bplus + internal_prof.iloc[i]['B+']
B = B + internal_prof.iloc[i]['B']
Bminus = Bminus + internal_prof.iloc[i]['B-']
Cplus = Cplus + internal_prof.iloc[i]['C+']
C = C + internal_prof.iloc[i]['C']
Cminus = Cminus + internal_prof.iloc[i]['C-']
Dplus = Dplus + internal_prof.iloc[i]['D+']
D = D + internal_prof.iloc[i]['D']
Dminus = Dminus + internal_prof.iloc[i]['D-']
F = F + internal_prof.iloc[i]['F']
enrollment = enrollment + internal_prof.iloc[i]['Enrollment']
withdraws = withdraws + internal_prof.iloc[i]['Withdraws']
i += 1
A = A / internal_prof.GPA.size
Aminus = Aminus / internal_prof.GPA.size
Bplus = Bplus / internal_prof.GPA.size
B = B / internal_prof.GPA.size
Bminus = Bminus / internal_prof.GPA.size
Cplus = Cplus / internal_prof.GPA.size
C = C / internal_prof.GPA.size
Cminus = Cminus / internal_prof.GPA.size
Dplus = Dplus / internal_prof.GPA.size
D = D / internal_prof.GPA.size
Dminus = Dminus / internal_prof.GPA.size
F = F / internal_prof.GPA.size
enrollment = enrollment / internal_prof.GPA.size
withdraws = withdraws / internal_prof.GPA.size
sortedprofgradedist = sortedprofgradedist + str('\nProfessor ' + key + ", Avg GPA: {:.3}".format(sortedprofs.get(key)))
if (sortedprofs.get(key) > GPA):
sortedprofgradedist = sortedprofgradedist + str(", Above average")
elif (sortedprofs.get(key) == GPA):
sortedprofgradedist = sortedprofgradedist + str(", Average")
else:
sortedprofgradedist = sortedprofgradedist + str(", Below average")
# print('')
sortedprofgradedist = sortedprofgradedist + str("\nEnrollment: {:.0f}".format(enrollment) + ', Withdraws: {:.0f}'.format(withdraws))
sortedprofgradedist = sortedprofgradedist + str("\nAvg Distribution: ")
letterchar = ['A', 'A-', 'B+', 'B', 'B-', 'C+', 'C', 'C-', 'D+', 'D', 'D-', 'F']
lettergrades = [A, Aminus, Bplus, B, Bminus, Cplus, C, Cminus, Dplus, D, Dminus, F]
j = 0
for letter in lettergrades:
sortedprofgradedist = sortedprofgradedist + str("{}: {:.3}%".format(letterchar[j], letter))
sortedprofgradedist = sortedprofgradedist + str(', ')
j += 1
sortedprofgradedist = sortedprofgradedist + str('\n')
# professors who teach the class
profswhoteach = ''
profswhoteach = profswhoteach + str('\n')
profswhoteach = profswhoteach + str("Professors who teach {}:".format(internal_grades.iloc[0]['Course_Title']))
profswhoteach = profswhoteach + str('\n')
profswhoteach = profswhoteach + str(internal_grades.Instructor.unique())
# enrollment and withdraws
print('')
return (subject + ' ' + str(internal_grades.iloc[0]['Course_Number']) + ' - ' + internal_grades.iloc[0]['Course_Title'] + ' (' + str(internal_grades.iloc[0]['Credits']) + ' Credits)') + ("\nCourse Average GPA: {:.3}".format(GPA)) + profswhoteach, gradedistribution, plot, sortedprofgradedist
# gradio interface
demo = gr.Interface(
fn=greet,
inputs=[gr.Textbox(label="Subject (Eg. ECON, chem)"), gr.Number(label="Course Number (Eg. 2005)")],
outputs=[gr.Textbox(label="Course Information"), gr.Textbox(label="Course Average Grade Distribution", lines=2), gr.Plot(label="Course Average Grade Distribution Plot"), gr.Textbox(label="Average Grade Distributions by Professors", lines=3)],
title="GradeDistVis",
description="πŸ“š Plan your courses like a PRO with GradeDistVis! πŸŽ“βœ¨ Access grade distributions and other key metrics to maximize your GPA!πŸ”₯ \n\n For Virginia Tech students,",
article="Feedback Form: https://forms.gle/G4zV25XH8tDYLsRW9",
examples = [["ECON", 2005], ["chem", 1036]],
cache_examples=True,
).queue(default_concurrency_limit=1000)
demo.launch()