######### IMPORT LIBRARIES AND LOAD ENVIRONMENTS VARIABLE ############## from dotenv import load_dotenv load_dotenv() import streamlit as st import os import sqlite3 import google.generativeai as genai ## CONFIGURE GENAI KEY genai.configure(api_key=os.getenv("GOOGLE_API_KEY")) ## CREATE FUNCTION TO LOAD GOOGLE GEMINI MODEL AND PROVIDE QUERIES AS RESPONSE def get_gemini_response(question, prompt): model = genai.GenerativeModel("gemini-pro") response = model.generate_content([prompt[0], question]) print(response.text) return response.text ## CRATE FUNCTION TO RETRIEVE QUERIES FROM DATAABASE def read_sql_query(sql, db): conn = sqlite3.connect(db) cur = conn.cursor() cur.execute(sql) rows = cur.fetchall() conn.commit() conn.close() for row in rows: print(row) return rows ### DEFINE YOUR PROMPT prompt = [ """ You are an expert in converting English questions to SQL query! The SQL database has the name STUDENT and has the following columns - NAME, CLASS, SECTION, MARKS \n\nFor example, \nExample 1 - How many entries of the records are present?, the SQL command will be something like this - SELECT COUNT(*) FROM STUDENT; \nExample 2- Telll me all the students studying in Data Science?, the SQL command will be something like this - SELECT * FROM STUDENT WHERE CLASS="Data Science"; also the sql code should not have ``` in the beginning or end and sql word in output """ ] #### CREATE STREAM APP st.set_page_config(page_title="I can retrieve Any SQL Query") st.header("Gemini App To Retrieve SQL Data") question = st.text_input("Input: ", key="input") submit = st.button("Ask the question") ## IF I SUBMIT IS CLICKED if submit: response = get_gemini_response(question, prompt) response= read_sql_query(response, "student.db") st.subheader("The response is ") for row in response: print(row) st.header(row)