amalsp commited on
Commit
e5ba825
·
verified ·
1 Parent(s): 65bc52e

Create main.py

Browse files
Files changed (1) hide show
  1. main.py +87 -0
main.py ADDED
@@ -0,0 +1,87 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """FastAPI Backend for OpenNL2SQL - Simplified for HuggingFace Spaces
2
+ Author: Amal SP
3
+ Created: December 2025
4
+ """
5
+
6
+ from fastapi import FastAPI, HTTPException
7
+ from fastapi.middleware.cors import CORSMiddleware
8
+ from pydantic import BaseModel
9
+ from typing import Optional, List, Dict, Any
10
+ import os
11
+ import logging
12
+
13
+ # Configure logging
14
+ logging.basicConfig(
15
+ level=logging.INFO,
16
+ format='%(asctime)s - %(name)s - %(levelname)s - %(message)s'
17
+ )
18
+ logger = logging.getLogger(__name__)
19
+
20
+ # Initialize FastAPI app
21
+ app = FastAPI(
22
+ title="OpenNL2SQL API",
23
+ description="AI-powered Natural Language to SQL Analytics System",
24
+ version="1.0.0"
25
+ )
26
+
27
+ # CORS configuration
28
+ app.add_middleware(
29
+ CORSMiddleware,
30
+ allow_origins=["*"],
31
+ allow_credentials=True,
32
+ allow_methods=["*"],
33
+ allow_headers=["*"],
34
+ )
35
+
36
+ # Request/Response Models
37
+ class QueryRequest(BaseModel):
38
+ question: str
39
+ session_id: Optional[str] = None
40
+
41
+ class QueryResponse(BaseModel):
42
+ success: bool
43
+ sql: Optional[str] = None
44
+ results: Optional[List[Dict[str, Any]]] = None
45
+ sql_explanation: Optional[str] = None
46
+ results_explanation: Optional[str] = None
47
+ error: Optional[str] = None
48
+ session_id: str
49
+
50
+ @app.get("/")
51
+ async def root():
52
+ """Health check endpoint"""
53
+ return {
54
+ "status": "healthy",
55
+ "service": "OpenNL2SQL API",
56
+ "version": "1.0.0",
57
+ "message": "FastAPI backend is running on Hugging Face Spaces!"
58
+ }
59
+
60
+ @app.get("/health")
61
+ async def health_check():
62
+ """Detailed health check"""
63
+ return {
64
+ "status": "healthy",
65
+ "groq_api_configured": bool(os.getenv("GROQ_API_KEY")),
66
+ "service": "OpenNL2SQL API"
67
+ }
68
+
69
+ @app.post("/query", response_model=QueryResponse)
70
+ async def process_query(request: QueryRequest):
71
+ """Process natural language query"""
72
+ # For now, return a placeholder response
73
+ # This will be integrated with GROQ API and full backend logic
74
+ session_id = request.session_id or "demo-session"
75
+
76
+ return QueryResponse(
77
+ success=True,
78
+ sql="SELECT * FROM demo_table LIMIT 10;",
79
+ results=[{"status": "Backend is running! Full implementation coming soon."}],
80
+ sql_explanation="This is a demo response. Full NL2SQL functionality will be integrated.",
81
+ results_explanation="The backend is successfully deployed on Hugging Face Spaces.",
82
+ session_id=session_id
83
+ )
84
+
85
+ if __name__ == "__main__":
86
+ import uvicorn
87
+ uvicorn.run(app, host="0.0.0.0", port=7860)