Update app.py
Browse files
app.py
CHANGED
|
@@ -1,103 +1,91 @@
|
|
| 1 |
-
#
|
|
|
|
| 2 |
|
| 3 |
-
|
| 4 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
import os
|
| 6 |
-
|
| 7 |
|
| 8 |
-
#
|
| 9 |
-
|
| 10 |
-
|
| 11 |
|
| 12 |
-
|
|
|
|
| 13 |
file_id = link.split('/d/')[1].split('/')[0]
|
| 14 |
-
download_url = f"https://drive.google.com/uc?id={file_id}
|
| 15 |
response = requests.get(download_url)
|
| 16 |
if response.status_code == 200:
|
| 17 |
-
|
| 18 |
-
with open(file_path, 'wb') as f:
|
| 19 |
-
f.write(response.content)
|
| 20 |
-
print(f"PDF downloaded successfully: {file_path}")
|
| 21 |
-
return file_path
|
| 22 |
else:
|
| 23 |
-
raise Exception("Failed to download file.
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
return
|
| 41 |
-
|
| 42 |
-
#
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
def create_faiss_index(embeddings):
|
| 54 |
-
dimension = len(embeddings[0])
|
| 55 |
-
index = faiss.IndexFlatL2(dimension)
|
| 56 |
-
index.add(np.array(embeddings))
|
| 57 |
-
faiss.write_index(index, "faiss_index.index")
|
| 58 |
-
print("Embeddings stored in FAISS.")
|
| 59 |
-
|
| 60 |
-
# Query the Groq model
|
| 61 |
-
from groq import Groq
|
| 62 |
-
|
| 63 |
-
client = Groq(api_key=os.environ.get("GROQ_API_KEY"))
|
| 64 |
-
|
| 65 |
-
def query_model(prompt):
|
| 66 |
-
response = client.chat.completions.create(
|
| 67 |
-
messages=[{"role": "user", "content": prompt}],
|
| 68 |
-
model="llama3-8b-8192"
|
| 69 |
)
|
| 70 |
-
return
|
| 71 |
-
|
| 72 |
-
#
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
if
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
create_faiss_index(embeddings)
|
| 93 |
-
all_chunks.extend(text_chunks)
|
| 94 |
-
else:
|
| 95 |
-
st.error("Failed to extract text from the document.")
|
| 96 |
-
except Exception as e:
|
| 97 |
-
st.error(f"Error processing document: {e}")
|
| 98 |
-
|
| 99 |
-
if all_chunks:
|
| 100 |
-
result = query_model(query)
|
| 101 |
-
st.write(result)
|
| 102 |
else:
|
| 103 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Required module installations (uncomment and run in your environment if needed)
|
| 2 |
+
# !pip install requests PyPDF2 langchain faiss-cpu streamlit groq sentence-transformers
|
| 3 |
|
| 4 |
+
import requests
|
| 5 |
+
import io
|
| 6 |
+
import PyPDF2
|
| 7 |
+
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
| 8 |
+
from langchain.embeddings import HuggingFaceEmbeddings # Open-source embedding model
|
| 9 |
+
from langchain.vectorstores import FAISS
|
| 10 |
+
from groq import Groq
|
| 11 |
import os
|
| 12 |
+
import streamlit as st
|
| 13 |
|
| 14 |
+
# Set up Groq API
|
| 15 |
+
os.environ["GROQ_API_KEY"] = "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" # Replace with your Groq API key
|
| 16 |
+
client = Groq(api_key=os.environ.get("GROQ_API_KEY"))
|
| 17 |
|
| 18 |
+
# Function to download PDF from Google Drive link
|
| 19 |
+
def download_pdf_from_link(link):
|
| 20 |
file_id = link.split('/d/')[1].split('/')[0]
|
| 21 |
+
download_url = f"https://drive.google.com/uc?export=download&id={file_id}"
|
| 22 |
response = requests.get(download_url)
|
| 23 |
if response.status_code == 200:
|
| 24 |
+
return response.content
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
else:
|
| 26 |
+
raise Exception("Failed to download file. Check the link.")
|
| 27 |
+
|
| 28 |
+
# Function to extract text from PDF
|
| 29 |
+
def read_pdf(pdf_content):
|
| 30 |
+
file_io = io.BytesIO(pdf_content)
|
| 31 |
+
pdf_reader = PyPDF2.PdfReader(file_io)
|
| 32 |
+
text = ""
|
| 33 |
+
for page in pdf_reader.pages:
|
| 34 |
+
text += page.extract_text()
|
| 35 |
+
return text
|
| 36 |
+
|
| 37 |
+
# Function to create chunks of text
|
| 38 |
+
def create_chunks(documents):
|
| 39 |
+
text_splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=200)
|
| 40 |
+
chunks = []
|
| 41 |
+
for doc in documents:
|
| 42 |
+
chunks.extend(text_splitter.split_text(doc))
|
| 43 |
+
return chunks
|
| 44 |
+
|
| 45 |
+
# Function to query the Groq API with vectorstore
|
| 46 |
+
def query_with_groq(query, vectorstore):
|
| 47 |
+
docs = vectorstore.similarity_search(query, k=3)
|
| 48 |
+
context = " ".join([doc.page_content for doc in docs])
|
| 49 |
+
|
| 50 |
+
chat_completion = client.chat.completions.create(
|
| 51 |
+
messages=[
|
| 52 |
+
{"role": "system", "content": "You are a helpful assistant."},
|
| 53 |
+
{"role": "user", "content": f"{context}\n\n{query}"}
|
| 54 |
+
],
|
| 55 |
+
model="llama3-8b-8192",
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
)
|
| 57 |
+
return chat_completion.choices[0].message.content
|
| 58 |
+
|
| 59 |
+
# Main function to initialize the app
|
| 60 |
+
def main():
|
| 61 |
+
st.title("RAG Application with Google Drive Links")
|
| 62 |
+
|
| 63 |
+
# Input links (replace these with your document links)
|
| 64 |
+
links = [
|
| 65 |
+
"https://drive.google.com/file/d/1zoo4-GNIGPtbT_Yb4nIZw-qYf8Wj57nP/view?usp=sharing"
|
| 66 |
+
# Add more links here if needed
|
| 67 |
+
]
|
| 68 |
+
|
| 69 |
+
# Load or process documents
|
| 70 |
+
if "vectorstore" not in st.session_state:
|
| 71 |
+
documents = [read_pdf(download_pdf_from_link(link)) for link in links]
|
| 72 |
+
chunks = create_chunks(documents)
|
| 73 |
+
|
| 74 |
+
# Generate embeddings and store in FAISS
|
| 75 |
+
embeddings = HuggingFaceEmbeddings(model_name="sentence-transformers/all-MiniLM-L6-v2")
|
| 76 |
+
vectorstore = FAISS.from_texts(chunks, embeddings)
|
| 77 |
+
vectorstore.save_local("faiss_index")
|
| 78 |
+
st.session_state.vectorstore = vectorstore
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 79 |
else:
|
| 80 |
+
vectorstore = st.session_state.vectorstore
|
| 81 |
+
|
| 82 |
+
# Query input from user
|
| 83 |
+
query = st.text_input("Enter your query:")
|
| 84 |
+
if query:
|
| 85 |
+
response = query_with_groq(query, vectorstore)
|
| 86 |
+
st.write("Response:")
|
| 87 |
+
st.write(response)
|
| 88 |
+
|
| 89 |
+
# Run the app
|
| 90 |
+
if __name__ == "__main__":
|
| 91 |
+
main()
|