CoolFace
Apppublic

lx160cm/text-to-sql-generativeai

sourceHugging Faceapache-2.0updated 2y agoView on Hugging Face
0likes
app.py77 linesDownload Raw Back to root
1from dotenv import load_dotenv2load_dotenv() ## load all the environemnt variables3 4import streamlit as st5import os6import sqlite37 8import google.generativeai as genai9## Configure Genai Key10 11genai.configure(api_key=os.getenv("GOOGLE_API_KEY"))12 13## Function To Load Google Gemini Model and provide queries 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## Fucntion To retrieve query from the database21 22def read_sql_query(sql,db):23    conn=sqlite3.connect(db)24    cur=conn.cursor()25    cur.execute(sql)26    rows=cur.fetchall()27    conn.commit()28    conn.close()29    for row in rows:30        print(row)31    return rows32 33## Define Your Prompt34prompt=[35    """36    You are an expert in converting English questions to SQL query!37    The SQL database has the name STUDENT and has the following columns - NAME, CLASS, 38    SECTION \n\nFor example,\nExample 1 - How many entries of records are present?, 39    the SQL command will be something like this SELECT COUNT(*) FROM STUDENT ;40    \nExample 2 - Tell me all the students studying in Data Science class?, 41    the SQL command will be something like this SELECT * FROM STUDENT 42    where CLASS="Data Science"; Please dont do any update or delete queries 43    also the sql code should not have ``` in beginning or end and sql word in output44 45    """46 47 48]49 50## Streamlit App51 52st.set_page_config(page_title="I can Retrieve Any SQL query")53st.header("Gemini App To Retrieve SQL Data")54 55question=st.text_input("Input: ",key="input")56 57submit=st.button("Ask the question")58 59# if submit is clicked60if submit:61    response=get_gemini_response(question,prompt)62    print(response)63    response=read_sql_query(response,"student.db")64    st.subheader("The Response From Alex Corporation")65    for row in response:66        print(row)67        st.header(row)68 69 70 71 72 73 74 75 76 77