File size: 2,343 Bytes
c576eab
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
29c67fa
c576eab
 
29c67fa
 
 
 
 
c576eab
 
 
9948c1e
 
c576eab
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
29c67fa
c576eab
 
29c67fa
9948c1e
c576eab
 
 
9948c1e
 
c576eab
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
import gradio as gr
from openai import OpenAI
import os

# --- 1. Get Secrets from HF Space Secrets ---
# These point to your *other* API Space
API_URL = os.environ.get("API_URL")
API_KEY = os.environ.get("API_KEY")

# These are for this Gradio UI
UI_USER = os.environ.get("UI_USER", "admin")
UI_PASS = os.environ.get("UI_PASS", "admin")

# Check if the API_URL was set
if not API_URL:
    raise ValueError("API_URL not set in Space secrets. Please add the URL of your API Space.")

# --- 2. Configure the OpenAI Client ---
# This client points to your *public* API Space
client = OpenAI(
    api_key=API_KEY,
    base_url=f"{API_URL}/v1" # Construct the full v1 URL
)

# --- 3. Define the Gradio Function ---
def get_sql_response(message: str, history: list[dict[str, str]]):
    system_prompt = "You are an expert AI that converts natural language questions into SQL queries. You only respond with the SQL query."

    messages_for_api = (
        [{"role": "system", "content": system_prompt}] +
        history +
        [{"role": "user", "content": message}]
    )

    try:
        response_stream = client.chat.completions.create(
            # The model name is a placeholder, llama.cpp server doesn't mind
            model="prem-1b-sql", 
            messages=messages_for_api,
            stream=True
        )
        
        partial_message = ""
        for chunk in response_stream:
            if chunk.choices[0].delta.content:
                partial_message += chunk.choices[0].delta.content
                yield partial_message
                
    except Exception as e:
        print(f"Error connecting to API Space: {e}")
        yield f"Error connecting to the API server: {e}. (Is the API Space running?)"

# --- 4. Launch the Gradio App ---
print("Launching Gradio interface...")
gr.ChatInterface(
    get_sql_response,
    title="Prem-1B-SQL Playground (UI)",
    description="Ask a natural language question. I will convert it to a SQL query.",
    examples=["Show me the average salary for all employees in the 'Engineering' department.",
              "List all users who signed up in the last 30 days."],
    type="messages" # Use modern "messages" format
).launch(
    server_name="0.0.0.0",
    server_port=7860,
    auth=(UI_USER, UI_PASS)
    # share=True has been REMOVED as it is not needed on HF Spaces
)