Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,41 +1,57 @@
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from
|
| 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 |
demo = gr.Interface(
|
| 33 |
-
fn=
|
| 34 |
-
inputs=gr.JSON(label="JSON (question, schema
|
| 35 |
outputs="text",
|
| 36 |
-
title="
|
| 37 |
-
description="
|
| 38 |
-
allow_flagging="never"
|
| 39 |
)
|
| 40 |
|
| 41 |
demo.launch()
|
|
|
|
| 1 |
+
from dotenv import load_dotenv
|
| 2 |
+
import os
|
| 3 |
import gradio as gr
|
| 4 |
+
from groq import Groq
|
| 5 |
+
|
| 6 |
+
load_dotenv()
|
| 7 |
+
api = os.getenv("groq_api_key")
|
| 8 |
+
|
| 9 |
+
def create_prompt(user_query, table_metadata):
|
| 10 |
+
system_prompt = """
|
| 11 |
+
You are a SQL query generator specialized in generating SQL queries for a single table at a time.
|
| 12 |
+
Your task is to accurately convert natural language queries into SQL statements based on the user's intent and the provided table metadata.
|
| 13 |
+
|
| 14 |
+
Rules:
|
| 15 |
+
- Single Table Only: Use only the table in the metadata.
|
| 16 |
+
- Metadata-Based Validation: Use only columns in the metadata.
|
| 17 |
+
- User Intent: Support filters, grouping, sorting, etc.
|
| 18 |
+
- SQL Syntax: Use standard SQL (DuckDB compatible).
|
| 19 |
+
- Output only valid SQL. No extra commentary.
|
| 20 |
+
|
| 21 |
+
Input:
|
| 22 |
+
User Query: {user_query}
|
| 23 |
+
Table Metadata: {table_metadata}
|
| 24 |
+
|
| 25 |
+
Output:
|
| 26 |
+
SQL Query (on a single line, nothing else).
|
| 27 |
+
"""
|
| 28 |
+
return system_prompt.strip(), f"User Query: {user_query}\nTable Metadata: {table_metadata}"
|
| 29 |
+
|
| 30 |
+
def generate_output(system_prompt, user_prompt):
|
| 31 |
+
client = Groq(api_key=api)
|
| 32 |
+
chat_completion = client.chat.completions.create(
|
| 33 |
+
messages=[
|
| 34 |
+
{"role": "system", "content": system_prompt},
|
| 35 |
+
{"role": "user", "content": user_prompt}
|
| 36 |
+
],
|
| 37 |
+
model="llama3-70b-8192"
|
| 38 |
)
|
| 39 |
+
response = chat_completion.choices[0].message.content.strip()
|
| 40 |
+
return response if response.lower().startswith("select") else "Can't perform the task at the moment."
|
| 41 |
|
| 42 |
+
# NEW: accepts user_query and dynamic table_metadata string
|
| 43 |
+
def response(payload):
|
| 44 |
+
user_query = payload.get("question", "")
|
| 45 |
+
table_metadata = payload.get("schema", "")
|
| 46 |
+
system_prompt, user_prompt = create_prompt(user_query, table_metadata)
|
| 47 |
+
return generate_output(system_prompt, user_prompt)
|
| 48 |
|
| 49 |
demo = gr.Interface(
|
| 50 |
+
fn=response,
|
| 51 |
+
inputs=gr.JSON(label="Input JSON (question, schema)"),
|
| 52 |
outputs="text",
|
| 53 |
+
title="SQL Generator (Groq + LLaMA3)",
|
| 54 |
+
description="Input: question & table metadata. Output: SQL using dynamic schema."
|
|
|
|
| 55 |
)
|
| 56 |
|
| 57 |
demo.launch()
|