Spaces:
Runtime error
Runtime error
| 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 | |
| ) |