Upload 3 files
Browse files- app.py +55 -0
- requirements.txt +3 -0
- student_marks.db +0 -0
app.py
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from dotenv import load_dotenv
|
| 2 |
+
load_dotenv() # Load all the environment variables
|
| 3 |
+
|
| 4 |
+
import streamlit as st
|
| 5 |
+
import os
|
| 6 |
+
import sqlite3
|
| 7 |
+
import google.generativeai as genai
|
| 8 |
+
|
| 9 |
+
# Configure Genai Key
|
| 10 |
+
genai.configure(api_key=os.getenv("GOOGLE_API_KEY"))
|
| 11 |
+
|
| 12 |
+
# Function to Load Google Gemini Model and provide queries as response
|
| 13 |
+
def 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 from the database
|
| 19 |
+
def 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 |
+
return rows
|
| 27 |
+
|
| 28 |
+
# Define Your Prompt
|
| 29 |
+
prompt = [
|
| 30 |
+
"""
|
| 31 |
+
You are an expert in converting English questions to SQL query!
|
| 32 |
+
The SQL database has the name STUDENT and has the following columns - NAME, CLASS,
|
| 33 |
+
SECTION \n\nFor example,\nExample 1 - How many entries of records are present?,
|
| 34 |
+
the SQL command will be something like this SELECT COUNT(*) FROM STUDENT ;
|
| 35 |
+
\nExample 2 - Tell me all the students studying in Data Science class?,
|
| 36 |
+
the SQL command will be something like this SELECT * FROM STUDENT
|
| 37 |
+
where CLASS="Data Science";
|
| 38 |
+
also the sql code should not have ``` in the beginning or end and sql word in output
|
| 39 |
+
"""
|
| 40 |
+
]
|
| 41 |
+
|
| 42 |
+
# Streamlit App
|
| 43 |
+
st.set_page_config(page_title="I can Retrieve Any SQL query")
|
| 44 |
+
st.title("🔍 Gemini App To Retrieve SQL Data")
|
| 45 |
+
|
| 46 |
+
question = st.text_input("Ask your question: ", key="input")
|
| 47 |
+
submit = st.button("Get SQL Query")
|
| 48 |
+
|
| 49 |
+
# if submit is clicked
|
| 50 |
+
if submit:
|
| 51 |
+
with st.spinner("Analyzing your question..."):
|
| 52 |
+
response = get_gemini_response(question, prompt)
|
| 53 |
+
st.success("Here's the response:")
|
| 54 |
+
for row in read_sql_query(response, "student_marks.db"):
|
| 55 |
+
st.write(f"**{row[0]}**") # Display each response with an icon and semi-formal style
|
requirements.txt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
streamlit
|
| 2 |
+
google-generativeai
|
| 3 |
+
python-dotenv
|
student_marks.db
ADDED
|
Binary file (8.19 kB). View file
|
|
|