Update app.py
Browse files
app.py
CHANGED
|
@@ -8,39 +8,29 @@ from langchain_community.embeddings import HuggingFaceEmbeddings
|
|
| 8 |
from langchain_community.vectorstores import FAISS
|
| 9 |
from groq import Groq
|
| 10 |
import streamlit as st
|
| 11 |
-
from googleapiclient.discovery import build
|
| 12 |
-
from googleapiclient.http import MediaIoBaseDownload
|
| 13 |
-
from google.oauth2.service_account import Credentials
|
| 14 |
|
| 15 |
# Initialize Groq client
|
| 16 |
client = Groq(api_key=os.getenv("GROQ_API_KEY"))
|
| 17 |
|
| 18 |
-
#
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
fh = BytesIO()
|
| 38 |
-
downloader = MediaIoBaseDownload(fh, request)
|
| 39 |
-
done = False
|
| 40 |
-
while done is False:
|
| 41 |
-
status, done = downloader.next_chunk()
|
| 42 |
-
fh.seek(0)
|
| 43 |
-
return fh
|
| 44 |
|
| 45 |
# Function to extract text from a PDF
|
| 46 |
def extract_text_from_pdf(pdf_stream):
|
|
@@ -65,9 +55,11 @@ def create_embeddings_and_store(chunks):
|
|
| 65 |
|
| 66 |
# Function to query the vector database and interact with Groq
|
| 67 |
def query_vector_db(query, vector_db):
|
|
|
|
| 68 |
docs = vector_db.similarity_search(query, k=3)
|
| 69 |
context = "\n".join([doc.page_content for doc in docs])
|
| 70 |
|
|
|
|
| 71 |
chat_completion = client.chat.completions.create(
|
| 72 |
messages=[
|
| 73 |
{"role": "system", "content": f"Use the following context:\n{context}"},
|
|
@@ -78,24 +70,30 @@ def query_vector_db(query, vector_db):
|
|
| 78 |
return chat_completion.choices[0].message.content
|
| 79 |
|
| 80 |
# Streamlit app
|
| 81 |
-
st.title("RAG-
|
| 82 |
|
| 83 |
st.write("Processing the Data links...")
|
| 84 |
|
| 85 |
all_chunks = []
|
| 86 |
|
| 87 |
-
# Process
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 99 |
|
| 100 |
if all_chunks:
|
| 101 |
# Generate embeddings and store in FAISS
|
|
|
|
| 8 |
from langchain_community.vectorstores import FAISS
|
| 9 |
from groq import Groq
|
| 10 |
import streamlit as st
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
# Initialize Groq client
|
| 13 |
client = Groq(api_key=os.getenv("GROQ_API_KEY"))
|
| 14 |
|
| 15 |
+
# if client:
|
| 16 |
+
# st.write(f"API KEY FOUND!!!{client}")
|
| 17 |
+
# else:
|
| 18 |
+
# st.write("API KEY NOT FOUND")
|
| 19 |
+
|
| 20 |
+
# Predefined list of Google Drive links
|
| 21 |
+
drive_links = [
|
| 22 |
+
"https://drive.google.com/file/d/1xswnMcJH4KRWFICjUpszYBQCsPTykZp0/view?usp=sharing"
|
| 23 |
+
]
|
| 24 |
+
|
| 25 |
+
# Function to download PDF from Google Drive
|
| 26 |
+
def download_pdf_from_drive(drive_link):
|
| 27 |
+
file_id = drive_link.split('/d/')[1].split('/')[0]
|
| 28 |
+
download_url = f"https://drive.google.com/uc?id={file_id}&export=download"
|
| 29 |
+
response = requests.get(download_url)
|
| 30 |
+
if response.status_code == 200:
|
| 31 |
+
return BytesIO(response.content)
|
| 32 |
+
else:
|
| 33 |
+
raise Exception("Failed to download the PDF file from Google Drive.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
|
| 35 |
# Function to extract text from a PDF
|
| 36 |
def extract_text_from_pdf(pdf_stream):
|
|
|
|
| 55 |
|
| 56 |
# Function to query the vector database and interact with Groq
|
| 57 |
def query_vector_db(query, vector_db):
|
| 58 |
+
# Retrieve relevant documents
|
| 59 |
docs = vector_db.similarity_search(query, k=3)
|
| 60 |
context = "\n".join([doc.page_content for doc in docs])
|
| 61 |
|
| 62 |
+
# Interact with Groq API
|
| 63 |
chat_completion = client.chat.completions.create(
|
| 64 |
messages=[
|
| 65 |
{"role": "system", "content": f"Use the following context:\n{context}"},
|
|
|
|
| 70 |
return chat_completion.choices[0].message.content
|
| 71 |
|
| 72 |
# Streamlit app
|
| 73 |
+
st.title("RAG-Based ChatBot (Already having Document)")
|
| 74 |
|
| 75 |
st.write("Processing the Data links...")
|
| 76 |
|
| 77 |
all_chunks = []
|
| 78 |
|
| 79 |
+
# Process each predefined Google Drive link
|
| 80 |
+
for link in drive_links:
|
| 81 |
+
try:
|
| 82 |
+
# st.write(f"Processing link: {link}")
|
| 83 |
+
# Download PDF
|
| 84 |
+
pdf_stream = download_pdf_from_drive(link)
|
| 85 |
+
# st.write("PDF Downloaded Successfully!")
|
| 86 |
+
|
| 87 |
+
# Extract text
|
| 88 |
+
text = extract_text_from_pdf(pdf_stream)
|
| 89 |
+
# st.write("PDF Text Extracted Successfully!")
|
| 90 |
+
|
| 91 |
+
# Chunk text
|
| 92 |
+
chunks = chunk_text(text)
|
| 93 |
+
# st.write(f"Created {len(chunks)} text chunks.")
|
| 94 |
+
all_chunks.extend(chunks)
|
| 95 |
+
except Exception as e:
|
| 96 |
+
st.write(f"Error processing link {link}: {e}")
|
| 97 |
|
| 98 |
if all_chunks:
|
| 99 |
# Generate embeddings and store in FAISS
|