Spaces:
Runtime error
Runtime error
Upload 6 files
Browse files- Dockerfile +51 -0
- requirements.txt +7 -0
- sql.py +75 -0
- sqlite copy.py +32 -0
- sqlite.py +36 -0
- student.db +0 -0
Dockerfile
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Use Ubuntu 18.04 as the base image
|
| 2 |
+
FROM ubuntu:18.04
|
| 3 |
+
|
| 4 |
+
# Set environment variables for locale
|
| 5 |
+
ENV LANG=C.UTF-8
|
| 6 |
+
ENV LC_ALL=C.UTF-8
|
| 7 |
+
|
| 8 |
+
# Install system dependencies and add Python 3.10 repository
|
| 9 |
+
RUN apt-get update && \
|
| 10 |
+
apt-get install -y \
|
| 11 |
+
software-properties-common && \
|
| 12 |
+
add-apt-repository ppa:deadsnakes/ppa && \
|
| 13 |
+
apt-get update && \
|
| 14 |
+
apt-get install -y \
|
| 15 |
+
python3.10 \
|
| 16 |
+
python3.10-distutils \
|
| 17 |
+
python3-pip \
|
| 18 |
+
build-essential \
|
| 19 |
+
libblas-dev \
|
| 20 |
+
liblapack-dev \
|
| 21 |
+
libbz2-dev \
|
| 22 |
+
zlib1g-dev \
|
| 23 |
+
liblzma-dev \
|
| 24 |
+
libsnappy-dev && \
|
| 25 |
+
rm -rf /var/lib/apt/lists/*
|
| 26 |
+
|
| 27 |
+
# Upgrade pip
|
| 28 |
+
RUN python3.10 -m pip install --upgrade pip
|
| 29 |
+
|
| 30 |
+
# Install Python packages
|
| 31 |
+
RUN python3.10 -m pip install \
|
| 32 |
+
streamlit \
|
| 33 |
+
google-generativeai \
|
| 34 |
+
langchain \
|
| 35 |
+
PyPDF2 \
|
| 36 |
+
chromadb \
|
| 37 |
+
faiss-cpu \
|
| 38 |
+
pdf2image
|
| 39 |
+
|
| 40 |
+
# Copy application files
|
| 41 |
+
COPY sql.py /app/sql.py
|
| 42 |
+
COPY sqlite.py /app/sqlite.py
|
| 43 |
+
|
| 44 |
+
# Set the working directory
|
| 45 |
+
WORKDIR /app
|
| 46 |
+
|
| 47 |
+
# Set Python 3.10 as the default Python
|
| 48 |
+
RUN update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.10 1
|
| 49 |
+
|
| 50 |
+
# Start the application
|
| 51 |
+
CMD ["streamlit", "run", "sql.py"]
|
requirements.txt
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
streamlit
|
| 2 |
+
google-generativeai
|
| 3 |
+
langchain
|
| 4 |
+
PyPDF2
|
| 5 |
+
chromadb
|
| 6 |
+
faiss-cpu
|
| 7 |
+
pdf2image
|
sql.py
ADDED
|
@@ -0,0 +1,75 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# from dotenv import load_dotenv
|
| 2 |
+
# load_dotenv() ## load all the environemnt variables
|
| 3 |
+
|
| 4 |
+
import streamlit as st
|
| 5 |
+
import os
|
| 6 |
+
import sqlite3
|
| 7 |
+
|
| 8 |
+
import google.generativeai as genai
|
| 9 |
+
import os
|
| 10 |
+
|
| 11 |
+
# Configure GenAI Key directly using environment variable or fallback to a hardcoded key
|
| 12 |
+
API_KEY = os.getenv("GOOGLE_API_KEY", "AIzaSyCOAMr9QxGUHwUO70hyHQSghHxFgtNyPsg")
|
| 13 |
+
genai.configure(api_key=API_KEY)
|
| 14 |
+
|
| 15 |
+
# Function To Load Google Gemini Model and provide queries as response
|
| 16 |
+
def get_gemini_response(question, prompt):
|
| 17 |
+
response = genai.generate_content([prompt, question])
|
| 18 |
+
return response.text
|
| 19 |
+
|
| 20 |
+
|
| 21 |
+
## Function To Load Google Gemini Model and provide queries as response
|
| 22 |
+
|
| 23 |
+
def get_gemini_response(question,prompt):
|
| 24 |
+
model=genai.GenerativeModel('gemini-pro')
|
| 25 |
+
response=model.generate_content([prompt[0],question])
|
| 26 |
+
return response.text
|
| 27 |
+
|
| 28 |
+
## Fucntion To retrieve query from the database
|
| 29 |
+
|
| 30 |
+
def read_sql_query(sql,db):
|
| 31 |
+
conn=sqlite3.connect(db)
|
| 32 |
+
cur=conn.cursor()
|
| 33 |
+
cur.execute(sql)
|
| 34 |
+
rows=cur.fetchall()
|
| 35 |
+
conn.commit()
|
| 36 |
+
conn.close()
|
| 37 |
+
for row in rows:
|
| 38 |
+
print(row)
|
| 39 |
+
return rows
|
| 40 |
+
|
| 41 |
+
## Define Your Prompt
|
| 42 |
+
prompt=[
|
| 43 |
+
"""
|
| 44 |
+
You are an expert in converting English questions to SQL query!
|
| 45 |
+
The SQL database has the name STUDENT and has the following columns - NAME, CLASS,
|
| 46 |
+
SECTION \n\nFor example,\nExample 1 - How many entries of records are present?,
|
| 47 |
+
the SQL command will be something like this SELECT COUNT(*) FROM STUDENT ;
|
| 48 |
+
\nExample 2 - Tell me all the students studying in Data Science class?,
|
| 49 |
+
the SQL command will be something like this SELECT * FROM STUDENT
|
| 50 |
+
where CLASS="Data Science";
|
| 51 |
+
also the sql code should not have ``` in beginning or end and sql word in output
|
| 52 |
+
|
| 53 |
+
"""
|
| 54 |
+
|
| 55 |
+
|
| 56 |
+
]
|
| 57 |
+
|
| 58 |
+
## Streamlit App
|
| 59 |
+
|
| 60 |
+
st.set_page_config(page_title="I can Retrieve Any SQL query")
|
| 61 |
+
st.header("SQL Data Retrieve")
|
| 62 |
+
|
| 63 |
+
question=st.text_input("Input: ",key="input")
|
| 64 |
+
|
| 65 |
+
submit=st.button("Ask the question")
|
| 66 |
+
|
| 67 |
+
# if submit is clicked
|
| 68 |
+
if submit:
|
| 69 |
+
response=get_gemini_response(question,prompt)
|
| 70 |
+
print(response)
|
| 71 |
+
response=read_sql_query(response,"student.db")
|
| 72 |
+
st.subheader("The Response is")
|
| 73 |
+
for row in response:
|
| 74 |
+
print(row)
|
| 75 |
+
st.header(row)
|
sqlite copy.py
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Import module
|
| 2 |
+
import sqlite3
|
| 3 |
+
|
| 4 |
+
# Connecting to sqlite
|
| 5 |
+
conn = sqlite3.connect('test.db')
|
| 6 |
+
|
| 7 |
+
# Creating a cursor object using the
|
| 8 |
+
# cursor() method
|
| 9 |
+
cursor = conn.cursor()
|
| 10 |
+
|
| 11 |
+
# Creating table
|
| 12 |
+
table ="""CREATE TABLE STUDENT(NAME VARCHAR(255), CLASS VARCHAR(255),
|
| 13 |
+
SECTION VARCHAR(255));"""
|
| 14 |
+
cursor.execute(table)
|
| 15 |
+
|
| 16 |
+
# Queries to INSERT records.
|
| 17 |
+
cursor.execute('''INSERT INTO STUDENT VALUES ('Krish', 'Data Science', 'A')''')
|
| 18 |
+
cursor.execute('''INSERT INTO STUDENT VALUES ('Darius', 'Data Science', 'B')''')
|
| 19 |
+
cursor.execute('''INSERT INTO STUDENT VALUES ('Sudhanshu', 'Devops', 'C')''')
|
| 20 |
+
cursor.execute('''INSERT INTO STUDENT VALUES ('Vikash', 'Data Science', 'C')''')
|
| 21 |
+
|
| 22 |
+
# Display data inserted
|
| 23 |
+
print("Data Inserted in the table: ")
|
| 24 |
+
data=cursor.execute('''SELECT * FROM STUDENT''')
|
| 25 |
+
for row in data:
|
| 26 |
+
print(row)
|
| 27 |
+
|
| 28 |
+
# Commit your changes in the database
|
| 29 |
+
conn.commit()
|
| 30 |
+
|
| 31 |
+
# Closing the connection
|
| 32 |
+
conn.close()
|
sqlite.py
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import sqlite3
|
| 2 |
+
|
| 3 |
+
## Connectt to SQlite
|
| 4 |
+
connection=sqlite3.connect("student.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()
|
| 36 |
+
|
student.db
ADDED
|
Binary file (8.19 kB). View file
|
|
|