harshpatel080503/Text-to-SQL-Generative-AI
0
1from dotenv import load_dotenv2load_dotenv() ## Load all the environment variables3 4import streamlit as st5import os6import sqlite37 8import google.generativeai as genai9 10## Configure our API Key11genai.configure(api_key=os.getenv("GOOGLE_API_KEYS"))12 13# Function to Load Google Gemini Model and provide sql query as response14 15def get_gemini_response(question,prompt):16 model = genai.GenerativeModel('gemini-pro')17 response = model.generate_content([prompt[0],question])18 return response.text19 20## Function to retrieve query from the sql database21def read_sql_query(sql,db):22 conn = sqlite3.connect(db)23 cur = conn.cursor()24 cur.execute(sql)25 rows = cur.fetchall()26 conn.commit()27 conn.close()28 for row in rows:29 print(row)30 return rows31 32## Define Your Prompt33prompt =[34 """35 You are an expert in converting English questions to SQL query!36 The SQL database has the name STUDENT and has the following columns - NAME, CLASS,37 SECTION and MARKS \n\nFor example,\nExample 1 - How many entries of records are present?,38 the SQL command will be something like the SELECT COUNT(*) FROM STUDENT;39 \nExample 2 - Tell me all the students studying in Data Science class?,40 the SQL command will be something like this SELECT * FROM STUDENT 41 where CLASS="Data Science";42 also the sql code should not have ```in beginning or end in sql word in output43 """44]45 46## Streamlit App47 48st.set_page_config(page_title="I can Retrieve Any SQL query")49st.header("Gemini App to Retrieve SQL Data")50 51question = st.text_input("Input:",key="input")52 53submit = st.button("Ask the question")54 55# if submit is clicked56if submit:57 response = get_gemini_response(question,prompt)58 print(response)59 data = read_sql_query(response,"student.db")60 st.subheader("The Response is")61 for row in data:62 print(row)63 st.header(row)