Team Ai
Apppublic

sibasispradhan/Text-To-SQL-Generative-AI

sourceHugging Faceapache-2.0updated 3y agoView on Hugging Face
1likes
app.py65 linesDownload Raw Back to root
1from dotenv import load_dotenv2load_dotenv() ## load all the env valriable3 4import streamlit as st5import os6import sqlite37 8import google.generativeai as genai9 10## configure genai key11 12genai.configure(api_key=os.getenv("GOOGLE_API_KEY"))13 14## function to load google gemini model15 16def 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 retrive query from the database22 23def read_sql_query(sql,db):24    conn = sqlite3.connect(db)25    cur = conn.cursor()26    cur.execute(sql)27    rows = cur.fetchall()28    #conn.commit()29    conn.close()30    for row in rows:31        print(row)32    return rows33 34## define prompt35prompt=[36    """37    You are an expert in connecting English questions to SQL query!38    The SQL database has the name Student and has the following columns - Name, Class, 39    Section \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 DS class?, 42      the SQL command will be something like this SELECT * FROM Student 43      where Class="DS"; 44      also the sql code should not have ``` in beginning or end and sql word in output45    """46]47 48## streamlit app49 50st.set_page_config(page_title="I can retrive any SQL query")51st.header("Gemini App To Retrive SQL Data")52 53question=st.text_input("Input: ",key="input")54submit=st.button("Ask the question")55 56## if submit is clicked57if submit:58    response=get_gemini_response(question,prompt)59    print(response)60    response = read_sql_query(response, "student.db")61    st.subheader("The response is")62    for row in response:63        print(row)64        st.header(row)65