Spaces:
Sleeping
Sleeping
KushwanthK
commited on
Upload app.py
Browse files
app.py
CHANGED
@@ -52,6 +52,54 @@ if "faq" not in st.session_state:
|
|
52 |
|
53 |
st.divider()
|
54 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
55 |
def highlight_pdf(file_path, text_to_highlight, page_numbers):
|
56 |
# Create a temporary file to save the modified PDF
|
57 |
# temp_pdf_path = "temp_highlighted_pdf.pdf"
|
@@ -150,8 +198,8 @@ def get_faiss_semantic_index():
|
|
150 |
except Exception as e:
|
151 |
st.error(f"Error loading embeddings: {e}")
|
152 |
return None
|
153 |
-
faiss_index = get_faiss_semantic_index()
|
154 |
-
print(faiss_index)
|
155 |
|
156 |
# def promt_engineer(text):
|
157 |
PROMPT_TEMPLATE = """
|
|
|
52 |
|
53 |
st.divider()
|
54 |
|
55 |
+
# def upload_file():
|
56 |
+
# uploaded_file = st.file_uploader("Upload a file")
|
57 |
+
# if uploaded_file is not None:
|
58 |
+
# return uploaded_file.read()
|
59 |
+
|
60 |
+
def create_pickle_file(filepath):
|
61 |
+
|
62 |
+
from langchain_community.document_loaders import PyMuPDFLoader
|
63 |
+
loader = PyMuPDFLoader(filepath)
|
64 |
+
pages = loader.load()
|
65 |
+
|
66 |
+
# Load a pre-trained sentence transformer model
|
67 |
+
model_name = "sentence-transformers/all-mpnet-base-v2"
|
68 |
+
model_kwargs = {'device': 'cpu'}
|
69 |
+
encode_kwargs = {'normalize_embeddings': False}
|
70 |
+
|
71 |
+
# Create a HuggingFaceEmbeddings object
|
72 |
+
from langchain_community.embeddings import HuggingFaceEmbeddings
|
73 |
+
embeddings = HuggingFaceEmbeddings(model_name=model_name, model_kwargs=model_kwargs, encode_kwargs=encode_kwargs)
|
74 |
+
|
75 |
+
from pathlib import Path
|
76 |
+
|
77 |
+
path = Path(filepath)
|
78 |
+
|
79 |
+
filename = path.name
|
80 |
+
|
81 |
+
print(filename)
|
82 |
+
|
83 |
+
from datetime import datetime
|
84 |
+
|
85 |
+
# Get current date and time
|
86 |
+
now = datetime.now()
|
87 |
+
|
88 |
+
# Format as string with milliseconds
|
89 |
+
formatted_datetime = now.strftime("%Y-%m-%d_%H:%M:%S.%f")[:-3]
|
90 |
+
|
91 |
+
print(formatted_datetime)
|
92 |
+
|
93 |
+
# Create FAISS index with the HuggingFace embeddings
|
94 |
+
faiss_index = FAISS.from_documents(pages, embeddings)
|
95 |
+
with open(f"./{filename}_{formatted_datetime}.pkl", "wb") as f:
|
96 |
+
pickle.dump(faiss_index, f)
|
97 |
+
|
98 |
+
|
99 |
+
uploaded_file = st.file_uploader("Upload a file", type=["pdf"])
|
100 |
+
if uploaded_file is not None:
|
101 |
+
create_pickle_file(uploaded_file)
|
102 |
+
|
103 |
def highlight_pdf(file_path, text_to_highlight, page_numbers):
|
104 |
# Create a temporary file to save the modified PDF
|
105 |
# temp_pdf_path = "temp_highlighted_pdf.pdf"
|
|
|
198 |
except Exception as e:
|
199 |
st.error(f"Error loading embeddings: {e}")
|
200 |
return None
|
201 |
+
# faiss_index = get_faiss_semantic_index()
|
202 |
+
# print(faiss_index)
|
203 |
|
204 |
# def promt_engineer(text):
|
205 |
PROMPT_TEMPLATE = """
|