CoolFace
Apppublic

tejas05in/Text-to-SQL-Generative-AI

sourceHugging Faceapache-2.0updated 3y agoView on Hugging Face
0likes
app.py64 linesDownload Raw Back to root
1import google.generativeai as genai2import sqlite33import os4import streamlit as st5from dotenv import load_dotenv6load_dotenv()  # load all the environment variables7 8 9# configure genai10genai.configure(api_key=os.getenv('GOOGLE_API_KEY'))11 12 13# function to load the google gemini model and provide queries as reponse14def get_gemini_response(question, prompt):15    model = genai.GenerativeModel('gemini-pro')16    response = model.generate_content([prompt[0], question])17    return response.text18 19 20# function to retrieve queries from the database21def read_sql_query(sql, db):22    conn = sqlite3.connect(db)23    cur = conn.cursor()24    cur.execute(sql)25    rows = cur.fetchall()26    conn.commit()27    conn.close()28    print(rows)29    for row in rows:30        print(row)31    return rows32 33 34# Define Your prompt35prompt = [36    """37    You are an expert in converting English questions to SQL query!38    The SQL database has the name STUDENT and has the following columns - NAME, CLASS, 39    SECTION, MARKS \n\nFor example,\nExample 1 - How many entries of records are present?, 40    the SQL command will be something like this SELECT COUNT(*) FROM STUDENT ;41    \nExample 2 - Tell me all the students studying in Data Science class?, 42    the SQL command will be something like this SELECT * FROM STUDENT 43    where CLASS="Data Science"; 44    also the sql code should not have ``` in beginning or end and sql word in output45    """46]47# Streamlit App48 49st.set_page_config(page_title="I can Retrieve Any SQL query")50st.header("Gemini APP to retrieve SQL Data")51 52question = st.text_input("Input: ", key="input")53submit = st.button("Ask this Question")54 55# if submit is clicked,56 57if submit:58    query = get_gemini_response(question, prompt)59    response = read_sql_query(query, "student.db")60    st.subheader("The response is: ")61    for row in response:62        print(row)63        st.write(row)64