amulyamuthoju/TextToSqlGenerativeAI
0
1from dotenv import load_dotenv2load_dotenv() #load all environment variables3 4import streamlit as st5import os6import sqlite37 8import google.generativeai as genai9 10#configure Genai Key11genai.configure(api_key=os.getenv("GOOGLE_API_KEY"))12 13# function to load Gemini Model and provide queries as response14def get_gemini_response(question,prompt):15 model=genai.GenerativeModel('gemini-pro')16 response=model.generate_content([prompt[0],question])17 return response.text18 19#function to retrieve query from database20def read_sql_query(sql,db):21 conn=sqlite3.connect(db)22 cur=conn.cursor()23 cur.execute(sql)24 rows=cur.fetchall()25 conn.commit()26 conn.close()27 for row in rows:28 print(row)29 return rows30 31#Define your prompt32prompt=[33 """34 You are an expert in converting English questions to SQL query!35 The SQL database has the name STUDENT and has the following columns - NAME, CLASS, 36 SECTION \n\nFor example,\nExample 1 - How many entries of records are present?,37 the SQL command will be something like this SELECT COUNT(*) FROM STUDENT ; 38 \nExample 2 - Tell me all the students studying in Data Science class?, 39 the SQL command will be something like this SELECT * FROM STUDENT 40 where CLASS="Data Science"; 41 also the sql code should not have in beginning or end and sql word in output 42 43 """]44#streamlit App45st.set_page_config(page_title="I can retrive any SQL query")46st.header("Gemini App To Retrieve SQL Data")47question= st.text_input("Input: ",key="input")48submit=st.button("Ask the Question")49 50#if submit is clicked51if submit:52 response=get_gemini_response(question,prompt)53 print(response)54 response=read_sql_query(response,"student.db")55 st.subheader("The response is")56 for row in response:57 print(row)58 st.header(row)59 60 