From e496d6f7c6840c682f29740cf992b91dfe48d7b6 Mon Sep 17 00:00:00 2001 From: Sanskriti Lal Date: Mon, 17 Jun 2024 10:39:08 +0530 Subject: [PATCH] added an AI-powered system to match food donors with recipients --- matching-model/.DS_Store | Bin 0 -> 6148 bytes matching-model/convertDStoCSV.py | 22 ++++++++++++++ matching-model/main.py | 50 +++++++++++++++++++++++++++++++ 3 files changed, 72 insertions(+) create mode 100644 matching-model/.DS_Store create mode 100644 matching-model/convertDStoCSV.py create mode 100644 matching-model/main.py diff --git a/matching-model/.DS_Store b/matching-model/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..b06b76a9baf3b3579f0a1d391a586ae4e3fd4b88 GIT binary patch literal 6148 zcmeHKyG{c!5S)cbM4|~DrN1B&e_)cMppHZh2^2_yLnt88UB~C}i7@*B>Q2GaibISBA~jYr2@a9 Fz#H_!DkT5_ literal 0 HcmV?d00001 diff --git a/matching-model/convertDStoCSV.py b/matching-model/convertDStoCSV.py new file mode 100644 index 0000000..68cc9ef --- /dev/null +++ b/matching-model/convertDStoCSV.py @@ -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() + diff --git a/matching-model/main.py b/matching-model/main.py new file mode 100644 index 0000000..f461500 --- /dev/null +++ b/matching-model/main.py @@ -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']])