Skip to content

Commit 34cd2a1

Browse files
committed
first commit
0 parents  commit 34cd2a1

21 files changed

Lines changed: 426 additions & 0 deletions

.gitignore

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Python
2+
__pycache__/
3+
*.py[cod]
4+
*$py.class
5+
6+
# Environments
7+
.env
8+
.venv
9+
env/
10+
venv/
11+
ENV/
12+
env.bak/
13+
venv.bak/
14+
15+
# Distribution / packaging
16+
dist/
17+
build/
18+
*.egg-info/
19+
20+
# IDE
21+
.vscode/
22+
.idea/

backend/.env.example

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
ENV=development
2+
3+
# Gemini
4+
GEMINI_API_KEY=your_gemini_api_key_here
5+
6+
# PostgreSQL (READ-ONLY USER)
7+
DB_HOST=localhost
8+
DB_PORT=5432
9+
DB_NAME=your_db_name
10+
DB_USER=readonly_user
11+
DB_PASSWORD=readonly_password

backend/Dockerfile

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
FROM python:3.11-slim
2+
3+
WORKDIR /app
4+
5+
# Install system dependencies (needed for psycopg2)
6+
RUN apt-get update && apt-get install -y \
7+
gcc \
8+
libpq-dev \
9+
&& rm -rf /var/lib/apt/lists/*
10+
11+
# Copy requirements
12+
COPY requirements.txt .
13+
14+
# Install python dependencies
15+
RUN pip install --no-cache-dir -r requirements.txt
16+
17+
# Copy application code
18+
COPY . .
19+
20+
# Expose port
21+
EXPOSE 8000
22+
23+
# Command to run the application
24+
CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "8000", "--reload"]

backend/README.md

Whitespace-only changes.

backend/app/api/__init__.py

Whitespace-only changes.

backend/app/api/query.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
from fastapi import APIRouter, HTTPException
2+
3+
from app.schemas.request import SQLQueryRequest
4+
from app.schemas.response import SQLQueryResponse
5+
from app.services.sql_generator import generate_sql
6+
from app.services.sql_executor import execute_sql
7+
8+
router = APIRouter(tags=["Query"])
9+
10+
11+
@router.post("/query", response_model=SQLQueryResponse)
12+
def query_sql(payload: SQLQueryRequest):
13+
try:
14+
# Step 1: Generate SQL + explanation using Gemini
15+
result = generate_sql(payload.question)
16+
17+
sql = result["sql"]
18+
explanation = result["explanation"]
19+
20+
# Step 2: Execute SQL safely
21+
columns, rows = execute_sql(sql)
22+
23+
return SQLQueryResponse(
24+
sql=sql,
25+
explanation=explanation,
26+
columns=columns,
27+
rows=rows,
28+
)
29+
30+
except ValueError as exc:
31+
raise HTTPException(status_code=400, detail=str(exc))
32+
except Exception:
33+
raise HTTPException(
34+
status_code=500,
35+
detail="Failed to process SQL query"
36+
)

backend/app/core/__init__.py

Whitespace-only changes.

backend/app/core/config.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
from pydantic_settings import BaseSettings
2+
3+
4+
class Settings(BaseSettings):
5+
# App
6+
APP_NAME: str = "AskSQLAI"
7+
ENV: str = "development"
8+
9+
# Gemini
10+
GEMINI_API_KEY: str
11+
GEMINI_MODEL: str = "gemini-1.5-flash"
12+
13+
# Database (read-only)
14+
DB_HOST: str
15+
DB_PORT: int = 5432
16+
DB_NAME: str
17+
DB_USER: str
18+
DB_PASSWORD: str
19+
20+
class Config:
21+
env_file = ".env"
22+
env_file_encoding = "utf-8"
23+
extra = "ignore"
24+
25+
26+
settings = Settings()

backend/app/schemas/__init__.py

Whitespace-only changes.

backend/app/schemas/request.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
from pydantic import BaseModel, Field
2+
3+
4+
class SQLQueryRequest(BaseModel):
5+
question: str = Field(
6+
...,
7+
min_length=5,
8+
description="Natural language question to be converted into SQL"
9+
)

0 commit comments

Comments
 (0)