-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi_server.py
More file actions
402 lines (320 loc) Β· 12.7 KB
/
api_server.py
File metadata and controls
402 lines (320 loc) Β· 12.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
#!/usr/bin/env python3
"""
FastAPI Backend for Hybrid Analysis Dashboard
Exposes hybrid analysis (statistical + Grok) to Next.js dashboard via HTTP API.
Endpoints:
- GET /api/recommendations - Get cached recommendations
- POST /api/analyze - Trigger new analysis
- GET /api/status - Check analysis status
Usage:
python api_server.py
# Or specify port
python api_server.py --port 8000
"""
import asyncio
import os
from datetime import datetime
from typing import List, Dict, Any, Optional
from fastapi import FastAPI, BackgroundTasks, HTTPException
from fastapi.middleware.cors import CORSMiddleware
from pydantic import BaseModel
import uvicorn
from hybrid_analysis import analyze_all_markets
# ============================================================================
# FASTAPI APP SETUP
# ============================================================================
app = FastAPI(
title="Kalshi Hybrid Analysis API",
description="Statistical + Grok AI betting recommendations",
version="1.0.0"
)
# Enable CORS for dashboard
# Note: For ngrok hosting, we allow all origins. In production, restrict this!
import re
def is_allowed_origin(origin: str) -> bool:
"""Check if origin is allowed (localhost, vercel, or ngrok)"""
if not origin:
return False
allowed_patterns = [
r"^http://localhost:\d+$", # localhost any port
r"^https://.*\.vercel\.app$", # Vercel
r"^https://.*\.ngrok\.io$", # ngrok
r"^https://.*\.ngrok-free\.app$", # ngrok free tier
]
return any(re.match(pattern, origin) for pattern in allowed_patterns)
# Custom CORS middleware that allows ngrok URLs
from fastapi.middleware.cors import CORSMiddleware
from starlette.middleware.cors import ALL_METHODS
from starlette.responses import Response
@app.middleware("http")
async def custom_cors_middleware(request, call_next):
"""Custom CORS middleware to handle ngrok URLs"""
origin = request.headers.get("origin")
response = await call_next(request)
# Allow all origins for demo (ngrok, localhost, etc.)
# In production, use is_allowed_origin(origin) check
if origin:
response.headers["Access-Control-Allow-Origin"] = origin
response.headers["Access-Control-Allow-Credentials"] = "true"
response.headers["Access-Control-Allow-Methods"] = "*"
response.headers["Access-Control-Allow-Headers"] = "*"
return response
# ============================================================================
# STATE MANAGEMENT
# ============================================================================
class AnalysisState:
"""Shared state for analysis results"""
def __init__(self):
self.recommendations: List[Dict[str, Any]] = []
self.last_updated: Optional[datetime] = None
self.is_analyzing: bool = False
self.analysis_error: Optional[str] = None
self.total_analyzed: int = 0
def to_dict(self):
return {
'recommendations': self.recommendations,
'lastUpdated': self.last_updated.isoformat() if self.last_updated else None,
'isAnalyzing': self.is_analyzing,
'error': self.analysis_error,
'totalAnalyzed': self.total_analyzed,
}
# Global state
state = AnalysisState()
# ============================================================================
# REQUEST/RESPONSE MODELS
# ============================================================================
class AnalyzeRequest(BaseModel):
edge_threshold: float = 0.10
max_recommendations: int = 20
quant_weight: float = 0.75
sentiment_weight: float = 0.25
class StatusResponse(BaseModel):
status: str
isAnalyzing: bool
lastUpdated: Optional[str]
count: int
error: Optional[str]
# ============================================================================
# BACKGROUND ANALYSIS TASK
# ============================================================================
async def run_analysis_task(
edge_threshold: float = 0.10,
max_recommendations: int = 20,
quant_weight: float = 0.75,
sentiment_weight: float = 0.25
):
"""Background task to run hybrid analysis"""
global state
try:
state.is_analyzing = True
state.analysis_error = None
print(f"\n{'='*100}")
print(f"π STARTING NEW ANALYSIS")
print(f"{'='*100}")
print(f"Time: {datetime.now().isoformat()}")
print(f"Parameters:")
print(f" β’ Edge threshold: {edge_threshold:.1%}")
print(f" β’ Max recommendations: {max_recommendations}")
print(f" β’ Weights: {quant_weight:.0%} quant, {sentiment_weight:.0%} sentiment")
print(f"{'='*100}\n")
# Run hybrid analysis
recommendations = await analyze_all_markets(
edge_threshold=edge_threshold,
max_recommendations=max_recommendations,
quant_weight=quant_weight,
sentiment_weight=sentiment_weight
)
# Update state
state.recommendations = recommendations
state.last_updated = datetime.now()
state.total_analyzed += len(recommendations)
print(f"\n{'='*100}")
print(f"β
ANALYSIS COMPLETE")
print(f"{'='*100}")
print(f"Total recommendations: {len(recommendations)}")
print(f"Total analyzed (lifetime): {state.total_analyzed}")
# Print summary of recommendations
if recommendations:
print(f"\nπ RECOMMENDATIONS SUMMARY:")
for i, rec in enumerate(recommendations[:5], 1): # Show first 5
print(f"\n{i}. {rec['ticker']}")
print(f" Action: {rec['action']}")
print(f" Edge: {rec['quant_edge']:.1%}")
print(f" Confidence: {rec['combined_confidence']:.1%}")
print(f" Grok: {rec['grok_sentiment'].get('label')} ({rec['grok_sentiment'].get('score')}%)")
if len(recommendations) > 5:
print(f"\n ... and {len(recommendations) - 5} more")
print(f"{'='*100}\n")
except Exception as e:
print(f"β Analysis failed: {e}")
import traceback
traceback.print_exc()
state.analysis_error = str(e)
finally:
state.is_analyzing = False
# ============================================================================
# API ENDPOINTS
# ============================================================================
@app.get("/")
async def root():
"""Health check"""
return {
"status": "online",
"service": "Kalshi Hybrid Analysis API",
"version": "1.0.0",
"docs": "/docs"
}
@app.get("/api/status")
async def get_status() -> StatusResponse:
"""Get current analysis status"""
return StatusResponse(
status="analyzing" if state.is_analyzing else "ready",
isAnalyzing=state.is_analyzing,
lastUpdated=state.last_updated.isoformat() if state.last_updated else None,
count=len(state.recommendations),
error=state.analysis_error
)
@app.get("/api/recommendations")
async def get_recommendations():
"""
Get current recommendations.
Returns cached results from last analysis.
Use POST /api/analyze to trigger new analysis.
"""
print(f"\nπ₯ GET /api/recommendations - Request received at {datetime.now().strftime('%H:%M:%S')}")
if state.is_analyzing:
print(f" β³ Analysis in progress, returning status")
return {
"status": "analyzing",
"message": "Analysis in progress, please wait...",
"opportunities": [],
"lastUpdated": None
}
if state.analysis_error:
print(f" β Last analysis failed: {state.analysis_error}")
raise HTTPException(
status_code=500,
detail=f"Last analysis failed: {state.analysis_error}"
)
if not state.recommendations:
print(f" β οΈ No recommendations available yet")
return {
"status": "empty",
"message": "No recommendations yet. Trigger analysis with POST /api/analyze",
"opportunities": [],
"lastUpdated": None
}
print(f" β
Returning {len(state.recommendations)} recommendations")
print(f" π
Last updated: {state.last_updated.strftime('%H:%M:%S') if state.last_updated else 'Never'}")
return {
"status": "success",
"opportunities": state.recommendations,
"lastUpdated": state.last_updated.isoformat() if state.last_updated else None,
"count": len(state.recommendations)
}
@app.post("/api/analyze")
async def trigger_analysis(
request: AnalyzeRequest,
background_tasks: BackgroundTasks
):
"""
Trigger new hybrid analysis.
Runs in background. Check status with GET /api/status.
"""
if state.is_analyzing:
return {
"status": "already_running",
"message": "Analysis already in progress"
}
# Start background task
background_tasks.add_task(
run_analysis_task,
edge_threshold=request.edge_threshold,
max_recommendations=request.max_recommendations,
quant_weight=request.quant_weight,
sentiment_weight=request.sentiment_weight
)
return {
"status": "started",
"message": "Analysis started in background",
"params": {
"edge_threshold": request.edge_threshold,
"max_recommendations": request.max_recommendations,
"quant_weight": request.quant_weight,
"sentiment_weight": request.sentiment_weight
}
}
@app.get("/api/recommendations/{ticker}")
async def get_recommendation_by_ticker(ticker: str):
"""Get specific recommendation by ticker"""
for rec in state.recommendations:
if rec['ticker'] == ticker:
return rec
raise HTTPException(status_code=404, detail=f"Recommendation for {ticker} not found")
# ============================================================================
# AUTO-REFRESH
# ============================================================================
async def auto_refresh_loop():
"""Auto-refresh recommendations every 5 minutes"""
while True:
try:
# Wait 5 minutes
await asyncio.sleep(300)
# Only refresh if not already analyzing
if not state.is_analyzing:
print("\nπ Auto-refresh triggered")
await run_analysis_task()
except Exception as e:
print(f"β Auto-refresh failed: {e}")
await asyncio.sleep(60) # Retry in 1 minute
@app.on_event("startup")
async def startup_event():
"""Run initial analysis and start auto-refresh"""
print("\n" + "="*80)
print("π KALSHI HYBRID ANALYSIS API STARTING")
print("="*80)
print(f"Time: {datetime.now().isoformat()}")
print(f"Docs: http://localhost:8000/docs")
print(f"API: http://localhost:8000/api/recommendations")
print("="*80)
print()
# Check for API keys
if not os.getenv("X_API_KEY"):
print("β οΈ WARNING: X_API_KEY not set. Grok sentiment analysis will fail!")
print(" Set it in .env or environment: export X_API_KEY=your-key")
print()
# Run initial analysis
print("π Running initial analysis...")
asyncio.create_task(run_analysis_task())
# Start auto-refresh loop
print("β° Starting auto-refresh (every 5 minutes)")
asyncio.create_task(auto_refresh_loop())
@app.on_event("shutdown")
async def shutdown_event():
"""Cleanup on shutdown"""
print("\n" + "="*80)
print("π KALSHI HYBRID ANALYSIS API SHUTTING DOWN")
print("="*80)
print(f"Total recommendations analyzed: {state.total_analyzed}")
print(f"Last updated: {state.last_updated.isoformat() if state.last_updated else 'Never'}")
print("="*80)
# ============================================================================
# MAIN
# ============================================================================
def main():
"""Run the server"""
import argparse
parser = argparse.ArgumentParser(description='Hybrid Analysis API Server')
parser.add_argument('--port', type=int, default=8000, help='Port to run on (default: 8000)')
parser.add_argument('--host', type=str, default='0.0.0.0', help='Host to bind to (default: 0.0.0.0)')
parser.add_argument('--reload', action='store_true', help='Enable auto-reload for development')
args = parser.parse_args()
uvicorn.run(
"api_server:app",
host=args.host,
port=args.port,
reload=args.reload,
log_level="info"
)
if __name__ == "__main__":
main()