-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
586 lines (504 loc) · 20.1 KB
/
app.py
File metadata and controls
586 lines (504 loc) · 20.1 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
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
# app.py — SQLWhisper API v2.0.0
# Enhanced with structured feedback storage, database info, and batch testing
from fastapi import FastAPI, HTTPException
from fastapi.middleware.cors import CORSMiddleware
from pydantic import BaseModel
from typing import List, Optional, Dict, Any
import sqlite3
import logging
from pydantic import BaseModel
import requests
from src.services.text2sql_service import EnhancedText2SQLService
from src.services.summarization_service import ResultSummarizationService
# -------------------------------------------------
# ✅ Logging Configuration
# -------------------------------------------------
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
# -------------------------------------------------
# ✅ App Initialization
# -------------------------------------------------
app = FastAPI(title="SQLWhisper API", version="2.0.0")
# -------------------------------------------------
# ✅ CORS Middleware
# -------------------------------------------------
app.add_middleware(
CORSMiddleware,
allow_origins=["*"], # Adjust this in production
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
# -------------------------------------------------
# ✅ Service Initialization
# -------------------------------------------------
t2s_service = EnhancedText2SQLService()
summarization_service = ResultSummarizationService()
# -------------------------------------------------
# ✅ Database Helper
# -------------------------------------------------
def get_db_connection(database_path: str = "data/my_database.sqlite"):
"""Get SQLite database connection."""
try:
conn = sqlite3.connect(database_path)
conn.row_factory = sqlite3.Row # Return rows as dictionaries
return conn
except Exception as e:
raise HTTPException(status_code=500, detail=f"Database connection failed: {str(e)}")
# -------------------------------------------------
# ✅ Models
# -------------------------------------------------
class Question(BaseModel):
question: str
database_path: str = "data/my_database.sqlite"
class Text2SQLResponse(BaseModel):
question: str
sql: str
valid: bool
execution_result: Optional[List[Dict]] = None
error: Optional[str] = None
raw_output: Optional[str] = None
confidence: Optional[float] = None
confidence_label: Optional[str] = None
class TestQuery(BaseModel):
question: str
expected_sql: Optional[str] = None
class BatchTestResponse(BaseModel):
results: List[Dict[str, Any]]
summary: Dict[str, Any]
class SummaryRequest(BaseModel):
question: str
sql_query: str
results: List[Dict[str, Any]]
class SummaryResponse(BaseModel):
summary: str
success: bool
row_count: int
sample_size: int
error: Optional[str] = None
insights: Optional[List[str]] = None
# -------------------------------------------------
# ✅ USER FEEDBACK ENDPOINT
# -------------------------------------------------
class FeedbackRequest(BaseModel):
question: str
generated_sql: str
verdict: str # "up" or "down"
user_correction: Optional[str] = None
comment: Optional[str] = None
@app.post("/feedback")
def submit_feedback(req: FeedbackRequest):
"""
Store user feedback (thumbs up/down) in sql_feedback table.
Includes question, generated SQL, optional correction, and comment.
"""
try:
conn = get_db_connection("data/my_database.sqlite")
cursor = conn.cursor()
cursor.execute(
"""
INSERT INTO sql_feedback (
question, generated_sql, user_correction, verdict, comment
) VALUES (?, ?, ?, ?, ?)
""",
(req.question, req.generated_sql, req.user_correction, req.verdict, req.comment),
)
conn.commit()
conn.close()
return {"ok": True, "message": "✅ Feedback saved successfully."}
except Exception as e:
logger.error(f"Feedback save error: {e}")
raise HTTPException(status_code=500, detail=f"Failed to save feedback: {str(e)}")
# -------------------------------------------------
# ✅ Root & Health Endpoints
# -------------------------------------------------
@app.get("/")
def root():
return {
"message": "SQLWhisper API is running!",
"version": "2.0.0",
"endpoints": {
"text2sql": "POST /text2sql - Generate SQL from natural language",
"test_query": "POST /test-query - Generate and execute SQL",
"batch_test": "POST /batch-test - Test multiple queries",
"feedback": "POST /feedback - Submit user feedback",
"db_info": "GET /db-info - Get database schema info",
"health": "GET /health - Health check",
},
}
@app.get("/health")
def health_check():
return {"status": "healthy", "service": "text2sql"}
# -------------------------------------------------
# ✅ Database Schema Info Endpoint
# -------------------------------------------------
@app.get("/db-info")
def get_database_info(database_path: str = "data/my_database.sqlite"):
"""Return tables, columns, and schema details."""
try:
conn = get_db_connection(database_path)
cursor = conn.cursor()
# Get all tables
cursor.execute("SELECT name FROM sqlite_master WHERE type='table';")
tables = [table[0] for table in cursor.fetchall()]
# Build schema dictionary
schema_info = {}
for table in tables:
cursor.execute(f"PRAGMA table_info({table})")
columns = cursor.fetchall()
schema_info[table] = [
{
"name": col[1],
"type": col[2],
"nullable": not col[3],
"primary_key": col[5] == 1
}
for col in columns
]
conn.close()
return {
"database_path": database_path,
"tables": tables,
"schema": schema_info,
"total_tables": len(tables),
}
except Exception as e:
logger.error(f"Error fetching DB info: {e}")
raise HTTPException(status_code=500, detail=str(e))
# -------------------------------------------------
# ✅ Text-to-SQL Generation
# -------------------------------------------------
@app.post("/text2sql", response_model=Text2SQLResponse)
def text2sql(payload: Question):
"""Generate SQL query from natural language question."""
question = payload.question.strip()
database_path = payload.database_path
if not question:
raise HTTPException(status_code=400, detail="Empty question provided")
try:
conn = get_db_connection(database_path)
result = t2s_service.generate_sql(question, conn)
conn.close()
return Text2SQLResponse(
question=question,
sql=result["sql"],
valid=result["valid"],
raw_output=result.get("raw_output", ""),
confidence=result.get("confidence"),
confidence_label=result.get("confidence_label")
)
except Exception as e:
logger.error(f"Text2SQL error: {e}")
raise HTTPException(status_code=500, detail=str(e))
# -------------------------------------------------
# ✅ Query Execution Test
# -------------------------------------------------
@app.post("/test-query", response_model=Text2SQLResponse)
def test_query(payload: Question):
"""Generate SQL and execute it to verify results."""
question = payload.question.strip()
database_path = payload.database_path
if not question:
raise HTTPException(status_code=400, detail="Empty question provided")
try:
conn = get_db_connection(database_path)
result = t2s_service.generate_sql(question, conn)
execution_result, error = None, None
if result["valid"]:
try:
cursor = conn.cursor()
cursor.execute(result["sql"])
rows = cursor.fetchall()
execution_result = [dict(row) for row in rows]
except Exception as e:
error = f"Execution failed: {str(e)}"
conn.close()
# ✅ Add confidence fields if they exist in result
confidence = result.get("confidence")
confidence_label = result.get("confidence_label")
return {
"question": question,
"sql": result["sql"],
"valid": result["valid"],
"execution_result": execution_result,
"error": error,
"raw_output": result.get("raw_output", ""),
"confidence": confidence,
"confidence_label": confidence_label,
}
except Exception as e:
logger.error(f"Test query error: {e}")
raise HTTPException(status_code=500, detail=str(e))
# -------------------------------------------------
# ✅ Batch Testing Endpoint
# -------------------------------------------------
@app.post("/batch-test", response_model=BatchTestResponse)
def batch_test(queries: List[TestQuery], database_path: str = "data/my_database.sqlite"):
"""Test multiple questions and evaluate performance."""
if not queries:
raise HTTPException(status_code=400, detail="No queries provided")
try:
conn = get_db_connection(database_path)
results = []
for i, query in enumerate(queries):
try:
result = t2s_service.generate_sql(query.question, conn)
execution_success, row_count = False, 0
if result["valid"]:
try:
cursor = conn.cursor()
cursor.execute(result["sql"])
rows = cursor.fetchall()
row_count = len(rows)
execution_success = True
except:
execution_success = False
results.append({
"index": i + 1,
"question": query.question,
"generated_sql": result["sql"],
"valid_syntax": result["valid"],
"execution_success": execution_success,
"rows_returned": row_count,
"expected_sql": query.expected_sql,
"raw_output": result.get("raw_output", "")
})
except Exception as e:
results.append({
"index": i + 1,
"question": query.question,
"error": str(e),
"valid_syntax": False,
"execution_success": False,
"rows_returned": 0
})
conn.close()
summary = {
"total_queries": len(queries),
"valid_syntax": sum(1 for r in results if r["valid_syntax"]),
"execution_success": sum(1 for r in results if r["execution_success"]),
}
summary["success_rate"] = f"{(summary['valid_syntax'] / len(queries)) * 100:.1f}%"
return BatchTestResponse(results=results, summary=summary)
except Exception as e:
logger.error(f"Batch test error: {e}")
raise HTTPException(status_code=500, detail=str(e))
# -------------------------------------------------
# ✅ Sample Queries Endpoint
# -------------------------------------------------
@app.get("/sample-queries")
def get_sample_queries(database_path: str = "data/my_database.sqlite"):
"""
Dynamically generate natural sample queries based on the database structure.
If the DB has common table names (students, courses, etc.), create smart examples.
Otherwise, fall back to generic examples.
"""
try:
conn = get_db_connection(database_path)
cursor = conn.cursor()
cursor.execute("SELECT name FROM sqlite_master WHERE type='table';")
tables = [row[0] for row in cursor.fetchall()]
conn.close()
if not tables:
# Fallback if DB is empty
samples = [
"Show all tables in the database",
"Describe the database schema",
"Count total records in all tables"
]
return {"sample_queries": samples, "count": len(samples)}
# ✅ Intelligent generation
samples = []
for tname in tables[:5]: # only suggest up to 5 tables
samples.extend([
f"Show the first 5 rows from {tname}",
f"Count total records in {tname}",
f"List all column names in {tname}",
f"Show top 10 records from {tname}",
f"Find duplicates in {tname} if any",
])
# ✅ Add a few general queries
samples += [
"List all tables in the database",
"Show tables with the most rows",
"Describe all relationships between tables",
"Find columns that contain date or time information",
]
# ✅ Remove duplicates and sort
samples = sorted(list(set(samples)))
return {
"database_path": database_path,
"sample_queries": samples,
"count": len(samples)
}
except Exception as e:
logger.error(f"Error generating sample queries: {e}")
# fallback on failure
fallback = [
"Show all tables in the database",
"Count total records",
"List top 5 rows from any table",
"Describe table schema"
]
return {
"database_path": database_path,
"sample_queries": fallback,
"count": len(fallback),
"error": str(e)
}
@app.post("/generate-summary")
def generate_summary(payload: SummaryRequest):
"""Return only the summary text as a plain string."""
try:
summary_result = summarization_service.generate_summary(
question=payload.question,
results=payload.results,
sql_query=payload.sql_query
)
return {"summary": summary_result["summary"]}
except Exception as e:
logger.error(f"Summary generation error: {e}")
raise HTTPException(status_code=500, detail=f"Summary generation failed: {str(e)}")
@app.post("/quick-insights")
def get_quick_insights(payload: SummaryRequest):
"""Get simple insights from results."""
try:
insights = summarization_service.quick_insights(payload.results, payload.question)
return {"insights": insights, "row_count": len(payload.results)}
except Exception as e:
logger.error(f"Insights generation error: {e}")
raise HTTPException(status_code=500, detail=f"Insights generation failed: {str(e)}")
# -------------------------------------------------
# ✅ Chatbot Assistant Endpoint (Schema-Aware + Conversational)
# -------------------------------------------------
class ChatRequest(BaseModel):
message: str
database_path: str = "data/my_database.sqlite"
def format_rows_human(rows, max_rows=5):
"""Convert SQL rows into readable sentences."""
if not rows:
return "No matching records were found."
text_lines = []
for i, row in enumerate(rows[:max_rows], 1):
# Only show up to 4 columns for clarity
parts = [f"{k}: {v}" for k, v in list(row.items())[:4]]
text_lines.append(f"{i}. " + ", ".join(parts))
return "\n".join(text_lines)
@app.post("/chat")
def chat_with_sql_assistant(req: ChatRequest):
"""
Conversational AI Assistant:
- Uses EnhancedText2SQLService to generate SQL
- Executes SQL on the user's DB
- Returns natural-language summary and results
"""
try:
# 1️⃣ Setup and generate SQL
db_path = req.database_path or "data/my_database.sqlite"
conn = get_db_connection(db_path)
# Generate SQL using schema-aware service
sql_result = t2s_service.generate_sql(req.message, conn)
sql_query = sql_result.get("sql", "").strip()
schema_used = sql_result.get("schema_used", "")
if not sql_query:
return {"reply": "⚠️ I couldn’t generate a valid SQL query for that question."}
# 2️⃣ Execute the SQL
try:
cursor = conn.cursor()
cursor.execute(sql_query)
rows = [dict(r) for r in cursor.fetchall()]
except Exception as e:
conn.close()
return {"reply": f"❌ Query execution error:\n{e}"}
conn.close()
# 3️⃣ Prepare base conversational text
total_rows = len(rows)
if total_rows == 0:
base_reply = "Hmm, I ran the query but didn’t find any matching data."
elif total_rows == 1:
base_reply = "Here’s the single record I found."
else:
base_reply = f"I ran your query successfully and found {total_rows} records."
# 4️⃣ Convert results into human-readable preview
readable_preview = format_rows_human(rows)
base_reply = f"{base_reply}\n\nHere’s what I found:\n{readable_preview}"
# 5️⃣ Prepare prompt for local LLM (schema-aware)
prompt = f"""
You are SQLWhisper — an intelligent, friendly assistant that explains SQL query results clearly.
DATABASE SCHEMA (for context):
{schema_used}
USER QUESTION:
{req.message}
GENERATED SQL:
{sql_query}
QUERY RESULTS:
Total rows: {total_rows}
Sample rows (human-readable):
{readable_preview}
TASK:
- Explain the query result naturally in 2–4 sentences.
- Be concise and conversational, like talking to a user.
- If user asks for "top one" or "highest", mention only that record.
- Do NOT repeat SQL or say database terms like "query" or "table".
"""
try:
response = requests.post(
"http://localhost:11434/api/generate",
json={"model": "phi", "prompt": prompt},
timeout=45
)
reply_text = ""
for line in response.iter_lines():
if line:
data = line.decode("utf-8")
if '"response":' in data:
reply_text += data.split('"response":"')[1].split('"')[0]
reply_text = reply_text.strip() or base_reply
except Exception as e:
logger.warning(f"LLM generation failed: {e}")
reply_text = base_reply
# 6️⃣ Return clean structured reply
return {
"reply": reply_text + "\n\nWould you like me to summarize these results for you?",
"sql": sql_query,
"rows": rows[:5],
"total_rows": total_rows,
"can_summarize": True
}
except Exception as e:
logger.error(f"Chat error: {e}")
return {"reply": f"Unexpected error: {str(e)}"}
@app.post("/chat/summary")
def chat_generate_summary(req: dict):
"""Generate a friendly natural summary of the last query results."""
try:
question = req.get("question", "")
sql_query = req.get("sql", "")
results = req.get("rows", [])
summary = summarization_service.generate_summary(
question=question,
results=results,
sql_query=sql_query
)
# ✅ Handle both dict and object return types
if isinstance(summary, dict):
return {
"reply": f"Summary:\n{summary.get('summary', 'No summary available.')}",
"insights": summary.get("insights", []),
"row_count": summary.get("row_count", len(results))
}
else:
# backward-compatible with object form
return {
"reply": f"Summary:\n{getattr(summary, 'summary', 'No summary available.')}",
"insights": getattr(summary, 'insights', []),
"row_count": getattr(summary, 'row_count', len(results))
}
except Exception as e:
return {"reply": f"Failed to summarize: {str(e)}"}
# -------------------------------------------------
# App Runner
# -------------------------------------------------
if __name__ == "__main__":
import uvicorn
uvicorn.run(app, host="0.0.0.0", port=8000, reload=True)