Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,76 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import openai
|
| 3 |
+
import os
|
| 4 |
+
|
| 5 |
+
# OpenRouter API Key
|
| 6 |
+
OPENROUTER_API_KEY = "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"
|
| 7 |
+
OPENROUTER_MODEL = "sophosympatheia/rogue-rose-103b-v0.2:free"
|
| 8 |
+
|
| 9 |
+
# Initialize OpenAI client with OpenRouter base URL
|
| 10 |
+
print(f"Using API Key: {OPENROUTER_API_KEY}")
|
| 11 |
+
openai_client = openai.OpenAI(
|
| 12 |
+
api_key=OPENROUTER_API_KEY,
|
| 13 |
+
base_url="https://openrouter.ai/api/v1" # OpenRouter API endpoint
|
| 14 |
+
)
|
| 15 |
+
|
| 16 |
+
# Few-shot examples for text-to-SQL conversion
|
| 17 |
+
few_shot_examples = [
|
| 18 |
+
{
|
| 19 |
+
"input": "Show all customers from the USA.",
|
| 20 |
+
"output": "SELECT * FROM customers WHERE country = 'USA';"
|
| 21 |
+
},
|
| 22 |
+
{
|
| 23 |
+
"input": "Find the total sales for each product category.",
|
| 24 |
+
"output": "SELECT product_category, SUM(sales) AS total_sales FROM sales GROUP BY product_category;"
|
| 25 |
+
},
|
| 26 |
+
{
|
| 27 |
+
"input": "List all orders placed in 2023.",
|
| 28 |
+
"output": "SELECT * FROM orders WHERE YEAR(order_date) = 2023;"
|
| 29 |
+
}
|
| 30 |
+
]
|
| 31 |
+
|
| 32 |
+
def text_to_sql(query):
|
| 33 |
+
# Construct the prompt with few-shot examples
|
| 34 |
+
prompt = "Convert the following natural language queries to SQL:\n\n"
|
| 35 |
+
for example in few_shot_examples:
|
| 36 |
+
prompt += f"Input: {example['input']}\nOutput: {example['output']}\n\n"
|
| 37 |
+
prompt += f"Input: {query}\nOutput:"
|
| 38 |
+
|
| 39 |
+
print("Sending query to OpenRouter API...")
|
| 40 |
+
try:
|
| 41 |
+
response = openai_client.chat.completions.create(
|
| 42 |
+
model=OPENROUTER_MODEL,
|
| 43 |
+
messages=[
|
| 44 |
+
{
|
| 45 |
+
"role": "system",
|
| 46 |
+
"content": "You are a helpful assistant. Your task is to convert natural language queries into SQL queries. "
|
| 47 |
+
"Use the provided examples as a guide. If the query cannot be converted into SQL, say 'I cannot convert this query into SQL.'"
|
| 48 |
+
},
|
| 49 |
+
{
|
| 50 |
+
"role": "user",
|
| 51 |
+
"content": prompt
|
| 52 |
+
}
|
| 53 |
+
]
|
| 54 |
+
)
|
| 55 |
+
print("Received response from OpenRouter API.")
|
| 56 |
+
return response.choices[0].message.content
|
| 57 |
+
except Exception as e:
|
| 58 |
+
print(f"Error calling OpenRouter API: {e}")
|
| 59 |
+
return f"Error: {e}"
|
| 60 |
+
|
| 61 |
+
# Gradio UI
|
| 62 |
+
def gradio_ui():
|
| 63 |
+
with gr.Blocks() as demo:
|
| 64 |
+
gr.Markdown("## Text-to-SQL Converter. Enter a natural language query and get the corresponding SQL query!")
|
| 65 |
+
query_input = gr.Textbox(label="Enter your query")
|
| 66 |
+
submit_btn = gr.Button("Convert to SQL")
|
| 67 |
+
output = gr.Textbox(label="SQL Query")
|
| 68 |
+
|
| 69 |
+
submit_btn.click(text_to_sql, inputs=[query_input], outputs=[output])
|
| 70 |
+
|
| 71 |
+
return demo
|
| 72 |
+
|
| 73 |
+
demo = gradio_ui()
|
| 74 |
+
|
| 75 |
+
print("Launching Gradio UI...")
|
| 76 |
+
demo.launch()
|