Spaces:
Sleeping
Sleeping
File size: 2,059 Bytes
1c3605b |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
from dotenv import load_dotenv
import streamlit as st
import os
import sqlite3
import google.generativeai as genai
import warnings
warnings.filterwarnings("ignore")
# Load all the environment variables
load_dotenv()
# Configure genAI key
genai.configure(api_key=os.getenv("GOOGLE_API_KEY"))
# Create a function to load the Gemini model and get a response
def get_gemini_response(query, prompt):
model = genai.GenerativeModel("gemini-pro")
response = model.generate_content([prompt[0], query])
print(response.text)
return response.text
# Function to retrieve query from the database
def read_sql_query(sql, db):
try:
connection = sqlite3.connect(db)
cursor = connection.cursor()
cursor.execute(sql)
rows = cursor.fetchall()
connection.commit()
for row in rows:
print(row)
except sqlite3.Error as e:
# st.error(f"An error occurred: {e}")
rows = []
finally:
connection.close()
return rows
# Define the prompt
prompt = ["""
You are an expert in converting English questions to SQL code!
The SQL database is named STUDENT and has the following columns: Name, Class, Section.
For example,
Example 1: How many entries of records are present?
The SQL command will be something like this: SELECT COUNT(*) FROM STUDENT;
Ensure that the SQL code in the response does not have ''' at the beginning or end of the response and does not contain the word 'sql' in the output.
"""]
# Streamlit app configuration
st.set_page_config(page_title="RetrieveGPT")
st.header("Retrieve_GPT")
question = st.text_input("Input your question:", key='input')
submit = st.button("Submit")
# If submit is clicked
if submit:
response = get_gemini_response(question, prompt)
if response:
results = read_sql_query(response, "Student.db")
st.subheader("Output:")
if results:
for row in results:
st.write(row)
else:
st.write("No results found or an error occurred.")
|