CoolFace
Apppublic

kivtim/textToSQLGenAI

sourceHugging Faceapache-2.0updated 3y agoView on Hugging Face
0likes
app.py67 linesDownload Raw Back to root
1######### IMPORT LIBRARIES AND LOAD ENVIRONMENTS VARIABLE ##############2 3from dotenv import load_dotenv4load_dotenv()5 6import streamlit as st 7import os8import sqlite39 10import google.generativeai as genai11 12## CONFIGURE GENAI KEY13genai.configure(api_key=os.getenv("GOOGLE_API_KEY"))14 15## CREATE FUNCTION TO LOAD GOOGLE GEMINI MODEL AND PROVIDE QUERIES AS RESPONSE16 17def get_gemini_response(question, prompt):18    model = genai.GenerativeModel("gemini-pro")19    response = model.generate_content([prompt[0], question])20    print(response.text)21    return response.text22 23## CRATE FUNCTION TO RETRIEVE QUERIES FROM DATAABASE24 25def read_sql_query(sql, db):26    conn = sqlite3.connect(db)27    cur = conn.cursor()28    cur.execute(sql)29    rows = cur.fetchall()30    conn.commit()31    conn.close()32    for row in rows:33        print(row)34    return rows35    36 37### DEFINE YOUR PROMPT38 39prompt = [40    """41    You are an expert in converting English questions  to SQL query!42    The SQL database has the name STUDENT and has the following columns - NAME, CLASS, SECTION, MARKS43    \n\nFor example, \nExample 1 - How many entries of the records are present?,44    the SQL command will be something like this - SELECT COUNT(*) FROM STUDENT;45    \nExample 2- Telll me all the students studying in Data Science?,46    the SQL command will be something like this - SELECT * FROM STUDENT WHERE CLASS="Data Science";47    also the sql code should not have ``` in the beginning or end and sql word in output48    """49]50 51#### CREATE STREAM APP52 53st.set_page_config(page_title="I can retrieve Any SQL Query")54st.header("Gemini App To Retrieve SQL Data")55 56question = st.text_input("Input: ", key="input")57submit = st.button("Ask the question")58 59## IF I SUBMIT  IS CLICKED 60 61if submit:62    response = get_gemini_response(question, prompt)63    response= read_sql_query(response, "student.db")64    st.subheader("The response is ")65    for row in response:66        print(row)67        st.header(row)