Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,105 +1,43 @@
|
|
| 1 |
-
|
|
|
|
|
|
|
| 2 |
import pandas as pd
|
| 3 |
import numpy as np
|
| 4 |
-
from sklearn.
|
| 5 |
-
from sklearn.
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
# Features and target
|
| 23 |
-
X = data[["math_grade","programming_grade","gpa","interest"]]
|
| 24 |
-
y = data["recommended_course"]
|
| 25 |
-
|
| 26 |
-
# Train Hybrid Models
|
| 27 |
-
dt_model = DecisionTreeClassifier()
|
| 28 |
-
dt_model.fit(X, y)
|
| 29 |
-
knn_model = KNeighborsClassifier(n_neighbors=3)
|
| 30 |
-
knn_model.fit(X, y)
|
| 31 |
-
|
| 32 |
-
# Hybrid prediction function
|
| 33 |
-
def recommend_courses(math, programming, gpa, interest_text, top_n=3):
|
| 34 |
-
interest_encoded = le_interest.transform([interest_text])[0]
|
| 35 |
-
X_new = np.array([[math, programming, gpa, interest_encoded]])
|
| 36 |
|
| 37 |
-
|
| 38 |
-
|
| 39 |
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
courses = le_course.inverse_transform(top_indices)
|
| 43 |
-
return courses
|
| 44 |
-
|
| 45 |
-
# Streamlit UI
|
| 46 |
-
st.set_page_config(page_title="Advanced Course Recommender", layout="wide")
|
| 47 |
-
st.title("π Advanced University Course Recommendation System")
|
| 48 |
-
|
| 49 |
-
st.sidebar.header("Input Student Data")
|
| 50 |
-
math = st.sidebar.slider("Math Grade",0,100,70)
|
| 51 |
-
programming = st.sidebar.slider("Programming Grade",0,100,70)
|
| 52 |
-
gpa = st.sidebar.slider("GPA",0.0,4.0,3.0,0.1)
|
| 53 |
-
interest = st.sidebar.selectbox("Interest", le_interest.classes_)
|
| 54 |
-
|
| 55 |
-
if st.button("Recommend Courses"):
|
| 56 |
-
top_courses = recommend_courses(math, programming, gpa, interest, top_n=3)
|
| 57 |
-
st.success(f"β
Top 3 Recommended Courses: {', '.join(top_courses)}")
|
| 58 |
-
|
| 59 |
-
# Show student input
|
| 60 |
-
st.subheader("π Your Input Data")
|
| 61 |
-
st.table(pd.DataFrame({
|
| 62 |
-
"Math Grade":[math],
|
| 63 |
-
"Programming Grade":[programming],
|
| 64 |
-
"GPA":[gpa],
|
| 65 |
-
"Interest":[interest]
|
| 66 |
-
}))
|
| 67 |
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
c = canvas.Canvas(buffer, pagesize=(400, 400))
|
| 82 |
-
c.setFont("Helvetica-Bold", 14)
|
| 83 |
-
c.drawString(50,350,"Course Recommendation Report")
|
| 84 |
-
c.setFont("Helvetica", 12)
|
| 85 |
-
c.drawString(50,320,f"Math Grade: {math}")
|
| 86 |
-
c.drawString(50,300,f"Programming Grade: {programming}")
|
| 87 |
-
c.drawString(50,280,f"GPA: {gpa}")
|
| 88 |
-
c.drawString(50,260,f"Interest: {interest}")
|
| 89 |
-
c.drawString(50,240,"Recommended Courses:")
|
| 90 |
-
for i, course in enumerate(top_courses):
|
| 91 |
-
c.drawString(70,220-20*i,f"{i+1}. {course}")
|
| 92 |
-
c.showPage()
|
| 93 |
-
c.save()
|
| 94 |
-
buffer.seek(0)
|
| 95 |
-
st.download_button("Download PDF", buffer, file_name="recommendation_report.pdf", mime="application/pdf")
|
| 96 |
-
|
| 97 |
-
with st.expander("View Dataset"):
|
| 98 |
-
st.dataframe(data)
|
| 99 |
-
|
| 100 |
-
# Accuracy metrics
|
| 101 |
-
st.subheader("π Model Accuracy Metrics")
|
| 102 |
-
dt_acc = accuracy_score(y, dt_model.predict(X))
|
| 103 |
-
knn_acc = accuracy_score(y, knn_model.predict(X))
|
| 104 |
-
st.write(f"Decision Tree Accuracy: {dt_acc*100:.2f}%")
|
| 105 |
-
st.write(f"KNN Accuracy: {knn_acc*100:.2f}%")
|
|
|
|
| 1 |
+
|
| 2 |
+
# app.py
|
| 3 |
+
import gradio as gr
|
| 4 |
import pandas as pd
|
| 5 |
import numpy as np
|
| 6 |
+
from sklearn.metrics.pairwise import cosine_similarity
|
| 7 |
+
from sklearn.preprocessing import StandardScaler
|
| 8 |
+
|
| 9 |
+
# 1οΈβ£ Data ya courses (urugero)
|
| 10 |
+
data = pd.DataFrame({
|
| 11 |
+
'Course': ['Mathematics', 'Physics', 'Computer Science', 'Chemistry', 'Biology'],
|
| 12 |
+
'Difficulty': [3, 4, 2, 4, 3],
|
| 13 |
+
'Popularity': [5, 4, 5, 3, 4]
|
| 14 |
+
})
|
| 15 |
+
|
| 16 |
+
# 2οΈβ£ Preprocess
|
| 17 |
+
scaler = StandardScaler()
|
| 18 |
+
features = scaler.fit_transform(data[['Difficulty', 'Popularity']])
|
| 19 |
+
|
| 20 |
+
# 3οΈβ£ Recommendation function
|
| 21 |
+
def recommend(course_name, top_n=3):
|
| 22 |
+
if course_name not in data['Course'].values:
|
| 23 |
+
return "Course not found!"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
|
| 25 |
+
idx = data.index[data['Course'] == course_name][0]
|
| 26 |
+
sim_scores = cosine_similarity([features[idx]], features)[0]
|
| 27 |
|
| 28 |
+
recommended_idx = np.argsort(sim_scores)[::-1][1:top_n+1]
|
| 29 |
+
recommended_courses = data['Course'].iloc[recommended_idx].tolist()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
|
| 31 |
+
return recommended_courses
|
| 32 |
+
|
| 33 |
+
# 4οΈβ£ Gradio interface
|
| 34 |
+
iface = gr.Interface(
|
| 35 |
+
fn=recommend,
|
| 36 |
+
inputs=[gr.Dropdown(choices=data['Course'].tolist(), label="Select a course"),
|
| 37 |
+
gr.Slider(1, 5, step=1, label="Number of recommendations")],
|
| 38 |
+
outputs="text",
|
| 39 |
+
title="University Course Recommendation System",
|
| 40 |
+
description="Select a course and get similar course recommendations."
|
| 41 |
+
)
|
| 42 |
+
|
| 43 |
+
iface.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|