Sebbe33 commited on
Commit
68a2af8
·
verified ·
1 Parent(s): e8b450c

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +93 -0
app.py ADDED
@@ -0,0 +1,93 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ from openai import OpenAI
3
+ import sqlite3
4
+ from init_db import initialize_database
5
+
6
+ # Initialize database
7
+ initialize_database()
8
+
9
+ # App configuration
10
+ st.set_page_config(page_title="Zero SQL", layout="wide")
11
+ st.title("Zero SQL - Natural Language to SQL Query")
12
+
13
+ # Sidebar for API key configuration
14
+ with st.sidebar:
15
+ st.header("API Configuration")
16
+ api_key = st.text_input("OpenAI API Key", type="password")
17
+
18
+ # Main form
19
+ with st.form("query_form"):
20
+ user_input = st.text_area(
21
+ "Enter your data request in natural language:",
22
+ placeholder="e.g. Show all orders from last week",
23
+ height=150
24
+ )
25
+ submitted = st.form_submit_button("Generate Query")
26
+
27
+ if submitted:
28
+ if not api_key:
29
+ st.error("🔑 API key is required!")
30
+ elif not user_input:
31
+ st.error("📝 Please enter your data request!")
32
+ else:
33
+ try:
34
+ # Initialize OpenAI client
35
+ client = OpenAI(api_key=api_key)
36
+
37
+ # System context with schema information
38
+ system_context = """Given the following SQL tables, your job is to write queries given a user's request.
39
+ CREATE TABLE Produkte (
40
+ ProduktID INTEGER PRIMARY KEY AUTOINCREMENT,
41
+ Produktname TEXT NOT NULL,
42
+ Preis REAL NOT NULL
43
+ );
44
+
45
+ CREATE TABLE Bestellungen (
46
+ BestellungID INTEGER PRIMARY KEY AUTOINCREMENT,
47
+ ProduktID INTEGER NOT NULL,
48
+ Menge INTEGER NOT NULL,
49
+ Bestelldatum TEXT NOT NULL,
50
+ Person TEXT NOT NULL,
51
+ FOREIGN KEY (ProduktID) REFERENCES Produkte(ProduktID)
52
+ );"""
53
+
54
+ # Generate SQL query using OpenAI
55
+ response = client.chat.completions.create(
56
+ model="gpt-4o",
57
+ messages=[
58
+ {"role": "system", "content": system_context},
59
+ {"role": "user", "content": f"Generate the SQL query for: {user_input}. Only output the raw SQL query without any code block delimiters or markdown."}
60
+ ],
61
+ response_format={"type": "text"}
62
+ )
63
+
64
+ sql_query = response.choices[0].message.content.strip()
65
+
66
+ # Execute query and fetch results
67
+ conn = sqlite3.connect('database.db')
68
+ cursor = conn.cursor()
69
+ cursor.execute(sql_query)
70
+
71
+ results = cursor.fetchall()
72
+ column_names = [description[0] for description in cursor.description]
73
+ conn.close()
74
+
75
+ # Display results
76
+ st.subheader("Generated SQL Query")
77
+ st.code(sql_query, language="sql")
78
+
79
+ st.subheader("Query Results")
80
+ if results:
81
+ st.dataframe(
82
+ data=results,
83
+ columns=column_names,
84
+ use_container_width=True,
85
+ hide_index=True
86
+ )
87
+ else:
88
+ st.info("No results found", icon="ℹ️")
89
+
90
+ except sqlite3.Error as e:
91
+ st.error(f"SQL Error: {str(e)}")
92
+ except Exception as e:
93
+ st.error(f"An error occurred: {str(e)}")