research-tool / app.py
Deaksh's picture
Update app.py
cd921da verified
raw
history blame
4.11 kB
import os
import streamlit as st
import pickle
import time
from langchain_community.embeddings import HuggingFaceEmbeddings
from langchain.chains import RetrievalQAWithSourcesChain
from langchain.text_splitter import RecursiveCharacterTextSplitter
from langchain.document_loaders import UnstructuredURLLoader
from langchain_groq import ChatGroq
from langchain.vectorstores import FAISS
import numpy as np
from dotenv import load_dotenv
load_dotenv() # take environment variables from .env (especially openai api key)
st.title("RockyBot: News Research Tool πŸ“ˆ")
st.sidebar.title("News Article URLs")
urls = []
for i in range(3):
url = st.sidebar.text_input(f"URL {i+1}")
urls.append(url)
process_url_clicked = st.sidebar.button("Process URLs")
file_path = "faiss_store_openai.pkl"
main_placeholder = st.empty()
llm = ChatGroq(model_name="llama-3.3-70b-versatile", temperature=0.9, max_tokens=500)
if process_url_clicked:
# Load data from URLs
loader = UnstructuredURLLoader(urls=urls)
main_placeholder.text("Data Loading...Started...βœ…βœ…βœ…")
data = loader.load()
# Split data into chunks
text_splitter = RecursiveCharacterTextSplitter(
separators=['\n\n', '\n', '.', ','],
chunk_size=1000
)
main_placeholder.text("Text Splitter...Started...βœ…βœ…βœ…")
docs = text_splitter.split_documents(data)
# Debugging: Check if docs is empty
if not docs:
main_placeholder.text("No valid documents found! Please check the URLs.")
# Debugging: Check the content of docs
for doc in docs:
main_placeholder.text(f"Document content: {doc.page_content[:200]}") # Show first 200 chars of each document
# Create embeddings using HuggingFaceEmbeddings
embedding_model = HuggingFaceEmbeddings(model_name="sentence-transformers/all-MiniLM-L6-v2")
main_placeholder.text("Embedding Vector Started Building...βœ…βœ…βœ…")
# Generate embeddings
embeddings = embedding_model.embed_documents([doc.page_content for doc in docs])
# Debugging: Check if embeddings are generated
if not embeddings:
main_placeholder.text("No embeddings were generated! Check the embedding model or document content.")
# Check the size of embeddings
main_placeholder.text(f"Generated {len(embeddings)} embeddings.")
# Convert embeddings to numpy array (needed by FAISS)
embeddings_np = np.array(embeddings).astype(np.float32)
# Check the shape of the embeddings
main_placeholder.text(f"Shape of embeddings: {embeddings_np.shape}")
# Create FAISS index
if len(embeddings) > 0:
dimension = len(embeddings[0]) # Embedding vector dimension
index = FAISS(dimension)
index.add(embeddings_np) # Add embeddings to FAISS index
# Wrap FAISS index using LangChain FAISS wrapper
vectorstore_huggingface = FAISS(embedding_function=embedding_model, index=index)
# Save the FAISS index to a pickle file
with open(file_path, "wb") as f:
pickle.dump(vectorstore_huggingface, f)
time.sleep(2)
else:
main_placeholder.text("Embeddings could not be generated, skipping FAISS index creation.")
query = main_placeholder.text_input("Question: ")
if query:
if os.path.exists(file_path):
# Load the FAISS index from the pickle file
with open(file_path, "rb") as f:
vectorstore = pickle.load(f)
chain = RetrievalQAWithSourcesChain.from_llm(llm=llm, retriever=vectorstore.as_retriever())
result = chain({"question": query}, return_only_outputs=True)
# Display the answer
st.header("Answer")
st.write(result["answer"])
# Display sources, if available
sources = result.get("sources", "")
if sources:
st.subheader("Sources:")
sources_list = sources.split("\n") # Split the sources by newline
for source in sources_list:
st.write(source)