File size: 3,375 Bytes
a72e59e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e1dae82
a72e59e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0b216b4
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
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)