Team Ai
Apppublic

dvwn/nl2sql-api

sourceHugging Faceupdated 4mo agoView on Hugging Face
0likes
api.py49 linesDownload Raw Back to utils
1# Path: frontend/utils/api.py2# API utility functions for making requests to the backend server.3import pandas as pd4import streamlit as st5import requests6from typing import List, Dict, Any, Tuple7 8FASTAPI_BASE_URL = "https://dvwn-nl2sql-api.hf.space"9 10@st.cache_data(ttl=600)11def get_available_models() -> List[str]:12    """ Fetches available models via FastAPI """13    try:14       resp = requests.get(f"{FASTAPI_BASE_URL}/models", timeout=10)15       resp.raise_for_status()16       return resp.json()17    except Exception as e:18        st.warning(f"Backend unreachable. Using fallback models. Error: {e}")19        return ["Qwen/Qwen2.5-Coder-7B-Instruct:featherless-ai"]20 21def process_userPrompt(question: str, model_id: str) -> Dict[str, Any]:22    """ Send user's question to FastAPI and parses the response """23    try:24        payload = {25            "question": question,26            "model_id": model_id27        }28 29        resp = requests.post(f"{FASTAPI_BASE_URL}/chat", json=payload, timeout=60)30        resp.raise_for_status()31        data = resp.json()32 33        df_results = pd.DataFrame(data["data"]) if data.get("data") else pd.DataFrame()34 35        return {36            "status": data["status"],37            "sql": data["sql"],38            "answer": data["answer"],39            "data": df_results,40            "error":  data["error"]41        }42    except requests.exceptions.RequestException as e:43        return {44            "status": "error",45            "sql": "N/A",46            "answer": "Failed to connect to the backend server.",47            "data": pd.DataFrame(),48            "error": str(e)49        }