CoolFace
Apppublic

ATTRAIN/Text_to_SQL_query

sourceHugging Faceupdated 3y agoView on Hugging Face
0likes
app.py88 linesDownload Raw Back to root
1from dotenv import load_dotenv2import os3 4# Explicitly specify the path to your .env file5dotenv_path = "/path/to/your/.env"  # Replace this with the actual path6 7# Load variables from the .env file8load_dotenv(dotenv_path)9 10 11# Path to your .env file12dotenv_path = os.path.join(os.path.dirname(__file__), '.env')13 14# Load variables from the .env file15load_dotenv(dotenv_path)16 17 18from dotenv import load_dotenv19load_dotenv() #import all our env variables20 21import streamlit as st22import os23import sqlite324import google.generativeai as genai25 26##Configure our api key27genai.configure(api_key=os.getenv("GOOGLE_API_KEY"))28 29#function to load google gemini model and provide sql query as response30def get_gemini_response(question,prompt):31    model=genai.GenerativeModel('gemini-pro')32    response=model.generate_content([prompt[0],question])33    return response.text34 35## to retrive query from sql database36def read_sql_query(sql,db):37    conn=sqlite3.connect(db)38    cur=conn.cursor()39    cur.execute(sql)40    rows= cur.fetchall()41    conn.commit()42    conn.close()43 44    for row in rows:45        print(row)46    return rows47 48    ## Define Your Prompt49prompt=[50    """51    You are an expert in converting English questions to SQL query!52    The SQL database has the name STUDENT and has the following columns - NAME, CLASS, 53    SECTION \n\nFor example,\nExample 1 - How many entries of records are present?, 54    the SQL command will be something like this SELECT COUNT(*) FROM STUDENT ;55    \nExample 2 - Tell me all the students studying in Data Science class?, 56    the SQL command will be something like this SELECT * FROM STUDENT 57    where CLASS="Data Science"; 58    also the sql code should not have ``` in beginning or end and sql word in output59 60    """61]62 63 64## Streamlit App65 66st.set_page_config(page_title="I can Retrieve Any SQL query")67st.header("Gemini App To Retrieve SQL Data")68 69question=st.text_input("Input: ",key="input")70 71submit=st.button("Ask the question")72 73# if submit is clicked74if submit:75    response=get_gemini_response(question,prompt)76    print(response)77    response=read_sql_query(response,"student.db")78    st.subheader("The REsponse is")79    for row in response:80        print(row)81        st.header(row)82 83 84 85 86 87 88