File size: 6,059 Bytes
0c591a7
 
 
 
 
 
 
 
 
 
05b62e8
0c591a7
 
 
 
 
 
05b62e8
 
 
 
 
 
 
 
 
 
 
 
 
0c591a7
e926e7e
 
 
 
 
 
 
 
 
 
0c591a7
 
 
e926e7e
 
0c591a7
 
e926e7e
 
 
0c591a7
 
 
 
 
3e0da39
0c591a7
 
 
e926e7e
0c591a7
 
 
 
e926e7e
 
 
 
 
 
 
 
 
 
 
0c591a7
 
 
 
 
 
 
 
05b62e8
 
 
 
 
0c591a7
 
05b62e8
 
 
0c591a7
 
 
e926e7e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
96a9373
 
e926e7e
 
96a9373
e926e7e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0c591a7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
FastAPI application for Instant SWOT Agent.
Provides REST API backend for React frontend.
"""

from pathlib import Path

from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from fastapi.staticfiles import StaticFiles
import os
from dotenv import load_dotenv

from src.api.routes.analysis import router as analysis_router
from src.api.routes.stocks import router as stocks_router, load_stock_listings
from src.services.workflow_store import WORKFLOWS

# Load environment variables from .env file (for local development)
# In HF Spaces, secrets are injected as environment variables automatically
load_dotenv()  # Safe to call even if .env doesn't exist

# Debug: Log which LLM providers are available (without exposing keys)
_llm_providers = []
if os.getenv("GROQ_API_KEY"):
    _llm_providers.append("Groq")
if os.getenv("GEMINI_API_KEY"):
    _llm_providers.append("Gemini")
if os.getenv("OPENROUTER_API_KEY"):
    _llm_providers.append("OpenRouter")
print(f"[Startup] LLM providers available: {_llm_providers or 'NONE - check HF Spaces secrets!'}")

from contextlib import asynccontextmanager


@asynccontextmanager
async def lifespan(_app: FastAPI):
    """Load stock listings on startup."""
    await load_stock_listings()
    yield


app = FastAPI(
    title="Instant SWOT Agent API",
    description="Multi-agent SWOT analysis with self-correcting quality control",
    version="2.0.0",
    lifespan=lifespan,
)

# CORS configuration for React frontend.
# Note: literal wildcards like "https://*.hf.space" are NOT matched by
# allow_origins; subdomain patterns require allow_origin_regex.
app.add_middleware(
    CORSMiddleware,
    allow_origins=[
        "http://localhost:5173",
        "http://localhost:8080",
        "http://localhost:8081",
        "http://localhost:3000",
        "https://huggingface.co",
    ],
    allow_origin_regex=r"https://.*\.hf\.space",
    allow_methods=["*"],
    allow_headers=["*"],
)


@app.middleware("http")
async def cache_headers(request, call_next):
    """Immutable caching for hashed /assets bundles; no-cache for the shell."""
    response = await call_next(request)
    if request.url.path.startswith("/assets/"):
        response.headers["Cache-Control"] = "public, max-age=31536000, immutable"
    elif request.url.path == "/":
        response.headers["Cache-Control"] = "no-cache"
    return response

# Include routers
app.include_router(analysis_router)
app.include_router(stocks_router)


@app.get("/health")
async def health_check():
    """Health check endpoint."""
    llm_status = {
        "groq": bool(os.getenv("GROQ_API_KEY")),
        "gemini": bool(os.getenv("GEMINI_API_KEY")),
        "openrouter": bool(os.getenv("OPENROUTER_API_KEY")),
    }
    return {
        "status": "ok",
        "active_workflows": len(WORKFLOWS),
        "llm_providers_configured": llm_status,
        "llm_available": any(llm_status.values())
    }


@app.get("/health/deep")
def health_check_deep():
    """
    Deep health check: sends a 1-token prompt to every configured LLM provider.
    Distinguishes 'key present' from 'model actually serves' — a plain key
    check masked the June 2026 Gemini/OpenRouter model retirements.
    Runs in the threadpool (sync def) since provider calls are blocking.
    """
    from src.llm_client import LLMClient

    results = {}
    try:
        client = LLMClient()
    except ValueError as e:
        return {"status": "error", "detail": str(e), "providers": {}}

    for provider in client.providers:
        try:
            # 512 tokens: reasoning models (gpt-oss) spend budget on hidden
            # reasoning before emitting content; 20 tokens returns "" content
            content, error = client._call_provider(
                provider=provider, prompt="Reply with: OK",
                temperature=0, max_tokens=512
            )
            results[provider["name"]] = {
                "model": provider["model"],
                "ok": bool(content),
                "error": error,
            }
        except Exception as e:
            results[provider["name"]] = {
                "model": provider["model"], "ok": False, "error": str(e)
            }

    return {
        "status": "ok" if any(r["ok"] for r in results.values()) else "degraded",
        "providers": results,
    }


@app.get("/api")
async def api_info():
    """API info endpoint."""
    return {
        "name": "Instant SWOT Agent API",
        "version": "2.0.0",
        "endpoints": [
            "POST /analyze - Start SWOT analysis",
            "GET /workflow/{id}/status - Get workflow progress",
            "GET /workflow/{id}/result - Get final result",
            "GET /api/stocks/search - Search US stocks",
            "GET /health - Health check"
        ]
    }


# Serve React frontend static files (for Docker/HF Spaces deployment)
# Static dir is at project root level
STATIC_DIR = Path(__file__).parent.parent.parent / "static"
if STATIC_DIR.exists():
    from fastapi.responses import FileResponse

    # Mount static assets FIRST (before catch-all routes)
    app.mount("/assets", StaticFiles(directory=str(STATIC_DIR / "assets")), name="assets")

    @app.get("/")
    async def serve_index():
        return FileResponse(STATIC_DIR / "index.html")

    # Serve vite.svg and other root static files
    @app.get("/vite.svg")
    async def serve_vite_svg():
        return FileResponse(STATIC_DIR / "vite.svg")

    # Fallback for SPA routing - exclude API and asset paths
    @app.get("/{full_path:path}")
    async def serve_spa(full_path: str):
        # Don't intercept API routes or assets
        if full_path.startswith(("api/", "assets/", "analyze", "workflow", "health")):
            return {"error": "Not found"}
        file_path = STATIC_DIR / full_path
        if file_path.exists() and file_path.is_file():
            return FileResponse(file_path)
        return FileResponse(STATIC_DIR / "index.html")


if __name__ == "__main__":
    import uvicorn
    uvicorn.run(app, host="0.0.0.0", port=8002)