Spaces:
Sleeping
Sleeping
Upload 2 files
Browse files- app.py +44 -0
- requirements.txt +2 -0
app.py
ADDED
@@ -0,0 +1,44 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import os
|
3 |
+
import openai
|
4 |
+
|
5 |
+
# Load OpenAI API Key
|
6 |
+
openai.api_key = os.environ.get("OPENAI_API_KEY")
|
7 |
+
|
8 |
+
BASE_SYSTEM_PROMPT = "You are an SQL expert. Based on the selected dialect, return a correct SQL query for the user's request."
|
9 |
+
|
10 |
+
def query_openai(message, chat_history, dialect):
|
11 |
+
system_prompt = f"{BASE_SYSTEM_PROMPT} Use the {dialect} dialect."
|
12 |
+
|
13 |
+
messages = [{"role": "system", "content": system_prompt}]
|
14 |
+
for user, bot in chat_history:
|
15 |
+
messages.append({"role": "user", "content": user})
|
16 |
+
messages.append({"role": "assistant", "content": bot})
|
17 |
+
messages.append({"role": "user", "content": message})
|
18 |
+
|
19 |
+
response = openai.ChatCompletion.create(
|
20 |
+
model="gpt-3.5-turbo",
|
21 |
+
messages=messages,
|
22 |
+
temperature=0.5
|
23 |
+
)
|
24 |
+
|
25 |
+
return response["choices"][0]["message"]["content"]
|
26 |
+
|
27 |
+
def respond(message, chat_history, dialect):
|
28 |
+
bot_reply = query_openai(message, chat_history, dialect)
|
29 |
+
chat_history.append((message, bot_reply))
|
30 |
+
return "", chat_history
|
31 |
+
|
32 |
+
with gr.Blocks() as demo:
|
33 |
+
gr.Markdown("## 🧠 SQL Query Generator Bot (OpenAI GPT-Powered)")
|
34 |
+
|
35 |
+
dialect = gr.Dropdown(["MySQL", "PostgreSQL", "SQLite"], value="MySQL", label="Select SQL Dialect")
|
36 |
+
chatbot = gr.Chatbot()
|
37 |
+
msg = gr.Textbox(label="Describe your SQL query in plain English")
|
38 |
+
clear = gr.Button("Clear Chat")
|
39 |
+
state = gr.State([])
|
40 |
+
|
41 |
+
msg.submit(respond, [msg, state, dialect], [msg, chatbot])
|
42 |
+
clear.click(lambda: ([], []), None, [chatbot, state])
|
43 |
+
|
44 |
+
demo.launch()
|
requirements.txt
ADDED
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
1 |
+
gradio
|
2 |
+
openai
|