Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -0,0 +1,104 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import requests
|
3 |
+
from groq import Groq
|
4 |
+
from langchain_community.embeddings import HuggingFaceEmbeddings
|
5 |
+
from langchain_community.vectorstores import FAISS
|
6 |
+
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
7 |
+
from PyPDF2 import PdfReader
|
8 |
+
import streamlit as st
|
9 |
+
from tempfile import NamedTemporaryFile
|
10 |
+
|
11 |
+
# Initialize Groq client
|
12 |
+
client = Groq(api_key=os.environ['GROQ_API_KEY'])
|
13 |
+
|
14 |
+
# Function to extract text from a PDF
|
15 |
+
def extract_text_from_pdf(pdf_file_path):
|
16 |
+
pdf_reader = PdfReader(pdf_file_path)
|
17 |
+
text = ""
|
18 |
+
for page in pdf_reader.pages:
|
19 |
+
page_text = page.extract_text()
|
20 |
+
if page_text:
|
21 |
+
text += page_text
|
22 |
+
return text
|
23 |
+
|
24 |
+
# Function to split text into chunks
|
25 |
+
def chunk_text(text, chunk_size=500, chunk_overlap=50):
|
26 |
+
text_splitter = RecursiveCharacterTextSplitter(
|
27 |
+
chunk_size=chunk_size, chunk_overlap=chunk_overlap
|
28 |
+
)
|
29 |
+
return text_splitter.split_text(text)
|
30 |
+
|
31 |
+
# Function to create embeddings and store them in FAISS
|
32 |
+
def create_embeddings_and_store(chunks, vector_db=None):
|
33 |
+
embeddings = HuggingFaceEmbeddings(model_name="sentence-transformers/all-MiniLM-L6-v2")
|
34 |
+
if vector_db is None:
|
35 |
+
vector_db = FAISS.from_texts(chunks, embedding=embeddings)
|
36 |
+
else:
|
37 |
+
vector_db.add_texts(chunks)
|
38 |
+
return vector_db
|
39 |
+
|
40 |
+
# Function to query the vector database and interact with Groq
|
41 |
+
def query_vector_db(query, vector_db):
|
42 |
+
docs = vector_db.similarity_search(query, k=3)
|
43 |
+
context = "\n".join([doc.page_content for doc in docs])
|
44 |
+
chat_completion = client.chat.completions.create(
|
45 |
+
messages=[
|
46 |
+
{"role": "system", "content": f"Use the following context:\n{context}"},
|
47 |
+
{"role": "user", "content": query},
|
48 |
+
],
|
49 |
+
model="llama3-8b-8192",
|
50 |
+
)
|
51 |
+
return chat_completion.choices[0].message.content
|
52 |
+
|
53 |
+
# Function to convert Google Drive view link to downloadable link
|
54 |
+
def get_direct_download_link(view_url):
|
55 |
+
if "drive.google.com/file/d/" in view_url:
|
56 |
+
file_id = view_url.split("/file/d/")[1].split("/")[0]
|
57 |
+
return f"https://drive.google.com/uc?export=download&id={file_id}"
|
58 |
+
return None
|
59 |
+
|
60 |
+
# Function to download and save a PDF from a URL
|
61 |
+
def download_pdf_from_url(url):
|
62 |
+
direct_url = get_direct_download_link(url)
|
63 |
+
if not direct_url:
|
64 |
+
return None
|
65 |
+
response = requests.get(direct_url)
|
66 |
+
if response.status_code == 200:
|
67 |
+
temp_file = NamedTemporaryFile(delete=False, suffix=".pdf")
|
68 |
+
temp_file.write(response.content)
|
69 |
+
temp_file.close()
|
70 |
+
return temp_file.name
|
71 |
+
else:
|
72 |
+
return None
|
73 |
+
|
74 |
+
# Streamlit app
|
75 |
+
st.title("RAG-Based QA on Google Drive PDFs")
|
76 |
+
|
77 |
+
# Only fetch from provided links
|
78 |
+
doc_links = [
|
79 |
+
"https://drive.google.com/file/d/1YWX-RYxgtcKO1QETnz1N3rboZUhRZwcH/view?usp=sharing",
|
80 |
+
"https://drive.google.com/file/d/1JPf0XvDhn8QoDOlZDrxCOpu4WzKFESNz/view?usp=sharing",
|
81 |
+
]
|
82 |
+
|
83 |
+
vector_db = None
|
84 |
+
|
85 |
+
# Process Google Drive documents
|
86 |
+
for idx, link in enumerate(doc_links):
|
87 |
+
st.write(f"π Fetching and processing PDF from Link {idx + 1}...")
|
88 |
+
pdf_path = download_pdf_from_url(link)
|
89 |
+
if pdf_path:
|
90 |
+
text = extract_text_from_pdf(pdf_path)
|
91 |
+
chunks = chunk_text(text)
|
92 |
+
vector_db = create_embeddings_and_store(chunks, vector_db=vector_db)
|
93 |
+
st.success(f"β
Processed document {idx + 1}")
|
94 |
+
else:
|
95 |
+
st.error(f"β Failed to download or process PDF from Link {idx + 1}")
|
96 |
+
|
97 |
+
# User query input
|
98 |
+
user_query = st.text_input("π Enter your query:")
|
99 |
+
if user_query and vector_db:
|
100 |
+
response = query_vector_db(user_query, vector_db)
|
101 |
+
st.subheader("π¬ Response from LLM:")
|
102 |
+
st.write(response)
|
103 |
+
elif user_query:
|
104 |
+
st.warning("β οΈ No documents processed to query.")
|