CoolFace
Apppublic

Divya196/Text_To_SQL_GenerativeAI

sourceHugging Faceapache-2.0updated 3y agoView on Hugging Face
0likes
app.py72 linesDownload Raw Back to root
1from dotenv  import load_dotenv2load_dotenv()3 4import streamlit as st5import os6import sqlite37 8import google.generativeai as genai 9 10#Configure genai key11 12genai.configure(api_key = os.getenv('GOOGLE_API_KEY'))13 14#Function to load google gemini model15 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#Function to retrieve query from the database22 23def read_sql_query(sql,db):24    conn = sqlite3.connect(db)25    cur = conn.cursor()26    cur.execute(sql)27    rows = cur.fetchall()28    conn.commit()29    conn.close()30    for row in rows:31        print(row)32    return rows33 34#Defining prompt35 36prompt=[37    """38    You are an expert in converting English questions to SQL query!39    The SQL database has the name EMPLOYEE and has the following columns - NAME, TEAM, 40    DOMAIN \n\nFor example,\nExample 1 - How many entries of records are present?, 41    the SQL command will be something like this SELECT COUNT(*) FROM EMPLOYEE ;42    \nExample 2 - Tell me all the employees working in MLOPS team?, 43    the SQL command will be something like this SELECT * FROM EMPLOYEE 44    where TEAM="MLOPS"; 45    also the sql code should not have ``` in beginning or end and sql word in output46 47    """48 49 50]51 52#streamlit Application53 54st.set_page_config(page_title = "I can retrieve any SQL query")55st.header("Gemini App to Retrieve SQL Data")56 57question = st.text_input("Input : ", key = "input")58 59submit = st.button("Ask the Question")60 61#if submit button is clicked62 63if submit:64    response = get_gemini_response(question,prompt)65    print(response)66    response = read_sql_query(response,"employee.db")67    st.subheader("THE RESPONSE IS")68    for row in response:69        print(row)70        st.header(row)71    72