CoolFace
Apppublic

abdul44haseeb/Text_To_SQL_GenerativeAI

sourceHugging Faceapache-2.0updated 3y agoView on Hugging Face
0likes
app.py69 linesDownload Raw Back to root
1from dotenv import load_dotenv2load_dotenv() ## load all the environment variables3 4import streamlit as st5import os6import sqlite37 8import google.generativeai as genai9 10##Configure Genai key11 12genai.configure(api_key=os.getenv("GOOGLE_API_KEY"))13 14## Function to load google gemini model and provide queries as reponse15 16def get_gemini_response(question,prompt):17    model=genai.GenerativeModel('gemini-pro')18    response=model.generate_content([prompt[0],question])19    return response.text20 21 22## Function to retrieve query from database23 24def read_sql_query(sql,db):25    conn=sqlite3.connect(db)26    cur=conn.cursor()27    cur.execute(sql)28    rows=cur.fetchall()29    conn.commit()30    conn.close()31    for row in rows:32        print(row)33    return rows34 35 36## Define your prompt37 38prompt=[39  """40      You are an expert in converting English questions to SQL query!41      The SQL database has the name STUDENT and has the following columns - NAME, CLASS, 42      SECTION \n\nFor example,\nExample 1 - How many entries of records are present?, 43      the SQL command will be something like this SELECT COUNT(*) FROM STUDENT ;44      \nExample 2 - Tell me all the students studying in Data Science class?, 45      the SQL command will be something like this SELECT * FROM STUDENT where CLASS="Data Science"; 46      also the sql code should not have ``` in beginning or end and sql word in output47 48      """49]50 51 52## streamlit app53 54st.set_page_config(page_title="I can retrive any sql query")55st.header("Gemini App to retrive SQL Data")56 57question=st.text_input("Input: " ,key="input")58 59submit=st.button("Ask the question")60 61# if submit is clicked62 63if submit:64    response=get_gemini_response(question,prompt)65    response=read_sql_query(response,"student.db")66    st.subheader("The response is")67    for row in response:68        print(row)69        st.header(row)