Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added an AI-powered system to match food donors with recipients #181

Merged
merged 1 commit into from
Jul 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added matching-model/.DS_Store
Binary file not shown.
22 changes: 22 additions & 0 deletions matching-model/convertDStoCSV.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Converts the datatset into csv file - i.e the donations table is being converted into .csv file

import pandas as pd
from your_flask_app import app, db # Import your Flask app and SQLAlchemy objects
from models import Donation # Import the Donation model

def export_to_csv():
with app.app_context():
donations = Donation.query.all()
data = [{
'food_type': donation.food_type,
'quantity': donation.quantity,
'location': donation.location,
'timestamp': donation.timestamp
} for donation in donations]

df = pd.DataFrame(data)
df.to_csv('donations_requests.csv', index=False)

# Call this function periodically, e.g., using a cron job or a scheduled task
export_to_csv()

50 changes: 50 additions & 0 deletions matching-model/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.neighbors import KNeighborsClassifier
from sklearn.metrics.pairwise import cosine_similarity

# Load the CSV file into a pandas DataFrame (assuming data is already loaded)
df = pd.read_csv('convertDStoCSV.csv')

# Preprocess the data (e.g., handle missing values, encode categorical variables)
# Assuming 'timestamp' is in string format, convert it to datetime if needed
df['timestamp'] = pd.to_datetime(df['timestamp'])

# Split the data into features (X) and target variable (y)
X = df[['food_type', 'location', 'quantity', 'timestamp']] # Features: food_type, location, quantity, timestamp
y = df['food_preferences'] # Target variable: food_preferences

# Encode categorical variables (if needed)
X_encoded = pd.get_dummies(X, columns=['food_type', 'location'], drop_first=True) # One-hot encoding for categorical variables

# Split the data into training and testing sets
X_train, X_test, y_train, y_test = train_test_split(X_encoded, y, test_size=0.2, random_state=42)

# Initialize the KNN classifier
knn = KNeighborsClassifier(n_neighbors=3) # Adjust hyperparameters as needed

# Train the classifier
knn.fit(X_train, y_train)

# Calculate similarity between donors and recipients
def calculate_similarity(row):
donor_food = row['food_type'].split(', ')
recipient_food = row['food_preferences'].split(', ')
vector_donor = [1 if food in donor_food else 0 for food in set(donor_food + recipient_food)]
vector_recipient = [1 if food in recipient_food else 0 for food in set(donor_food + recipient_food)]
similarity = cosine_similarity([vector_donor], [vector_recipient])[0][0]
return similarity

# Calculate similarity scores for the test set
X_test_similarity = X_test.apply(calculate_similarity, axis=1)

# Set a similarity threshold to filter matches
threshold = 0.5
matched_indices = X_test_similarity[X_test_similarity >= threshold].index

# Get the matched donor-recipient pairs
matched_pairs = df.iloc[matched_indices]

# Output matched pairs along with their similarity scores
print("Matched Donor-Recipient Pairs:")
print(matched_pairs[['donor_id', 'recipient_id', 'location', 'quantity']])