CoolFace
Apppublic

KenanKhan/Text-To-SQL-GenerativeAI

sourceHugging Faceapache-2.0updated 2y agoView on Hugging Face
0likes
app.py67 linesDownload Raw Back to root
1from dotenv import load_dotenv
2load_dotenv() ## load all env vars
3
4import streamlit as st
5import os
6import sqlite3
7
8import google.generativeai as genai
9
10genai.configure(api_key=os.getenv("GOOGLE_API_KEY"))
11
12## load Gemini
13def get_gemini_response(question,prompt):
14    model=genai.GenerativeModel('gemini-pro')
15    response=model.generate_content([prompt[0],question])
16    return response.text
17
18## function to retrieve query
19def read_sql_query(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 rows
29
30## Define prompt
31prompt=[
32    """
33    You are an expert in converting English questions to SQL query!
34    The SQL database has the name STUDENT and has the following columns - NAME, CLASS, 
35    SECTION \n\nFor example,\nExample 1 - How many entries of records are present?, 
36    the SQL command will be something like this SELECT COUNT(*) FROM STUDENT ;
37    \nExample 2 - Tell me all the students studying in Data Science class?, 
38    the SQL command will be something like this SELECT * FROM STUDENT 
39    where CLASS="Data Science"; 
40    also the sql code should not have ``` in beginning or end and sql word in output
41    """
42]
43
44## streamlit
45st.set_page_config(page_title="I can Retrieve Any SQL query")
46st.header("Gemini App To Retrieve SQL Data")
47
48question=st.text_input("Input: ",key="input")
49
50submit=st.button("Ask the question")
51
52## submit
53if submit:
54    response=get_gemini_response(question, prompt)
55    print(response)
56    response=read_sql_query(response, "student.db")
57    st.subheader("The Response is")
58    for row in response:
59        print(row)
60        st.header(row)\
61        
62## tell me the student name in the Data Science class
63## tell me the class of the Krish and Gonzo
64## tell me the student name from section B
65## tell me the student whose marks are greater than 90
66## tell me the student names based on marks rank
67## streamlit run sql.py