Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -41,33 +41,53 @@ def init_llm():
|
|
41 |
)
|
42 |
|
43 |
|
|
|
|
|
44 |
def process_document(file):
|
45 |
-
"""Process uploaded PDF and create a retriever"""
|
46 |
global conversation_retrieval_chain
|
47 |
|
48 |
if not llm_pipeline or not embeddings:
|
49 |
init_llm()
|
50 |
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
69 |
|
70 |
-
return "π PDF uploaded and processed successfully! You can now ask questions."
|
71 |
|
72 |
|
73 |
def process_prompt(prompt, chat_history_display):
|
|
|
41 |
)
|
42 |
|
43 |
|
44 |
+
import time
|
45 |
+
|
46 |
def process_document(file):
|
|
|
47 |
global conversation_retrieval_chain
|
48 |
|
49 |
if not llm_pipeline or not embeddings:
|
50 |
init_llm()
|
51 |
|
52 |
+
start_time = time.time()
|
53 |
+
print(f"π Uploading PDF: {file.name}")
|
54 |
+
|
55 |
+
try:
|
56 |
+
# β
Ensure file is saved correctly
|
57 |
+
file_path = os.path.join("/tmp/uploads", file.name)
|
58 |
+
with open(file_path, "wb") as f:
|
59 |
+
f.write(file.read())
|
60 |
+
print(f"β
PDF saved at {file_path} in {time.time() - start_time:.2f}s")
|
61 |
+
|
62 |
+
# β
Load PDF
|
63 |
+
start_time = time.time()
|
64 |
+
loader = PyPDFLoader(file_path)
|
65 |
+
documents = loader.load()
|
66 |
+
print(f"β
PDF loaded in {time.time() - start_time:.2f}s")
|
67 |
+
|
68 |
+
# β
Split text
|
69 |
+
start_time = time.time()
|
70 |
+
text_splitter = RecursiveCharacterTextSplitter(chunk_size=512, chunk_overlap=50)
|
71 |
+
texts = text_splitter.split_documents(documents)
|
72 |
+
print(f"β
Text split in {time.time() - start_time:.2f}s")
|
73 |
+
|
74 |
+
# β
Create ChromaDB
|
75 |
+
start_time = time.time()
|
76 |
+
db = Chroma.from_documents(texts, embedding=embeddings, persist_directory="/tmp/chroma_db")
|
77 |
+
print(f"β
ChromaDB created in {time.time() - start_time:.2f}s")
|
78 |
+
|
79 |
+
# β
Create retrieval chain
|
80 |
+
conversation_retrieval_chain = ConversationalRetrievalChain.from_llm(
|
81 |
+
llm=llm_pipeline, retriever=db.as_retriever()
|
82 |
+
)
|
83 |
+
print("β
Document processing complete!")
|
84 |
+
|
85 |
+
return "π PDF uploaded and processed successfully! You can now ask questions."
|
86 |
+
|
87 |
+
except Exception as e:
|
88 |
+
print(f"β Error processing PDF: {str(e)}")
|
89 |
+
return f"Error: {str(e)}"
|
90 |
|
|
|
91 |
|
92 |
|
93 |
def process_prompt(prompt, chat_history_display):
|