Team Ai
Apppublic

Adroit10/Text_to_SQL_GenAI

sourceHugging Faceapache-2.0updated 3y agoView on Hugging Face
0likes
app.py63 linesDownload Raw Back to root
1from dotenv import load_dotenv2load_dotenv() # load all environment vars3 4import streamlit as st5import os6import sqlite37 8import google.generativeai as genai9 10genai.configure(api_key=os.getenv("GOOGLE_API_KEY"))11 12## Function to load Google Gemini Model and provide queries as response13 14def get_gemini_response(question,prompt):15    model=genai.GenerativeModel('gemini-pro')16    response=model.generate_content([prompt[0],question])17    return response.text18 19## function to retrieve query from database20 21def 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    for row in rows:29        print(row)30    return rows31 32## Define Your Prompt33prompt=[34    """35    You are an expert in converting English questions to SQL query!36    The SQL database has the name STUDENT and has the following columns - NAME, CLASS, 37    SECTION \n\nFor example,\nExample 1 - How many entries of records are present?, 38    the SQL command will be something like this SELECT COUNT(*) FROM STUDENT ;39    \nExample 2 - Tell me all the students studying in Data Science class?, 40    the SQL command will be something like this SELECT * FROM STUDENT 41    where CLASS="Data Science"; 42    also the sql code should not have ``` in beginning or end and sql word in output43 44    """45]46 47## Streamlit app48 49st.set_page_config(page_title="I can retrieve any SQL ")50st.header("Gemini App to retrieve SQL Data")51 52question=st.text_input("Input: ",key="input")53submit=st.button("Ask the question")54 55if submit:56    response=get_gemini_response(question,prompt)57    print(response)58    response=read_sql_query(response,"student.db")59    st.subheader("The response is")60    for row in response:61        print(row)62        st.header(row)63