Update app.py
Browse files
app.py
CHANGED
|
@@ -1,21 +1,15 @@
|
|
| 1 |
-
from dotenv import load_dotenv
|
| 2 |
import streamlit as st
|
| 3 |
import os
|
| 4 |
import sqlite3
|
| 5 |
import google.generativeai as genai
|
| 6 |
|
| 7 |
-
# Load environment variables
|
| 8 |
load_dotenv()
|
| 9 |
|
| 10 |
-
# # Retrieve the API key securely from Streamlit secrets
|
| 11 |
-
# GOOGLE_API_KEY = st.secrets["gemini"]["GOOGLE_API_KEY"]
|
| 12 |
-
|
| 13 |
# Configure Gemini API
|
| 14 |
genai.configure(api_key=os.getenv("GOOGLE_API_KEY"))
|
| 15 |
|
| 16 |
-
# Configure Gemini API with the API Key from secrets
|
| 17 |
-
# genai.configure(api_key=API_KEY)
|
| 18 |
-
|
| 19 |
# Function to load Gemini model and generate SQL query
|
| 20 |
def get_gemini_response(question, prompt):
|
| 21 |
model = genai.GenerativeModel('gemini-pro')
|
|
@@ -36,7 +30,7 @@ def read_sql_query(sql, db):
|
|
| 36 |
except Exception as e:
|
| 37 |
return [("Error:", str(e))] # Return error message if query fails
|
| 38 |
|
| 39 |
-
# Define prompt
|
| 40 |
prompt = """
|
| 41 |
You are an expert in SQL query generation. Your task is to convert natural language questions into valid SQL queries based on the given database schema.
|
| 42 |
|
|
@@ -59,23 +53,17 @@ Input: "Show the names of students in Data Science Section."
|
|
| 59 |
Output: SELECT NAME FROM STUDENT_INFO WHERE SECTION = 'Data Science';
|
| 60 |
"""
|
| 61 |
|
| 62 |
-
# Streamlit App
|
| 63 |
st.set_page_config(page_title="SQL Query Generator")
|
| 64 |
st.header("Gemini App To Retrieve SQL Data")
|
| 65 |
|
| 66 |
-
# Input for user's question
|
| 67 |
question = st.text_input("Enter your question:", key="input")
|
| 68 |
submit = st.button("Generate SQL Query")
|
| 69 |
|
| 70 |
-
# If submit is clicked
|
| 71 |
if submit:
|
| 72 |
sql_query = get_gemini_response(question, prompt)
|
| 73 |
st.subheader("Generated SQL Query")
|
| 74 |
st.code(sql_query, language="sql") # Show SQL query
|
| 75 |
|
| 76 |
-
# Execute the SQL query and retrieve results
|
| 77 |
response = read_sql_query(sql_query, "student.db")
|
| 78 |
-
|
| 79 |
-
# Show the query results
|
| 80 |
-
st.subheader("Query Results")
|
| 81 |
-
st.write(response)
|
|
|
|
| 1 |
+
from dotenv import load_dotenv
|
| 2 |
import streamlit as st
|
| 3 |
import os
|
| 4 |
import sqlite3
|
| 5 |
import google.generativeai as genai
|
| 6 |
|
| 7 |
+
# Load environment variables
|
| 8 |
load_dotenv()
|
| 9 |
|
|
|
|
|
|
|
|
|
|
| 10 |
# Configure Gemini API
|
| 11 |
genai.configure(api_key=os.getenv("GOOGLE_API_KEY"))
|
| 12 |
|
|
|
|
|
|
|
|
|
|
| 13 |
# Function to load Gemini model and generate SQL query
|
| 14 |
def get_gemini_response(question, prompt):
|
| 15 |
model = genai.GenerativeModel('gemini-pro')
|
|
|
|
| 30 |
except Exception as e:
|
| 31 |
return [("Error:", str(e))] # Return error message if query fails
|
| 32 |
|
| 33 |
+
# Define prompt
|
| 34 |
prompt = """
|
| 35 |
You are an expert in SQL query generation. Your task is to convert natural language questions into valid SQL queries based on the given database schema.
|
| 36 |
|
|
|
|
| 53 |
Output: SELECT NAME FROM STUDENT_INFO WHERE SECTION = 'Data Science';
|
| 54 |
"""
|
| 55 |
|
| 56 |
+
# Streamlit App
|
| 57 |
st.set_page_config(page_title="SQL Query Generator")
|
| 58 |
st.header("Gemini App To Retrieve SQL Data")
|
| 59 |
|
|
|
|
| 60 |
question = st.text_input("Enter your question:", key="input")
|
| 61 |
submit = st.button("Generate SQL Query")
|
| 62 |
|
| 63 |
+
# If submit is clicked
|
| 64 |
if submit:
|
| 65 |
sql_query = get_gemini_response(question, prompt)
|
| 66 |
st.subheader("Generated SQL Query")
|
| 67 |
st.code(sql_query, language="sql") # Show SQL query
|
| 68 |
|
|
|
|
| 69 |
response = read_sql_query(sql_query, "student.db")
|
|
|
|
|
|
|
|
|
|
|
|