Team Ai
Apppublic

kunalmishravitb/Text_to_SQL_Generative_AI

sourceHugging Faceapache-2.0updated 3y agoView on Hugging Face
0likes
app.py67 linesDownload Raw Back to root
1# This file will be responsible for creating our LLM Application2 3from dotenv import load_dotenv4load_dotenv() # load all the environment variables5 6import streamlit as st7import os8import sqlite39 10import google.generativeai as genai11 12# Configure Genai Key13genai.configure(api_key=os.getenv("GOOGLE_API_KEY"))14 15# Function to Load Google Gemini Model and provide queries as response16def get_gemini_response(question, prompt): # question is the input we are giving17    model=genai.GenerativeModel('gemini-pro')18    response=model.generate_content([prompt[0], question])19    return response.text20 21# Function to retrieve queries from the database22def read_sql_query(sql, db):23    conn=sqlite3.connect(db) # This connect is with respect to db24    cur=conn.cursor() # Cursor is created25    cur.execute(sql) # The cursor will be responsible for executing sql query26    rows=cur.fetchall() # It will fetch all the records with respect to that27    conn.commit()28    conn.close()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 \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]48 49# Streamlit App50st.set_page_config(page_title="I Can Retrieve Any SQL Query")51st.header("Gemini App to Retrieve SQL Data")52 53question=st.text_input("Input: ", key="input")54 55submit=st.button("Ask the question")56 57# If submit is clicked58if submit:59    response=get_gemini_response(question, prompt)60    print(response)61    response=read_sql_query(response, "student.db")62    st.subheader("The Response is")63    for row in response:64        print(row) # Printing all the rows65        st.header(row)66 67