Spaces:
Sleeping
Sleeping
Upload 3 files
Browse files- Student.db +0 -0
- app.py +67 -0
- requirements.txt +3 -0
Student.db
ADDED
Binary file (8.19 kB). View file
|
|
app.py
ADDED
@@ -0,0 +1,67 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from dotenv import load_dotenv
|
2 |
+
import streamlit as st
|
3 |
+
import os
|
4 |
+
import sqlite3
|
5 |
+
import google.generativeai as genai
|
6 |
+
import warnings
|
7 |
+
warnings.filterwarnings("ignore")
|
8 |
+
|
9 |
+
# Load all the environment variables
|
10 |
+
load_dotenv()
|
11 |
+
|
12 |
+
# Configure genAI key
|
13 |
+
genai.configure(api_key=os.getenv("GOOGLE_API_KEY"))
|
14 |
+
|
15 |
+
|
16 |
+
# Create a function to load the Gemini model and get a response
|
17 |
+
def get_gemini_response(query, prompt):
|
18 |
+
model = genai.GenerativeModel("gemini-pro")
|
19 |
+
response = model.generate_content([prompt[0], query])
|
20 |
+
print(response.text)
|
21 |
+
return response.text
|
22 |
+
|
23 |
+
# Function to retrieve query from the database
|
24 |
+
def read_sql_query(sql, db):
|
25 |
+
try:
|
26 |
+
connection = sqlite3.connect(db)
|
27 |
+
cursor = connection.cursor()
|
28 |
+
cursor.execute(sql)
|
29 |
+
rows = cursor.fetchall()
|
30 |
+
connection.commit()
|
31 |
+
for row in rows:
|
32 |
+
print(row)
|
33 |
+
except sqlite3.Error as e:
|
34 |
+
# st.error(f"An error occurred: {e}")
|
35 |
+
rows = []
|
36 |
+
finally:
|
37 |
+
connection.close()
|
38 |
+
return rows
|
39 |
+
|
40 |
+
# Define the prompt
|
41 |
+
prompt = ["""
|
42 |
+
You are an expert in converting English questions to SQL code!
|
43 |
+
The SQL database is named STUDENT and has the following columns: Name, Class, Section.
|
44 |
+
For example,
|
45 |
+
Example 1: How many entries of records are present?
|
46 |
+
The SQL command will be something like this: SELECT COUNT(*) FROM STUDENT;
|
47 |
+
Ensure that the SQL code in the response does not have ''' at the beginning or end of the response and does not contain the word 'sql' in the output.
|
48 |
+
"""]
|
49 |
+
|
50 |
+
# Streamlit app configuration
|
51 |
+
st.set_page_config(page_title="Data Retrieval from SQL Database Using gemini-pro")
|
52 |
+
st.header("Data Retrieval from SQL Database Using gemini-pro")
|
53 |
+
|
54 |
+
question = st.text_input("Input your question:", key='input')
|
55 |
+
submit = st.button("Submit")
|
56 |
+
|
57 |
+
# If submit is clicked
|
58 |
+
if submit:
|
59 |
+
response = get_gemini_response(question, prompt)
|
60 |
+
if response:
|
61 |
+
results = read_sql_query(response, "Student.db")
|
62 |
+
st.subheader("Output:")
|
63 |
+
if results:
|
64 |
+
for row in results:
|
65 |
+
st.write(row)
|
66 |
+
else:
|
67 |
+
st.write("No results found or an error occurred.")
|
requirements.txt
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
streamlit
|
2 |
+
google-generativeai
|
3 |
+
python-dotenv
|