|
|
|
|
|
|
|
|
from dotenv import load_dotenv
|
|
|
|
|
|
load_dotenv()
|
|
|
|
|
|
import streamlit as st
|
|
|
import os
|
|
|
import sqlite3
|
|
|
import google.generativeai as genai
|
|
|
|
|
|
|
|
|
genai.configure(api_key=os.getenv("GOOGLE_API_KEY"))
|
|
|
|
|
|
|
|
|
|
|
|
def get_gemini_response(question, prompt):
|
|
|
model = genai.GenerativeModel('gemini-2.0-flash')
|
|
|
response = model.generate_content([prompt[0], question])
|
|
|
return response.text
|
|
|
|
|
|
|
|
|
|
|
|
def read_sql_query(sql, db):
|
|
|
try:
|
|
|
conn = sqlite3.connect(db)
|
|
|
cur = conn.cursor()
|
|
|
cur.execute(sql)
|
|
|
rows = cur.fetchall()
|
|
|
conn.commit()
|
|
|
conn.close()
|
|
|
return rows
|
|
|
except sqlite3.Error as e:
|
|
|
return [("Error:", str(e))]
|
|
|
|
|
|
|
|
|
|
|
|
def ensure_table_exists():
|
|
|
conn = sqlite3.connect("student.db")
|
|
|
cursor = conn.cursor()
|
|
|
cursor.execute("""
|
|
|
CREATE TABLE IF NOT EXISTS STUDENT(
|
|
|
NAME VARCHAR(25),
|
|
|
CLASS VARCHAR(25),
|
|
|
SECTION VARCHAR(25),
|
|
|
MARKS INT);
|
|
|
""")
|
|
|
conn.commit()
|
|
|
conn.close()
|
|
|
|
|
|
|
|
|
|
|
|
prompt = [
|
|
|
"""
|
|
|
You are an expert in converting English questions to SQL queries!
|
|
|
The SQL database has the name STUDENT and has the following columns - NAME, CLASS,
|
|
|
SECTION, MARKS.\n\nFor example:
|
|
|
- How many entries of records are present?
|
|
|
SQL: SELECT COUNT(*) FROM STUDENT;
|
|
|
- Tell me all the students studying in Data Science class?
|
|
|
SQL: SELECT * FROM STUDENT WHERE CLASS="Data Science";
|
|
|
|
|
|
**Do NOT include "```sql" or "```" in the response.**
|
|
|
"""
|
|
|
]
|
|
|
|
|
|
|
|
|
st.set_page_config(page_title="Advanced SQL Query Generator", layout="wide")
|
|
|
|
|
|
|
|
|
with st.sidebar:
|
|
|
st.image("https://cdn-icons-png.flaticon.com/512/2721/2721292.png", width=100)
|
|
|
st.title("π How It Works")
|
|
|
st.markdown(
|
|
|
"1οΈβ£ **Enter a question in plain English** \n"
|
|
|
"2οΈβ£ **The AI converts it into an SQL query** \n"
|
|
|
"3οΈβ£ **The app fetches results from the database** \n"
|
|
|
"4οΈβ£ **You get an answer instantly! π―**"
|
|
|
)
|
|
|
|
|
|
st.warning("β οΈ **First, run all three files (sqlite.py, sqlite_copy.py, and sql.py) to ensure the database exists.** Then, ask your query.")
|
|
|
st.markdown("---")
|
|
|
st.write("π©βπ» **Created By:** Prarthana π")
|
|
|
|
|
|
|
|
|
st.markdown(
|
|
|
"<h1 style='text-align: center; color: #1f77b4;'>π― AI-Powered SQL Query Generator π</h1>",
|
|
|
unsafe_allow_html=True
|
|
|
)
|
|
|
|
|
|
|
|
|
question = st.text_input("π **Enter Your Question:**", key="input")
|
|
|
|
|
|
|
|
|
if st.button("π Get SQL Query & Fetch Data"):
|
|
|
if question:
|
|
|
ensure_table_exists()
|
|
|
|
|
|
with st.spinner("π€ Generating SQL query..."):
|
|
|
sql_query = get_gemini_response(question, prompt)
|
|
|
|
|
|
st.success(f"β
Generated Query: `{sql_query}`")
|
|
|
|
|
|
|
|
|
with st.spinner("π‘ Fetching Data from Database..."):
|
|
|
response = read_sql_query(sql_query, "student.db")
|
|
|
|
|
|
if response:
|
|
|
st.subheader("π **Query Results:**")
|
|
|
for row in response:
|
|
|
st.write(row)
|
|
|
else:
|
|
|
st.warning("β οΈ No data found in the database.")
|
|
|
else:
|
|
|
st.error("β Please enter a valid question.")
|
|
|
|
|
|
|
|
|
st.markdown("<br><br>", unsafe_allow_html=True)
|
|
|
st.markdown("<h4 style='text-align: center;'>β¨ Powered by Google Gemini Pro β¨</h4>", unsafe_allow_html=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|