ravi46931/textToSQLGenAI
0
1from dotenv import load_dotenv2load_dotenv()3import streamlit as st4import os5import sqlite36import google.generativeai as genai7 8## Configure genai key9genai.configure(api_key=os.getenv("GOOGLE_API_KEY"))10 11## Function to load Google Gemini Model12def get_gemini_response(question, prompt):13 model=genai.GenerativeModel('gemini-pro')14 response=model.generate_content([prompt[0], question])15 return response.text16 17## Function to retrieve query from database18def read_sql_query(sql, db):19 conn=sqlite3.connect(db)20 cursor=conn.cursor()21 cursor.execute(sql)22 rows=cursor.fetchall()23 conn.commit()24 conn.close()25 for row in rows:26 print(row)27 return rows 28 29## Define the prompt30prompt=[31 """32 You are an expert in converting the English questions into SQL query!33 The SQL database has the name STUDENT and has columns - NAME, CLASS, SECTION \n\n 34 For example, \n Example1 - How many entries of records are present?, the SQL command will be something like this 35 SELECT COUNT(*) FROM STUDENT;36 also the sql code should not have ``` in the beginning or end and sql word in output37 """38]39 40## Streamlit app41st.set_page_config(page_title ="I can retrieve any query")42st.header("Gemini app to retrieve SQL Data")43question = st.text_input("Input: ", key= 'input')44submit=st.button("Ask the question: ")45if submit:46 response = get_gemini_response(question, prompt)47 response = read_sql_query(response, "student.db")48 st.subheader("The response is: ")49 for row in response:50 print(row)51 st.header(row)