import os import io import json import base64 import matplotlib.pyplot as plt import pandas as pd from datetime import datetime from dotenv import load_dotenv load_dotenv() # Gemini SDK (New google-genai) GEMINI_AVAILABLE = False try: from google import genai as _genai_sdk from google.genai import types GEMINI_AVAILABLE = True except ImportError: pass class LLMVisionEngine: def __init__(self): self.api_key = os.getenv("GEMINI_API_KEY") self.client = None self.model = "gemini-2.0-flash" # Best for vision/speed if GEMINI_AVAILABLE and self.api_key: try: self.client = _genai_sdk.Client(api_key=self.api_key) except Exception as e: print(f"[Vision] Init error: {e}") def analyze_chart(self, ticker: str, df: pd.DataFrame) -> dict: """ Generates a chart image and asks Gemini to interpret the patterns. """ if not self.client: return {"error": "Gemini Vision unavailable"} # 1. Generate the Chart img_bytes = self._generate_chart_bytes(ticker, df) # 2. Prepare Prompt prompt = f""" Analyze this stock chart for {ticker}. Identify key technical patterns (e.g., support/resistance, breakouts, trendlines). Predict the likely direction for the next 5-10 bars. Respond ONLY with this JSON format: {{ "direction": "bullish|bearish|neutral", "score": , "reasoning": "", "patterns": ["pattern1", "pattern2"] }} """ try: # 3. Send to Gemini response = self.client.models.generate_content( model=self.model, contents=[ prompt, types.Part.from_bytes(data=img_bytes, mime_type="image/png") ] ) text = response.text.strip() if "```" in text: text = text.split("```")[1] if text.startswith("json"): text = text[4:] text = text.strip() prediction = json.loads(text) prediction["source"] = f"gemini-vision/{self.model}" return prediction except Exception as e: print(f"[Vision] API error: {e}") return {"error": str(e)} def _generate_chart_bytes(self, ticker: str, df: pd.DataFrame) -> bytes: """Helper to create a matplotlib chart and return raw bytes.""" plt.figure(figsize=(10, 6)) # Plot Close price plt.plot(df.index, df['Close'], label='Close', color='#2196F3', linewidth=2) # Add a few key indicators if present if 'SMA_20' in df.columns: plt.plot(df.index, df['SMA_20'], label='SMA 20', color='#FF9800', linestyle='--', alpha=0.7) if 'SMA_50' in df.columns: plt.plot(df.index, df['SMA_50'], label='SMA 50', color='#E91E63', linestyle='--', alpha=0.7) plt.title(f"{ticker} - Market Analysis Graph", color='white', fontsize=14) plt.grid(True, alpha=0.2) plt.legend() # Style for Dark Mode (matching UI) plt.gca().set_facecolor('#0f172a') plt.gcf().set_facecolor('#0f172a') plt.tick_params(colors='white') for spine in plt.gca().spines.values(): spine.set_color('#334155') # Save to buffer buf = io.BytesIO() plt.savefig(buf, format='png', bbox_inches='tight', facecolor='#0f172a') plt.close() return buf.getvalue()