|
import os |
|
import streamlit as st |
|
import pickle |
|
import time |
|
import langchain |
|
|
|
|
|
|
|
from langchain.text_splitter import RecursiveCharacterTextSplitter |
|
from langchain.document_loaders import UnstructuredURLLoader |
|
|
|
from langchain.vectorstores import FAISS |
|
import requests |
|
import pandas as pd |
|
from langchain_community.llms import HuggingFaceEndpoint |
|
from sentence_transformers import SentenceTransformer |
|
from langchain.document_loaders import TextLoader |
|
from sentence_transformers import SentenceTransformer, util |
|
from langchain.schema import SystemMessage, HumanMessage, AIMessage |
|
import faiss |
|
from dotenv import load_dotenv |
|
load_dotenv() |
|
|
|
def querypreprocess(query: str ): |
|
vec = model.encode(query) |
|
|
|
distances, I = index.search(vec, k=2) |
|
row_indices = I.tolist()[0] |
|
list1 = [docs[i].page_content for i in row_indices] |
|
str1 = " " |
|
str1 = str1.join(list1) |
|
|
|
|
|
return str1 |
|
|
|
def augmented_prompt(query: str): |
|
messages = querypreprocess(query) |
|
source_knowledge =''.join([str(message) for message in messages]) |
|
|
|
augmented_prompt = f""" |
|
using the contexts below, answer the query. |
|
|
|
Contexts: |
|
{source_knowledge} |
|
Question: {query} |
|
Answer:""" |
|
return augmented_prompt |
|
|
|
|
|
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 = "sentence_embeddings.pkl" |
|
|
|
main_placeholder = st.empty() |
|
|
|
llm = HuggingFaceEndpoint(repo_id="mistralai/Mistral-7B-Instruct-v0.2") |
|
|
|
if process_url_clicked: |
|
|
|
loader = UnstructuredURLLoader(urls=urls) |
|
main_placeholder.text("Data Loading...Started...β
β
β
") |
|
data = loader.load() |
|
|
|
text_splitter = RecursiveCharacterTextSplitter( |
|
separators=['\n\n', '\n', '.', ','], |
|
chunk_size=1000, |
|
chunk_overlap=0 |
|
) |
|
main_placeholder.text("Text Splitter...Started...β
β
β
") |
|
docs = text_splitter.split_documents(data) |
|
|
|
sentences = [] |
|
for i, row in enumerate(docs): |
|
sentences.append(row.page_content) |
|
|
|
|
|
|
|
|
|
model = SentenceTransformer('bert-base-nli-mean-tokens') |
|
|
|
sentence_embeddings = model.encode(sentences) |
|
main_placeholder.text("Embedding Vector Started Building...β
β
β
") |
|
time.sleep(2) |
|
|
|
|
|
with open(file_path, "wb") as f: |
|
pickle.dump(sentence_embeddings, f) |
|
|
|
query = main_placeholder.text_input("Question: ") |
|
if query: |
|
if os.path.exists(file_path): |
|
with open(file_path, "rb") as f: |
|
query = pickle.load(f) |
|
import faiss |
|
d = sentence_embeddings.shape[1] |
|
index = faiss.IndexFlatL2(d) |
|
|
|
index.add(sentence_embeddings) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
messages = [ |
|
SystemMessage(content="You are a helpful assistant."), |
|
HumanMessage(content=query), |
|
AIMessage(content="I am Great, Thank You, How Can I Help You.") |
|
] |
|
prompt = augmented_prompt(query) |
|
messages.append(prompt) |
|
result = llm.invoke(messages) |
|
st.header("Answer") |
|
|
|
sources = result.get("sources", "") |
|
if sources: |
|
st.subheader("Sources:") |
|
sources_list = sources.split("\n") |
|
for source in sources_list: |
|
st.write(source) |