AreebaAliAsghar commited on
Commit
d83f2ad
·
verified ·
1 Parent(s): ffc2e83

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +47 -0
app.py ADDED
@@ -0,0 +1,47 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ try:
29
+ bot_reply = query_openai(message, chat_history, dialect)
30
+ except Exception as e:
31
+ bot_reply = f"❌ Error: {str(e)}"
32
+ chat_history.append((message, bot_reply))
33
+ return "", chat_history
34
+
35
+ with gr.Blocks() as demo:
36
+ gr.Markdown("## 🧠 SQL Query Generator Bot (OpenAI GPT-Powered)")
37
+
38
+ dialect = gr.Dropdown(["MySQL", "PostgreSQL", "SQLite"], value="MySQL", label="Select SQL Dialect")
39
+ chatbot = gr.Chatbot()
40
+ msg = gr.Textbox(label="Describe your SQL query in plain English")
41
+ clear = gr.Button("Clear Chat")
42
+ state = gr.State([])
43
+
44
+ msg.submit(respond, [msg, state, dialect], [msg, chatbot])
45
+ clear.click(lambda: ([], []), None, [chatbot, state])
46
+
47
+ demo.launch()