from flask import Flask, render_template, request, jsonify import os import json import datetime import google.generativeai as genai from dotenv import load_dotenv import os load_dotenv() app = Flask(__name__) @app.route('/') def index(): return render_template('index.html') @app.route('/analyze', methods=['POST']) def analyze(): try: # Gather crop data into a list of dictionaries crops_data = [] crop_count = int(request.form.get('crop_count', 0)) for i in range(1, crop_count + 1): crop_name = request.form.get(f'crop_name_{i}') if crop_name: crop = { 'name': crop_name, 'quantity': request.form.get(f'crop_quantity_{i}'), 'unit': request.form.get(f'crop_unit_{i}'), 'harvest_date': request.form.get(f'harvest_date_{i}') } crops_data.append(crop) warehouse_size = request.form.get('warehouse_size') warehouse_unit = request.form.get('warehouse_unit') language = request.form.get('language', 'English') # Calculate today's date for the day-wise planner (next 7 days) current_date = datetime.datetime.now() future_dates = [current_date + datetime.timedelta(days=x) for x in range(7)] date_strings = [date.strftime("%d %b %Y") for date in future_dates] # Enhanced prompt with a complete day-wise planner and detailed spoilage symptoms & important points prompt = f""" Analyze these crops for post-harvest storage in {language}: json {json.dumps(crops_data, indent=2)} Warehouse size: {warehouse_size} {warehouse_unit} Create a practical, visual storage plan that includes: 1. *STORAGE SUMMARY:* Start with 3-4 bullet points summarizing key actions. 2. *CROP CARDS:* For each crop, create a visually distinct "card" section with: - Crop name as heading - Quantity and harvest date - Optimal temperature and humidity (exact numbers) - Expected shelf life in days and calculated expiry date - 3-4 specific spoilage symptoms and important points detailing how spoilage affects the crop - Recommendations for crop placement (e.g., shelf area or storage zone) - Necessary precautions and measures to prevent spoilage - Estimated space requirements 3. *STORAGE PRIORITY:* List crops in order of storage priority based on perishability. 4. *VISUAL STORAGE LAYOUT:* Create a markdown table that shows crop placement in the warehouse with shelf/area names. 5. *MONITORING PLANNER:* Provide a complete day-wise planner for the next 7 days. Specify daily tasks and monitoring details for these dates: {', '.join(date_strings)} *IMPORTANT FORMATTING REQUIREMENTS:* - Use short, simple sentences (max 10-15 words each) - Use markdown headers (##, ###) for clear section breaks - Present each crop as a visually distinct "card" section - Use emoji icons where appropriate (🌽 🌾 🥔 etc.) - Bold important numbers and dates - Provide only practical advice that farmers can immediately apply Format your response as clean markdown optimized for card-based visual presentation. """ # Configure Gemini API api_key = os.getenv("GEMINI_API") genai.configure(api_key=api_key) model = genai.GenerativeModel(model_name='gemini-1.5-flash') response = model.generate_content(prompt) return jsonify({'success': True, 'analysis': response.text}) except Exception as e: return jsonify({'success': False, 'error': str(e)}) if __name__ == '__main__': app.run(debug=True)