from flask import Flask, render_template_string, request, jsonify import os import numpy as np from werkzeug.utils import secure_filename from PIL import Image import cv2 import base64 from io import BytesIO app = Flask(__name__) # Configuration UPLOAD_FOLDER = 'uploads' os.makedirs(UPLOAD_FOLDER, exist_ok=True) ALLOWED_EXTENSIONS = {'png', 'jpg', 'jpeg'} app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER HTML_TEMPLATE = """ AI Trading Chart Analyzer

Drag & drop your trading chart

Supports JPG, PNG (Max 5MB)

""" def analyze_chart(image_path, ticker, timeframe): """Analyze trading chart and generate predictions""" # Load and preprocess image img = cv2.imread(image_path) img = cv2.resize(img, (224, 224)) # In a real app, you would use a trained model here # This is just a simulation for demonstration trend = np.random.choice(["Uptrend", "Downtrend", "Sideways"]) confidence = round(np.random.uniform(0.6, 0.9), 2) # Generate random but realistic price zones base_price = np.random.randint(100, 500) buy_zone = f"${base_price}-${base_price + np.random.randint(10, 30)}" sell_zone = f"${base_price + np.random.randint(40, 80)}-${base_price + np.random.randint(80, 120)}" # Generate analysis text analysis = ( f"The chart shows a {trend.lower()} pattern. " f"Key support appears around {buy_zone.split('-')[0]}, " f"with resistance near {sell_zone.split('-')[1]}. " f"Consider buying in the {buy_zone} range." ) return { "trend": trend, "confidence": confidence, "buy_zone": buy_zone, "sell_zone": sell_zone, "analysis": analysis } @app.route('/') def home(): return render_template_string(HTML_TEMPLATE) @app.route('/analyze', methods=['POST']) def analyze(): if 'file' not in request.files: return jsonify({"error": "No file uploaded"}), 400 file = request.files['file'] if file.filename == '': return jsonify({"error": "No file selected"}), 400 if file and file.filename.split('.')[-1].lower() in ALLOWED_EXTENSIONS: filename = secure_filename(file.filename) filepath = os.path.join(app.config['UPLOAD_FOLDER'], filename) file.save(filepath) try: result = analyze_chart( filepath, request.form.get('ticker', 'Unknown'), request.form.get('timeframe', '1D') ) os.remove(filepath) return jsonify(result) except Exception as e: if os.path.exists(filepath): os.remove(filepath) return jsonify({"error": str(e)}), 500 return jsonify({"error": "Invalid file type"}), 400 if __name__ == '__main__': app.run(debug=True)