mgbam commited on
Commit
ddaf3fc
·
verified ·
1 Parent(s): f29497b

Create gemini_analyzer.py

Browse files
Files changed (1) hide show
  1. app/gemini_analyzer.py +90 -0
app/gemini_analyzer.py ADDED
@@ -0,0 +1,90 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ A sophisticated analyzer using the Google Gemini Pro API.
3
+
4
+ This module provides structured analysis of financial text, including:
5
+ - Nuanced sentiment with reasoning.
6
+ - Key entity extraction (e.g., cryptocurrencies).
7
+ - Topic classification.
8
+ - Potential market impact assessment.
9
+ """
10
+ import os
11
+ import logging
12
+ import httpx
13
+ from typing import Optional, TypedDict, List
14
+
15
+ # Configure logging
16
+ logger = logging.getLogger(__name__)
17
+
18
+ # --- Pydantic-like models for structured output ---
19
+ class AnalysisResult(TypedDict):
20
+ sentiment: str
21
+ sentiment_score: float
22
+ reason: str
23
+ entities: List[str]
24
+ topic: str
25
+ impact: str
26
+ summary: str
27
+ error: Optional[str]
28
+
29
+ class GeminiAnalyzer:
30
+ """Manages interaction with the Google Gemini API for deep text analysis."""
31
+
32
+ API_URL = "https://generativelanguage.googleapis.com/v1beta/models/gemini-1.5-pro-latest:generateContent"
33
+
34
+ def __init__(self, client: httpx.AsyncClient, api_key: Optional[str] = None):
35
+ self.client = client
36
+ self.api_key = api_key or os.getenv("GEMINI_API_KEY")
37
+ if not self.api_key:
38
+ raise ValueError("GEMINI_API_KEY is not set. Please add it as a repository secret.")
39
+ self.params = {"key": self.api_key}
40
+ self.headers = {"Content-Type": "application/json"}
41
+
42
+ def _build_prompt(self, text: str) -> dict:
43
+ """Creates the structured JSON prompt for the Gemini API."""
44
+ # This is where the magic happens. We're "prompt engineering" Gemini.
45
+ return {
46
+ "contents": [{
47
+ "parts": [{
48
+ "text": f"""
49
+ Analyze the following financial text from the cryptocurrency world.
50
+ Provide your analysis as a single, minified JSON object with NO markdown formatting.
51
+
52
+ The JSON object must have these exact keys: "sentiment", "sentiment_score", "reason", "entities", "topic", "impact", "summary".
53
+
54
+ - "sentiment": MUST be one of "POSITIVE", "NEGATIVE", or "NEUTRAL".
55
+ - "sentiment_score": A float between -1.0 (very negative) and 1.0 (very positive).
56
+ - "reason": A brief, one-sentence explanation for the sentiment score.
57
+ - "entities": A JSON array of strings listing the primary cryptocurrencies or tokens mentioned (e.g., ["Bitcoin", "ETH"]).
58
+ - "topic": MUST be one of "Regulation", "Partnership", "Technical Update", "Market Hype", "Security", or "General News".
59
+ - "impact": Assess the potential short-term market impact. MUST be one of "LOW", "MEDIUM", or "HIGH".
60
+ - "summary": A concise, one-sentence summary of the provided text.
61
+
62
+ Text to analyze: "{text}"
63
+ """
64
+ }]
65
+ }]
66
+ }
67
+
68
+ async def analyze_text(self, text: str) -> AnalysisResult:
69
+ """Sends text to Gemini and returns a structured analysis."""
70
+ prompt = self._build_prompt(text)
71
+ try:
72
+ response = await self.client.post(self.API_URL, headers=self.headers, params=self.params, json=prompt, timeout=60.0)
73
+ response.raise_for_status()
74
+
75
+ # Extract the JSON content from the response
76
+ full_response = response.json()
77
+ json_text = full_response["candidates"][0]["content"]["parts"][0]["text"]
78
+
79
+ # The output is a JSON string, so we parse it.
80
+ analysis = json.loads(json_text)
81
+ analysis["error"] = None
82
+ return analysis
83
+
84
+ except Exception as e:
85
+ logger.error(f"❌ Gemini API Error: {e}")
86
+ return {
87
+ "sentiment": "ERROR", "sentiment_score": 0, "reason": str(e),
88
+ "entities": [], "topic": "Unknown", "impact": "Unknown",
89
+ "summary": "Failed to analyze text.", "error": str(e)
90
+ }