Spaces:
Sleeping
Sleeping
File size: 1,115 Bytes
950dcd2 | 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 | """SQL Whisperer routes."""
from flask import Blueprint, render_template, request, jsonify
from .whisperer import whisper
bp = Blueprint("sql_whisperer", __name__, template_folder="templates")
_DIALECTS = {"PostgreSQL", "MySQL", "SQLite", "SQL Server", "BigQuery"}
@bp.route("/")
def index():
return render_template("sql_whisperer/index.html")
@bp.route("/api/whisper", methods=["POST"])
def api_whisper():
body = request.get_json(silent=True) or {}
question = (body.get("question") or "").strip()
schema = (body.get("schema") or "").strip()
dialect = body.get("dialect", "PostgreSQL")
if not question:
return jsonify({"error": "question is required"}), 400
if dialect not in _DIALECTS:
dialect = "PostgreSQL"
try:
result = whisper(question, schema, dialect)
except Exception as e:
return jsonify({"error": "AI failed to convert query — please try again"}), 502
if not result or not isinstance(result, dict):
return jsonify({"error": "AI failed to convert query — please try again"}), 502
return jsonify(result)
|