Spaces:
Sleeping
Sleeping
import os | |
import streamlit as st | |
import pickle | |
import time | |
from langchain.chains import RetrievalQA | |
from langchain.text_splitter import RecursiveCharacterTextSplitter | |
from langchain.document_loaders import UnstructuredURLLoader | |
#from langchain.vectorstores import FAISS | |
from langchain_community.vectorstores import FAISS | |
from langchain_huggingface import HuggingFaceEndpoint | |
from sentence_transformers import SentenceTransformer | |
from langchain.embeddings import HuggingFaceEmbeddings | |
#from langchain import HuggingFaceHub | |
from langchain_community.llms import HuggingFaceHub | |
from dotenv import load_dotenv | |
with st.sidebar: | |
st.image("sriram.jpg", caption="Say cheese :)", use_container_width=True) | |
load_dotenv() | |
st.title("Sriram’s Q Reflections 🔎") | |
#st.sidebar.title("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_index.pkl" | |
chunk_path="chunks.pkl" | |
placeholder=st.empty() | |
temp=st.empty() | |
query=placeholder.text_input("Search for a Memory :") | |
submit=st.button("Recall it") | |
if query: | |
temp.text("Searching for memories..!") | |
if os.path.exists(file_path): | |
with open(file_path,'rb') as f: | |
index=pickle.load(f) | |
with open(chunk_path,'rb') as f: | |
chunks=pickle.load(f) | |
model = SentenceTransformer("thenlper/gte-large")#'sentence-transformers/paraphrase-MiniLM-L12-v2') | |
temp.text("Searching for memories..!") | |
query_embedding = model.encode(query).astype('float32').reshape(1, -1) # Encode the query | |
k = 6 # Number of nearest neighbors to retrieve | |
distances, indices = index.search(query_embedding, k) | |
retrieved_chunks = [chunks[i] for i in indices[0]] | |
# # Use a prompt to generate a response with your language model | |
# input_prompt = f"""Given the question and | |
# context, Understand the question and give answer based on the context passed. | |
# Question: {query}\nContext: {context}\n Answer: """ | |
# response = llm.invoke(input_prompt) # Replace with your LLM call | |
# text=response | |
if query or submit: | |
st.header("Q Memories :") | |
temp.text("Memories retrieved..!") | |
cleaned_text_list = [] | |
for item in retrieved_chunks: | |
item = item.strip() | |
item = item.replace(" .", ".").replace("\n", " ") | |
item = item.lstrip(". ").strip() | |
item+="." | |
cleaned_text_list.append(item) | |
temp.empty() | |
for item in cleaned_text_list: | |
st.write(item) | |
# start_index = text.find("\nHelpful Answer:") | |
# # Extract everything after "\nHelpful Answer:" if it exists | |
# if start_index != -1: | |
# parsed_text =text[start_index + len("\nHelpful Answer:"):] | |
# parsed_text = parsed_text.strip() # Optionally strip any extra whitespace | |
# if query or submit: | |
# st.header("Answer :") | |
# st.write(parsed_text) | |
st.markdown(""" | |
<hr style="margin-top: 2em;"> | |
<p style="text-align: center; color: gray; font-size: small;"> | |
Developed by Aditya Hariharan | |
</p> | |
""", unsafe_allow_html=True) |