Spaces:
Runtime error
Runtime error
Upload 5 files
Browse files- SQL.py +35 -0
- app.py +64 -0
- requirements.txt +9 -0
- student.db +0 -0
- students.db +0 -0
SQL.py
ADDED
@@ -0,0 +1,35 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import sqlite3
|
2 |
+
|
3 |
+
## Connectt to SQlite
|
4 |
+
connection=sqlite3.connect("students.db")
|
5 |
+
|
6 |
+
# Create a cursor object to insert record,create table
|
7 |
+
|
8 |
+
cursor=connection.cursor()
|
9 |
+
|
10 |
+
## create the table
|
11 |
+
table_info="""
|
12 |
+
Create table STUDENT(NAME VARCHAR(25),CLASS VARCHAR(25),
|
13 |
+
SECTION VARCHAR(25),MARKS INT);
|
14 |
+
|
15 |
+
"""
|
16 |
+
cursor.execute(table_info)
|
17 |
+
|
18 |
+
## Insert Some more records
|
19 |
+
|
20 |
+
cursor.execute('''Insert Into STUDENT values('Krish','Data Science','A',90)''')
|
21 |
+
cursor.execute('''Insert Into STUDENT values('Sudhanshu','Data Science','B',100)''')
|
22 |
+
cursor.execute('''Insert Into STUDENT values('Darius','Data Science','A',86)''')
|
23 |
+
cursor.execute('''Insert Into STUDENT values('Vikash','DEVOPS','A',50)''')
|
24 |
+
cursor.execute('''Insert Into STUDENT values('Dipesh','DEVOPS','A',35)''')
|
25 |
+
|
26 |
+
## Disspaly ALl the records
|
27 |
+
|
28 |
+
print("The isnerted records are")
|
29 |
+
data=cursor.execute('''Select * from STUDENT''')
|
30 |
+
for row in data:
|
31 |
+
print(row)
|
32 |
+
|
33 |
+
## Commit your changes int he databse
|
34 |
+
connection.commit()
|
35 |
+
connection.close()
|
app.py
ADDED
@@ -0,0 +1,64 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Load all environment variables
|
2 |
+
|
3 |
+
import streamlit as st
|
4 |
+
import os
|
5 |
+
import sqlite3
|
6 |
+
import google.generativeai as genai
|
7 |
+
|
8 |
+
# Configure GenAI Key
|
9 |
+
genai.configure(api_key="AIzaSyA4jlt819TA84K9zr5EUroIQK83Rsx1A6E") # Use environment variable for API key
|
10 |
+
|
11 |
+
# Function to load Google Gemini Model and provide queries as responses
|
12 |
+
def get_gemini_response(question, prompt):
|
13 |
+
try:
|
14 |
+
model = genai.GenerativeModel('gemini-pro')
|
15 |
+
response = model.generate_content([prompt[0], question])
|
16 |
+
return response.text.strip() # Ensure no extra whitespace
|
17 |
+
except Exception as e:
|
18 |
+
st.error(f"Error generating response: {e}")
|
19 |
+
return ""
|
20 |
+
|
21 |
+
# Function to retrieve query from the database
|
22 |
+
def read_sql_query(sql, db):
|
23 |
+
try:
|
24 |
+
conn = sqlite3.connect(db)
|
25 |
+
cur = conn.cursor()
|
26 |
+
cur.execute(sql)
|
27 |
+
rows = cur.fetchall()
|
28 |
+
conn.close()
|
29 |
+
return rows
|
30 |
+
except sqlite3.Error as e:
|
31 |
+
st.error(f"SQL error: {e}")
|
32 |
+
return []
|
33 |
+
|
34 |
+
# Define your prompt
|
35 |
+
prompt = [
|
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 |
+
Note: The SQL code should not have ``` in beginning or end, and should not include the word "sql".
|
45 |
+
"""
|
46 |
+
]
|
47 |
+
|
48 |
+
# Streamlit App
|
49 |
+
st.set_page_config(page_title="SQL Query Retriever")
|
50 |
+
st.header("Gemini App To Retrieve SQL Data")
|
51 |
+
|
52 |
+
question = st.text_input("Input your question:", key="input")
|
53 |
+
|
54 |
+
if st.button("Ask the question"):
|
55 |
+
response = get_gemini_response(question, prompt)
|
56 |
+
if response:
|
57 |
+
st.write(f"Generated SQL Query: `{response}`")
|
58 |
+
result = read_sql_query(response, "students.db")
|
59 |
+
if result:
|
60 |
+
st.subheader("Query Results")
|
61 |
+
for row in result:
|
62 |
+
st.write(row)
|
63 |
+
else:
|
64 |
+
st.write("No results returned or an error occurred.")
|
requirements.txt
ADDED
@@ -0,0 +1,9 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
streamlit
|
2 |
+
google-generativeai
|
3 |
+
python-dotenv
|
4 |
+
langchain
|
5 |
+
PyPDF2
|
6 |
+
chromadb
|
7 |
+
faiss-cpu
|
8 |
+
pdf2image
|
9 |
+
sqlite3
|
student.db
ADDED
File without changes
|
students.db
ADDED
File without changes
|