CoolFace
Apppublic

Anand28/Text_to_SQL_Generative_AI

sourceHugging Faceapache-2.0updated 3y agoView on Hugging Face
0likes
app.py66 linesDownload Raw Back to root
1 2#loading environment variable3from dotenv import load_dotenv4load_dotenv()5 6import streamlit as st 7import os 8import sqlite39 10import google.generativeai as genai 11 12genai.configure(api_key=os.getenv('GOOGLE_API_KEY'))13 14# print(genai)15#function and provides queries as response16def get_gemini_response(question,prompt):17    model=genai.GenerativeModel('gemini-pro')18    response=model.generate_content([prompt[0],question])19    return response.text20 21#function to retrieve data from database22def 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 STUDENTS ;40    \nExample 2 - Tell me all the students studying in Data Science class?, 41    the SQL command will be something like this SELECT * FROM STUDENTS 42    where CLASS="Data Science"; 43    also the sql code should not have ``` in beginning or end and sql word in output44 45    """46 47 48]49 50 51#streamlit 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 59if submit:60    response = get_gemini_response(question,prompt)61    print(response)62    response=read_sql_query(response,"students.db")63    st.subheader("The Response is")64    for row in response:65        print(row[0])66        st.subheader(row[0])