Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
from google import genai
|
| 3 |
+
from prompts import SYSTEM_INSTRUCTION, USER_PROMPT_TEMPLATE
|
| 4 |
+
|
| 5 |
+
st.set_page_config(page_title="SQL AI Assistant", layout="wide")
|
| 6 |
+
|
| 7 |
+
# 1. Setup Sidebar for Context
|
| 8 |
+
st.sidebar.title("🛠️ Database Context")
|
| 9 |
+
dialect = st.sidebar.selectbox("SQL Dialect", ["PostgreSQL", "MySQL", "SQLite", "BigQuery", "Snowflake"])
|
| 10 |
+
db_name = st.sidebar.text_input("Database Name", placeholder="e.g. production_db")
|
| 11 |
+
schema = st.sidebar.text_area("Table Schemas (DDL)", placeholder="CREATE TABLE users (id INT, name TEXT...)", height=300)
|
| 12 |
+
|
| 13 |
+
st.title("🤖 Gemini SQL Generator")
|
| 14 |
+
st.caption(f"Powered by Gemini 2.5 Flash")
|
| 15 |
+
|
| 16 |
+
# 2. Initialize Gemini Client
|
| 17 |
+
# In HF Spaces, go to Settings -> Secrets and add 'GEMINI_API_KEY'
|
| 18 |
+
api_key = st.secrets["GEMINI_API_KEY"]
|
| 19 |
+
client = genai.Client(api_key=api_key)
|
| 20 |
+
|
| 21 |
+
# 3. Chat Interface
|
| 22 |
+
if "messages" not in st.session_state:
|
| 23 |
+
st.session_state.messages = []
|
| 24 |
+
|
| 25 |
+
for msg in st.session_state.messages:
|
| 26 |
+
st.chat_message(msg["role"]).write(msg["content"])
|
| 27 |
+
|
| 28 |
+
if prompt := st.chat_input("Show me the top 10 users by signup date..."):
|
| 29 |
+
st.session_state.messages.append({"role": "user", "content": prompt})
|
| 30 |
+
st.chat_message("user").write(prompt)
|
| 31 |
+
|
| 32 |
+
# Build the full system instructions with current sidebar context
|
| 33 |
+
full_system_msg = SYSTEM_INSTRUCTION.format(
|
| 34 |
+
dialect=dialect,
|
| 35 |
+
db_name=db_name,
|
| 36 |
+
schema=schema if schema else "No specific schema provided."
|
| 37 |
+
)
|
| 38 |
+
|
| 39 |
+
with st.spinner("Generating SQL..."):
|
| 40 |
+
response = client.models.generate_content(
|
| 41 |
+
model="gemini-2.5-flash",
|
| 42 |
+
config={'system_instruction': full_system_msg},
|
| 43 |
+
contents=USER_PROMPT_TEMPLATE.format(user_input=prompt)
|
| 44 |
+
)
|
| 45 |
+
|
| 46 |
+
sql_output = response.text
|
| 47 |
+
st.session_state.messages.append({"role": "assistant", "content": sql_output})
|
| 48 |
+
st.chat_message("assistant").code(sql_output, language="sql")
|