Sebbe33 commited on
Commit
9a8f90f
Β·
verified Β·
1 Parent(s): 39616dc

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +38 -39
app.py CHANGED
@@ -1,14 +1,12 @@
1
  import streamlit as st
2
- from openai import OpenAI
3
  import sqlite3
4
- import pandas as pd # Add to top of file
5
-
6
 
7
  # App UI
8
  st.set_page_config(page_title="Zero SQL", layout="wide")
9
  st.title("Zero SQL - Natural Language to SQL Query")
10
 
11
-
12
  # Database initialization with caching
13
  @st.cache_resource
14
  def initialize_database():
@@ -61,11 +59,10 @@ def initialize_database():
61
  # Initialize the database
62
  initialize_database()
63
 
64
-
65
  # Sidebar for API key
66
  with st.sidebar:
67
  st.header("Configuration")
68
- api_key = st.text_input("OpenAI API Key", type="password")
69
  st.markdown("---")
70
  st.markdown("**Sample Questions:**")
71
  st.markdown("- Show total sales per product")
@@ -82,46 +79,48 @@ with st.form("query_form"):
82
  submitted = st.form_submit_button("πŸš€ Generate Query")
83
 
84
  if submitted:
85
- if not api_key:
86
- st.error("πŸ”‘ API key is required!")
87
- elif not user_input:
88
  st.error("πŸ“ Please enter your data request!")
89
  else:
90
  try:
91
- client = OpenAI(api_key=api_key)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
92
 
93
- # System prompt with schema
94
- system_context = """You are a SQL expert. Given these SQL tables:
95
- CREATE TABLE Produkte (
96
- ProduktID INTEGER PRIMARY KEY,
97
- Produktname TEXT NOT NULL,
98
- Preis REAL NOT NULL
99
- );
100
- CREATE TABLE Bestellungen (
101
- BestellungID INTEGER PRIMARY KEY,
102
- ProduktID INTEGER,
103
- Menge INTEGER,
104
- Bestelldatum TEXT,
105
- Person TEXT,
106
- FOREIGN KEY (ProduktID) REFERENCES Produkte(ProduktID)
107
- );
108
- Generate ONLY the raw SQL query for the user's request.
109
- NEVER use markdown code blocks or any formatting.
110
- ONLY output the pure SQL statement."""
111
-
112
- # Generate SQL query
113
- response = client.chat.completions.create(
114
- model="gpt-4o",
115
- messages=[
116
- {"role": "system", "content": system_context},
117
- {"role": "user", "content": user_input}
118
- ],
119
- temperature=0.3
120
  )
121
 
122
  # Clean up the response
123
- sql_query = response.choices[0].message.content.strip()
124
- sql_query = sql_query.replace("```sql", "").replace("```", "").strip() # Add this line
125
 
126
  # Execute and display results
127
  with sqlite3.connect('database.db') as conn:
 
1
  import streamlit as st
 
2
  import sqlite3
3
+ import pandas as pd
4
+ import google.generativeai as genai
5
 
6
  # App UI
7
  st.set_page_config(page_title="Zero SQL", layout="wide")
8
  st.title("Zero SQL - Natural Language to SQL Query")
9
 
 
10
  # Database initialization with caching
11
  @st.cache_resource
12
  def initialize_database():
 
59
  # Initialize the database
60
  initialize_database()
61
 
 
62
  # Sidebar for API key
63
  with st.sidebar:
64
  st.header("Configuration")
65
+ api_key_iput = st.text_input("Gemini API Key", type="password")
66
  st.markdown("---")
67
  st.markdown("**Sample Questions:**")
68
  st.markdown("- Show total sales per product")
 
79
  submitted = st.form_submit_button("πŸš€ Generate Query")
80
 
81
  if submitted:
82
+ api_key = api_key_iput or os.getenv("GEMINI_API_KEY")
83
+
84
+ if not user_input:
85
  st.error("πŸ“ Please enter your data request!")
86
  else:
87
  try:
88
+ # Configure Gemini
89
+ genai.configure(api_key=api_key)
90
+ model = genai.GenerativeModel('gemini-2.0-flash')
91
+
92
+
93
+ # Schema context
94
+ system_context = """Given these SQL tables:
95
+ CREATE TABLE Produkte (
96
+ ProduktID INTEGER PRIMARY KEY,
97
+ Produktname TEXT NOT NULL,
98
+ Preis REAL NOT NULL
99
+ );
100
+ CREATE TABLE Bestellungen (
101
+ BestellungID INTEGER PRIMARY KEY,
102
+ ProduktID INTEGER,
103
+ Menge INTEGER,
104
+ Bestelldatum TEXT,
105
+ Person TEXT,
106
+ FOREIGN KEY (ProduktID) REFERENCES Produkte(ProduktID)
107
+ );
108
+ Generate ONLY the raw SQL query for the following request.
109
+ Output ONLY the pure SQL statement without any formatting,
110
+ explanations, or markdown blocks."""
111
+
112
+ # Combine context with user input
113
+ full_prompt = f"{system_context}\n\nUser Request: {user_input}"
114
 
115
+ # Generate SQL query
116
+ response = model.generate_content(
117
+ full_prompt,
118
+ generation_config={"temperature": 0.3}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
119
  )
120
 
121
  # Clean up the response
122
+ sql_query = response.text.strip()
123
+ sql_query = sql_query.replace("```sql", "").replace("```", "").strip()
124
 
125
  # Execute and display results
126
  with sqlite3.connect('database.db') as conn: