Team Ai
Apppublic

dgarzon/SQL-Explainer

sourceHugging Faceupdated 7mo agoView on Hugging Face
0likes
sql_explainer.py93 linesDownload Raw Back to root
1#!/usr/bin/env python32"""SQL Explainer Agent CLI."""3 4from __future__ import annotations5 6import argparse7import sys8 9from sql_explainer_core import (10    DEFAULT_MAX_NEW_TOKENS,11    DEFAULT_MODEL,12    call_local_model,13)14 15 16def read_sql_from_terminal() -> str:17    """Read SQL either interactively from the terminal or from stdin."""18    if sys.stdin.isatty():19        print("Paste the SQL query below.")20        print("Finish with either:")21        print("- a blank line after the SQL")22        print("- or a line containing only :end")23        print("- or Ctrl-D on macOS/Linux, Ctrl-Z then Enter on Windows\n")24 25        lines: list[str] = []26        while True:27            try:28                line = input()29            except EOFError:30                break31 32            if line.strip() == ":end":33                break34 35            if not line.strip() and lines:36                break37 38            lines.append(line)39 40        sql = "\n".join(lines).strip()41    else:42        sql = sys.stdin.read().strip()43 44    if not sql:45        raise ValueError("No SQL query was provided.")46    return sql47 48 49def parse_args() -> argparse.Namespace:50    """Define CLI options for local model inference."""51    parser = argparse.ArgumentParser(52        description="Explain a SQL query using a local open model."53    )54    parser.add_argument(55        "--model",56        default=DEFAULT_MODEL,57        help=f"Local model to use. Default: {DEFAULT_MODEL}",58    )59    parser.add_argument(60        "--max-new-tokens",61        type=int,62        default=DEFAULT_MAX_NEW_TOKENS,63        help=f"Maximum generated tokens. Default: {DEFAULT_MAX_NEW_TOKENS}",64    )65    return parser.parse_args()66 67 68def main() -> int:69    """Run the full CLI flow."""70    args = parse_args()71 72    try:73        sql = read_sql_from_terminal()74        explanation = call_local_model(75            sql,76            model_id=args.model,77            max_new_tokens=args.max_new_tokens,78        )79    except KeyboardInterrupt:80        print("\nCancelled.", file=sys.stderr)81        return 13082    except Exception as exc:  # noqa: BLE001 - keep CLI errors simple for V1.83        print(f"Error: {exc}", file=sys.stderr)84        return 185 86    print("\nSQL Explainer Agent\n")87    print(explanation)88    return 089 90 91if __name__ == "__main__":92    raise SystemExit(main())93