Spaces:
Running
Running
File size: 3,376 Bytes
28035e9 | 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 | """FastAPI application β AI SQL Analyst API and frontend server."""
import logging
from pathlib import Path
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import FileResponse
from fastapi.staticfiles import StaticFiles
from pydantic import BaseModel
logging.basicConfig(level=logging.INFO, format="%(asctime)s %(name)s %(message)s")
app = FastAPI(title="AI SQL Analyst", version="1.0.0")
# ββ CORS ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_methods=["*"],
allow_headers=["*"],
)
# ββ Request / Response schemas ββββββββββββββββββββββββββββββββββββββββββββββ
class QuestionRequest(BaseModel):
question: str
provider: str = "groq" # "groq" | "openai"
class GenerateSQLResponse(BaseModel):
sql: str
class ChatResponse(BaseModel):
sql: str
data: list
answer: str
insights: str
# ββ Endpoints βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
@app.post("/generate-sql", response_model=GenerateSQLResponse)
def generate_sql_endpoint(req: QuestionRequest):
from ai.pipeline import SQLAnalystPipeline
pipeline = SQLAnalystPipeline(provider=req.provider)
sql = pipeline.generate_sql_only(req.question)
return GenerateSQLResponse(sql=sql)
@app.post("/chat", response_model=ChatResponse)
def chat_endpoint(req: QuestionRequest):
from ai.pipeline import SQLAnalystPipeline
pipeline = SQLAnalystPipeline(provider=req.provider)
result = pipeline.run(req.question)
return ChatResponse(**result)
# ββ Schema info endpoint (for debugging / transparency) βββββββββββββββββββββ
@app.get("/schema")
def schema_endpoint():
from db.schema import get_schema
return get_schema()
@app.get("/relationships")
def relationships_endpoint():
from db.relationships import discover_relationships
rels = discover_relationships()
return [
{
"table_a": r.table_a, "column_a": r.column_a,
"table_b": r.table_b, "column_b": r.column_b,
"confidence": r.confidence, "source": r.source,
}
for r in rels
]
# ββ Frontend static files ββββββββββββββββββββββββββββββββββββββββββββββββββ
FRONTEND_DIR = Path(__file__).parent / "frontend"
app.mount("/static", StaticFiles(directory=str(FRONTEND_DIR)), name="static")
@app.get("/")
def serve_frontend():
return FileResponse(str(FRONTEND_DIR / "index.html"))
# ββ Run βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
if __name__ == "__main__":
import uvicorn
uvicorn.run("app:app", host="0.0.0.0", port=8000, reload=True)
|