Spaces:
Sleeping
Sleeping
Upload 3 files
Browse filesFiles changed (3)
- basic_training_data.pyAdded+70
- course_recommender.pyAdded+208
- database_connection.pyAdded+57
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970import pandas as pd
import numpy as np
def create_basic_training_data():
"""Create basic training data for the course recommender"""
# Define available courses
courses = [
"Computer Science", "Information Technology", "Data Science",
"Software Engineering", "Cybersecurity", "Computer Engineering",
"Business Administration", "Marketing", "Finance", "Accounting",
"Psychology", "Education", "Literature", "History", "Philosophy",
"Nursing", "Medicine", "Engineering", "Architecture", "Design"
]
# Define strands
strands = ["STEM", "ABM", "HUMSS", "GAS", "TVL"]
# Define common hobbies
hobbies_list = [
"Programming", "Reading", "Sports", "Music", "Art", "Gaming",
"Photography", "Writing", "Dancing", "Cooking", "Traveling",
"Mathematics", "Science", "History", "Literature", "Technology"
]
# Generate synthetic data
np.random.seed(42) # For reproducible results
n_samples = 1000
data = []
for _ in range(n_samples):
# Generate random but realistic data
stanine = np.random.randint(1, 10)
gwa = np.random.uniform(75, 100) # GWA between 75-100
strand = np.random.choice(strands)
course = np.random.choice(courses)
hobbies = np.random.choice(hobbies_list, size=np.random.randint(1, 4), replace=False)
hobbies_str = ", ".join(hobbies)
# Generate rating based on some logic
if stanine >= 7 and gwa >= 85:
rating = np.random.choice([4, 5], p=[0.3, 0.7])
elif stanine >= 5 and gwa >= 80:
rating = np.random.choice([3, 4, 5], p=[0.2, 0.5, 0.3])
else:
rating = np.random.choice([1, 2, 3, 4], p=[0.1, 0.3, 0.4, 0.2])
count = np.random.randint(1, 10)
data.append({
'course': course,
'stanine': stanine,
'gwa': gwa,
'strand': strand,
'rating': rating,
'hobbies': hobbies_str,
'count': count
})
return pd.DataFrame(data)
def save_basic_data():
"""Save basic training data to CSV"""
df = create_basic_training_data()
df.to_csv('basic_training_data.csv', index=False)
print(f"Basic training data saved with {len(df)} samples")
return df
if __name__ == "__main__":
save_basic_data()123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208import pandas as pd
import numpy as np
from sklearn.ensemble import RandomForestClassifier
from sklearn.preprocessing import LabelEncoder, StandardScaler
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score, classification_report
import joblib
import re
from typing import List, Dict, Tuple
from database_connection import DatabaseConnection
import os
class CourseRecommender:
def __init__(self):
self.model = RandomForestClassifier(n_estimators=100, random_state=42)
self.label_encoders = {}
self.scaler = StandardScaler()
self.db_connection = DatabaseConnection()
self.is_trained = False
def preprocess_data(self, df: pd.DataFrame) -> pd.DataFrame:
"""Preprocess the data for training"""
df_processed = df.copy()
# Encode categorical variables
categorical_columns = ['strand', 'hobbies']
for col in categorical_columns:
if col not in self.label_encoders:
self.label_encoders[col] = LabelEncoder()
df_processed[col] = self.label_encoders[col].fit_transform(df_processed[col].astype(str))
else:
# Handle unseen labels by using a default value
try:
df_processed[col] = self.label_encoders[col].transform(df_processed[col].astype(str))
except ValueError:
# For unseen labels, use the most common label from training
most_common = self.label_encoders[col].classes_[0]
df_processed[col] = self.label_encoders[col].transform([most_common] * len(df_processed))
return df_processed
def extract_hobbies_features(self, hobbies: str) -> Dict[str, int]:
"""Extract features from hobbies string"""
if not hobbies or pd.isna(hobbies):
hobbies = ""
hobbies_lower = str(hobbies).lower()
# Define hobby categories
hobby_categories = {
'technical': ['programming', 'coding', 'computer', 'technology', 'software', 'gaming', 'electronics', 'math', 'mathematics'],
'creative': ['art', 'music', 'writing', 'design', 'photography', 'dancing', 'drawing', 'literature'],
'academic': ['reading', 'mathematics', 'science', 'research', 'studying', 'history', 'literature', 'books'],
'physical': ['sports', 'fitness', 'exercise', 'running', 'swimming', 'basketball', 'football', 'gym'],
'social': ['traveling', 'cooking', 'volunteering', 'community', 'leadership', 'social']
}
features = {}
for category, keywords in hobby_categories.items():
features[f'hobby_{category}'] = sum(1 for keyword in keywords if keyword in hobbies_lower)
return features
def prepare_features(self, df: pd.DataFrame) -> pd.DataFrame:
"""Prepare features for the model"""
df_features = df.copy()
# Extract hobby features
hobby_features = []
for hobbies in df['hobbies']:
features = self.extract_hobbies_features(hobbies)
hobby_features.append(features)
hobby_df = pd.DataFrame(hobby_features)
df_features = pd.concat([df_features, hobby_df], axis=1)
# Normalize GWA to 0-1 scale (75-100 -> 0-1)
df_features['gwa_normalized'] = (df_features['gwa'] - 75) / 25
# Create stanine bins
df_features['stanine_high'] = (df_features['stanine'] >= 7).astype(int)
df_features['stanine_medium'] = ((df_features['stanine'] >= 4) & (df_features['stanine'] < 7)).astype(int)
df_features['stanine_low'] = (df_features['stanine'] < 4).astype(int)
return df_features
def train_model(self, use_database: bool = True):
"""Train the recommendation model"""
print("Loading training data...")
if use_database:
# Try to get data from database first
df = self.db_connection.get_student_feedback_counts()
if df.empty:
print("No data from database, using basic training data...")
from basic_training_data import create_basic_training_data
df = create_basic_training_data()
else:
from basic_training_data import create_basic_training_data
df = create_basic_training_data()
if df.empty:
raise ValueError("No training data available")
print(f"Training with {len(df)} samples")
# Prepare features
df_features = self.prepare_features(df)
df_processed = self.preprocess_data(df_features)
# Select features for training
feature_columns = [
'stanine', 'gwa_normalized', 'strand', 'hobby_technical',
'hobby_creative', 'hobby_academic', 'hobby_physical', 'hobby_social',
'stanine_high', 'stanine_medium', 'stanine_low'
]
X = df_processed[feature_columns]
y = df_processed['course']
# Split data
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.2, random_state=42, stratify=y
)
# Scale features
X_train_scaled = self.scaler.fit_transform(X_train)
X_test_scaled = self.scaler.transform(X_test)
# Train model
self.model.fit(X_train_scaled, y_train)
# Evaluate
y_pred = self.model.predict(X_test_scaled)
accuracy = accuracy_score(y_test, y_pred)
print(f"Model accuracy: {accuracy:.3f}")
self.is_trained = True
# Save model
self.save_model()
return accuracy
def predict_course(self, stanine: int, gwa: float, strand: str, hobbies: str) -> List[Tuple[str, float]]:
"""Predict course recommendations"""
if not self.is_trained:
self.load_model()
if not self.is_trained:
raise ValueError("Model not trained. Please train the model first.")
# Create input data
input_data = pd.DataFrame({
'stanine': [stanine],
'gwa': [gwa],
'strand': [strand],
'hobbies': [hobbies]
})
# Prepare features
input_features = self.prepare_features(input_data)
input_processed = self.preprocess_data(input_features)
# Select same features as training
feature_columns = [
'stanine', 'gwa_normalized', 'strand', 'hobby_technical',
'hobby_creative', 'hobby_academic', 'hobby_physical', 'hobby_social',
'stanine_high', 'stanine_medium', 'stanine_low'
]
X = input_processed[feature_columns]
X_scaled = self.scaler.transform(X)
# Get predictions with probabilities
probabilities = self.model.predict_proba(X_scaled)[0]
classes = self.model.classes_
# Get top 5 recommendations
top_indices = np.argsort(probabilities)[-5:][::-1]
recommendations = [(classes[i], probabilities[i]) for i in top_indices]
return recommendations
def save_model(self):
"""Save the trained model and encoders"""
os.makedirs('models', exist_ok=True)
joblib.dump(self.model, 'models/course_recommender_model.pkl')
joblib.dump(self.label_encoders, 'models/label_encoders.pkl')
joblib.dump(self.scaler, 'models/scaler.pkl')
print("Model saved successfully")
def load_model(self):
"""Load the trained model and encoders"""
try:
self.model = joblib.load('models/course_recommender_model.pkl')
self.label_encoders = joblib.load('models/label_encoders.pkl')
self.scaler = joblib.load('models/scaler.pkl')
self.is_trained = True
print("Model loaded successfully")
except FileNotFoundError:
print("No saved model found. Please train the model first.")
self.is_trained = False
def add_feedback(self, course: str, stanine: int, gwa: float, strand: str,
rating: int, hobbies: str) -> bool:
"""Add user feedback to the database"""
return self.db_connection.add_feedback(course, stanine, gwa, strand, rating, hobbies)123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657import requests
import pandas as pd
from typing import Dict, List, Optional
import json
class DatabaseConnection:
def __init__(self, base_url: str = "https://database-dhe2.onrender.com"):
self.base_url = base_url
self.session = requests.Session()
def get_student_feedback_counts(self) -> pd.DataFrame:
"""Fetch student feedback data from the database"""
try:
url = f"{self.base_url}/student_feedback_counts"
response = self.session.get(url)
response.raise_for_status()
data = response.json()
if isinstance(data, list):
return pd.DataFrame(data)
else:
return pd.DataFrame([data])
except Exception as e:
print(f"Error fetching data: {e}")
return pd.DataFrame()
def add_feedback(self, course: str, stanine: int, gwa: float, strand: str,
rating: int, hobbies: str) -> bool:
"""Add new feedback to the database"""
try:
url = f"{self.base_url}/student_feedback_counts"
data = {
"course": course,
"stanine": stanine,
"gwa": gwa,
"strand": strand,
"rating": rating,
"hobbies": hobbies
}
response = self.session.post(url, json=data)
response.raise_for_status()
return True
except Exception as e:
print(f"Error adding feedback: {e}")
return False
def update_feedback_count(self, feedback_id: int, count: int) -> bool:
"""Update the count for existing feedback"""
try:
url = f"{self.base_url}/student_feedback_counts/{feedback_id}"
data = {"count": count}
response = self.session.put(url, json=data)
response.raise_for_status()
return True
except Exception as e:
print(f"Error updating feedback count: {e}")
return Falsebasic_training_data.pyAdded+70
course_recommender.pyAdded+208
database_connection.pyAdded+57