AdityaTheDev commited on
Commit
0b216b4
·
verified ·
1 Parent(s): 4ad1d55

Upload sri_app.py

Browse files
Files changed (1) hide show
  1. sri_app.py +90 -0
sri_app.py ADDED
@@ -0,0 +1,90 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import streamlit as st
3
+ import pickle
4
+ import time
5
+ from langchain.chains import RetrievalQA
6
+ from langchain.text_splitter import RecursiveCharacterTextSplitter
7
+ from langchain.document_loaders import UnstructuredURLLoader
8
+ #from langchain.vectorstores import FAISS
9
+ from langchain_community.vectorstores import FAISS
10
+ from langchain_huggingface import HuggingFaceEndpoint
11
+ from sentence_transformers import SentenceTransformer
12
+ from langchain.embeddings import HuggingFaceEmbeddings
13
+ #from langchain import HuggingFaceHub
14
+ from langchain_community.llms import HuggingFaceHub
15
+ from dotenv import load_dotenv
16
+
17
+
18
+
19
+ with st.sidebar:
20
+ st.image("sriram.jpg", caption="Say cheese :)", use_column_width=True)
21
+
22
+
23
+ load_dotenv()
24
+
25
+ st.title("Sriram’s Q Reflections 🔎")
26
+ #st.sidebar.title("Article URLs")
27
+
28
+
29
+ # urls=[]
30
+ # for i in range(3):
31
+ # url=st.sidebar.text_input(f"URL {i+1}")
32
+ # urls.append(url)
33
+ # process_url_clicked=st.sidebar.button("Process URLs")
34
+
35
+
36
+ file_path="faiss_index.pkl"
37
+ chunk_path="chunks.pkl"
38
+ placeholder=st.empty()
39
+ temp=st.empty()
40
+ query=placeholder.text_input("Search for a Memory :")
41
+ submit=st.button("Recall it")
42
+ if query:
43
+ temp.text("Searching for memories..!")
44
+ if os.path.exists(file_path):
45
+ with open(file_path,'rb') as f:
46
+ index=pickle.load(f)
47
+ with open(chunk_path,'rb') as f:
48
+ chunks=pickle.load(f)
49
+ model = SentenceTransformer("thenlper/gte-large")#'sentence-transformers/paraphrase-MiniLM-L12-v2')
50
+ temp.text("Searching for memories..!")
51
+ query_embedding = model.encode(query).astype('float32').reshape(1, -1) # Encode the query
52
+ k = 6 # Number of nearest neighbors to retrieve
53
+ distances, indices = index.search(query_embedding, k)
54
+ retrieved_chunks = [chunks[i] for i in indices[0]]
55
+ # # Use a prompt to generate a response with your language model
56
+ # input_prompt = f"""Given the question and
57
+ # context, Understand the question and give answer based on the context passed.
58
+ # Question: {query}\nContext: {context}\n Answer: """
59
+ # response = llm.invoke(input_prompt) # Replace with your LLM call
60
+ # text=response
61
+ if query or submit:
62
+
63
+ st.header("Q Memories :")
64
+ temp.text("Memories retrieved..!")
65
+ cleaned_text_list = []
66
+ for item in retrieved_chunks:
67
+ item = item.strip()
68
+ item = item.replace(" .", ".").replace("\n", " ")
69
+ item = item.lstrip(". ").strip()
70
+ item+="."
71
+ cleaned_text_list.append(item)
72
+ temp.empty()
73
+ for item in cleaned_text_list:
74
+ st.write(item)
75
+ # start_index = text.find("\nHelpful Answer:")
76
+
77
+ # # Extract everything after "\nHelpful Answer:" if it exists
78
+ # if start_index != -1:
79
+ # parsed_text =text[start_index + len("\nHelpful Answer:"):]
80
+ # parsed_text = parsed_text.strip() # Optionally strip any extra whitespace
81
+ # if query or submit:
82
+ # st.header("Answer :")
83
+ # st.write(parsed_text)
84
+
85
+ st.markdown("""
86
+ <hr style="margin-top: 2em;">
87
+ <p style="text-align: center; color: gray; font-size: small;">
88
+ Developed by Aditya Hariharan
89
+ </p>
90
+ """, unsafe_allow_html=True)