| from flask import Flask, request, jsonify, send_from_directory
|
| from flask_cors import CORS
|
| import sqlite3
|
| import os
|
| from datetime import datetime, timedelta
|
| import model
|
|
|
| app = Flask(__name__, static_folder='.')
|
| CORS(app)
|
|
|
| DB_FILE = 'food_waste.db'
|
|
|
|
|
| def init_db():
|
| conn = sqlite3.connect(DB_FILE)
|
| c = conn.cursor()
|
| c.execute('''
|
| CREATE TABLE IF NOT EXISTS inventory (
|
| id INTEGER PRIMARY KEY AUTOINCREMENT,
|
| name TEXT NOT NULL,
|
| category TEXT,
|
| quantity TEXT,
|
| expiry_date DATE,
|
| added_date DATE DEFAULT CURRENT_DATE
|
| )
|
| ''')
|
| conn.commit()
|
| conn.close()
|
|
|
|
|
| if not os.path.exists(DB_FILE):
|
| init_db()
|
|
|
|
|
| @app.route('/')
|
| def serve_index():
|
| return send_from_directory('.', 'index.html')
|
|
|
| @app.route('/<path:path>')
|
| def serve_static(path):
|
| return send_from_directory('.', path)
|
|
|
|
|
|
|
| @app.route('/api/add-item', methods=['POST'])
|
| def add_item():
|
| data = request.json
|
| name = data.get('name')
|
| quantity = data.get('quantity', '1 unit')
|
| category = data.get('category', 'Uncategorized')
|
| expiry_date = data.get('expiryDate')
|
|
|
|
|
| if not expiry_date:
|
| days = model.predict_days(name)
|
| expiry_date = (datetime.now() + timedelta(days=days)).strftime('%Y-%m-%d')
|
| print(f"AI Predicted expiry for {name}: {expiry_date} ({days} days)")
|
|
|
| conn = sqlite3.connect(DB_FILE)
|
| c = conn.cursor()
|
| c.execute('INSERT INTO inventory (name, category, quantity, expiry_date) VALUES (?, ?, ?, ?)',
|
| (name, category, quantity, expiry_date))
|
| conn.commit()
|
| conn.close()
|
|
|
| return jsonify({"success": True, "message": "Item added", "expiryDate": expiry_date})
|
|
|
| @app.route('/api/get-items', methods=['GET'])
|
| def get_items():
|
| conn = sqlite3.connect(DB_FILE)
|
| conn.row_factory = sqlite3.Row
|
| c = conn.cursor()
|
| c.execute('SELECT * FROM inventory ORDER BY expiry_date ASC')
|
| rows = c.fetchall()
|
| conn.close()
|
|
|
| items = []
|
| for row in rows:
|
| items.append({
|
| "id": row['id'],
|
| "name": row['name'],
|
| "quantity": row['quantity'],
|
| "category": row['category'],
|
| "expiryDate": row['expiry_date']
|
| })
|
|
|
| return jsonify({"items": items})
|
|
|
| @app.route('/api/suggest-recipes', methods=['POST'])
|
| def suggest_recipes():
|
|
|
|
|
| return jsonify([
|
| {
|
| "title": "Smart AI Salad",
|
| "image": "https://images.unsplash.com/photo-1512621776951-a57141f2eefd?w=500",
|
| "ingredients": "Based on your Veggies"
|
| },
|
| {
|
| "title": "Leftover Stir Fry",
|
| "image": "https://images.unsplash.com/photo-1603133872878-684f208fb74b?w=500",
|
| "ingredients": "Mix of everything expiring soon"
|
| }
|
| ])
|
|
|
| @app.route('/api/dashboard-stats', methods=['GET'])
|
| def get_stats():
|
|
|
| pass
|
|
|
| if __name__ == '__main__':
|
|
|
| model.load_model()
|
| print("Server running on http://0.0.0.0:7860")
|
| app.run(host='0.0.0.0', port=7860)
|
|
|