Team Ai
Apppublic

Kiash/texttoSQLgenerativeAI

sourceHugging Facemitupdated 3y agoView on Hugging Face
0likes
app.py63 linesDownload Raw Back to root
1import streamlit as st2from dotenv import load_dotenv3import google.generativeai as genai4import os5import sqlite36# Load the environment variables7load_dotenv()8# Configure the API key9genai.configure(api_key=os.getenv("Google_api_key"))10 11#create a functipn to load  gen ai model and provide Query12def get_gemini_response(question, prompt):13    model = genai.GenerativeModel('gemini-pro')14    full_prompt = prompt[0] + " " + question15    response = model.generate_content(full_prompt)16    return response.text17 18#fucntion to reteive Query from the database19def read_sql_quert(sql,db):20    conn=sqlite3.connect(db)21    cur=conn.cursor()22    cur.execute(sql)23    rows=cur.fetchall()24    conn.commit()25    conn.close()26    for row in rows:27        print(row)28    return rows29 30#Define the Promopt31prompt=[32    """33    You are an expert in converting English questions into Sql code!34    The SQL database has the name STUDENT and has the following columns- NAME,CLASS,SECTION\n\n35    For example,\nExample 1 - How many entries of records are present ?,36    the SQL command will be something like this SELECT COUNT(*) FROM STUDENT ;37    38    \nExample 2 - tell me all Students study in data science class?,39    the SQL command will be something like this SELECT * FROM STUDENT where class ="Data Science" ;40    also the sql code should not have ``` in the beginning or end and sql word output41    42    \nExample 2 - tell me all Students study in data science class?,43    the SQL command will be something like this SELECT * FROM STUDENT where class ="web dev" ;44    also the sql code should not have ``` in the beginning or end and sql word output45    46    """47]48 49#setting the streamlit app50st.set_page_config(page_title="I Can retrive any SQL Query")51st.header("GEMini APP to Retieve SQL DATA")52 53question=st.text_input("input",key='input')54 55submit=st.button("Ask Question")56 57if submit:58    response=get_gemini_response(question,prompt)59    response=read_sql_quert(response,"student.db")60    st.subheader("Response is")61    for row in response:62        print(row)63        st.header(row)