This Flask API predicts the price of a food item based on its demand and season.
- Flask
- pandas
- scikit-learn
-
Create a CSV dataset:
- The dataset should have columns for "Food Type" (product name), "Demand" (Low, Medium, High), "Season" (Spring, Summer, Autumn, Winter), "Expiration Date", and "Price".
- Replace "your_dataset.csv" in the code with the actual filename of your dataset.
-
Run the API:
- Save the code as
food_price_prediction.py
. - Open a terminal and navigate to the directory containing the file.
- Run the following command:
python farefinale.py
- This will start the API on
http://localhost:5000/predict
.
- Save the code as
-
Send a prediction request:
- Use a tool like Postman or curl to send a POST request to
http://localhost:5000/predict
with the following JSON data:
{ "product_name": "Apple", "demand": "Medium", "season": "Summer" }
- Replace "Apple" with the desired product name, "Medium" with the demand level (Low, Medium, High), and "Summer" with the season (Spring, Summer, Autumn, Winter).
- Use a tool like Postman or curl to send a POST request to
-
Response:
- The API will respond with a JSON object containing the predicted price or an error message if the model is not trained for the specified product.
{
"predicted_price": 12.50
}
{
"error": "Model not trained for product Pear"
}
- The API uses a Random Forest Regressor model for prediction.
- The model considers the demand and season when adjusting the predicted price.
- You can modify the demand and season multipliers in the
predict_price
function to customize the price adjustment logic.
➜ ~ curl -X POST http://localhost:5000/predict \
-H "Content-Type: application/json" \
-d '{
"items": [
{"product_name": "Chips", "demand": "Medium", "season": "Summer"},
{"product_name": "Instant Noodles", "demand": "High", "season": "Winter"},
{"product_name": "Popcorn", "demand": "Low", "season": "Spring"}
]
}'
"predictions": [
{
"predicted_price": 2.1375,
"product_name": "Chips"
},
{
"predicted_price": 2.0,
"product_name": "Instant Noodles"
},
{
"predicted_price": 1.6829999999999972,
"product_name": "Popcorn"
}
]
}