File size: 1,623 Bytes
a5b3518
 
 
 
 
 
 
7caef6c
a5b3518
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# Path: frontend/utils/api.py
# API utility functions for making requests to the backend server.
import pandas as pd
import streamlit as st
import requests
from typing import List, Dict, Any, Tuple

FASTAPI_BASE_URL = "https://dvwn-nl2sql-api.hf.space"

@st.cache_data(ttl=600)
def get_available_models() -> List[str]:
    """ Fetches available models via FastAPI """
    try:
       resp = requests.get(f"{FASTAPI_BASE_URL}/models", timeout=10)
       resp.raise_for_status()
       return resp.json()
    except Exception as e:
        st.warning(f"Backend unreachable. Using fallback models. Error: {e}")
        return ["Qwen/Qwen2.5-Coder-7B-Instruct:featherless-ai"]

def process_userPrompt(question: str, model_id: str) -> Dict[str, Any]:
    """ Send user's question to FastAPI and parses the response """
    try:
        payload = {
            "question": question,
            "model_id": model_id
        }

        resp = requests.post(f"{FASTAPI_BASE_URL}/chat", json=payload, timeout=60)
        resp.raise_for_status()
        data = resp.json()

        df_results = pd.DataFrame(data["data"]) if data.get("data") else pd.DataFrame()

        return {
            "status": data["status"],
            "sql": data["sql"],
            "answer": data["answer"],
            "data": df_results,
            "error":  data["error"]
        }
    except requests.exceptions.RequestException as e:
        return {
            "status": "error",
            "sql": "N/A",
            "answer": "Failed to connect to the backend server.",
            "data": pd.DataFrame(),
            "error": str(e)
        }