Spaces:
Sleeping
Sleeping
Initial Commit
Browse files- app.py +18 -0
- gpt4o_infer.py +16 -0
- prompt_generator.py +10 -0
- requirements.txt +2 -0
app.py
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from prompt_generator import generate_prompt
|
| 3 |
+
from gpt4o_infer import call_gpt4o
|
| 4 |
+
|
| 5 |
+
def nlp_to_sql(nl_query, schema_text):
|
| 6 |
+
prompt = generate_prompt(nl_query, schema_text)
|
| 7 |
+
sql = call_gpt4o(prompt)
|
| 8 |
+
return sql
|
| 9 |
+
|
| 10 |
+
iface = gr.Interface(
|
| 11 |
+
fn=nlp_to_sql,
|
| 12 |
+
inputs=[gr.Textbox(label="Natural Language Query"), gr.Textbox(label="Schema in Natural Format")],
|
| 13 |
+
outputs="text",
|
| 14 |
+
title="NLP to SQL with GPT-4o",
|
| 15 |
+
description="Enter your natural language query and schema to get SQL"
|
| 16 |
+
)
|
| 17 |
+
|
| 18 |
+
iface.launch()
|
gpt4o_infer.py
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import openai
|
| 2 |
+
import os
|
| 3 |
+
|
| 4 |
+
openai.api_key = os.getenv("OPENAI_API_KEY")
|
| 5 |
+
|
| 6 |
+
def call_gpt4o(prompt):
|
| 7 |
+
response = openai.ChatCompletion.create(
|
| 8 |
+
model="gpt-4o",
|
| 9 |
+
messages=[
|
| 10 |
+
{"role": "system", "content": "You are a helpful assistant that converts natural language into SQL queries using the provided schema."},
|
| 11 |
+
{"role": "user", "content": prompt}
|
| 12 |
+
],
|
| 13 |
+
temperature=0.2,
|
| 14 |
+
max_tokens=512
|
| 15 |
+
)
|
| 16 |
+
return response['choices'][0]['message']['content']
|
prompt_generator.py
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
def generate_prompt(query, schema):
|
| 2 |
+
return f"""Given the database schema below:
|
| 3 |
+
|
| 4 |
+
{schema}
|
| 5 |
+
|
| 6 |
+
Translate the following question into SQL:
|
| 7 |
+
|
| 8 |
+
Question: {query}
|
| 9 |
+
|
| 10 |
+
Only provide SQL output without explanation."""
|
requirements.txt
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
|
|
|
| 1 |
+
gradio
|
| 2 |
+
openai
|