Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -7,6 +7,8 @@ from langchain_core.runnables import RunnablePassthrough
|
|
| 7 |
from langchain_text_splitters import RecursiveCharacterTextSplitter
|
| 8 |
from sentence_transformers import SentenceTransformer
|
| 9 |
import torch
|
|
|
|
|
|
|
| 10 |
|
| 11 |
|
| 12 |
# Define the embedding class
|
|
@@ -35,11 +37,15 @@ langchain_api_key = "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"
|
|
| 35 |
|
| 36 |
def load_document(document_path):
|
| 37 |
try:
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
except Exception as e:
|
| 44 |
return str(e)
|
| 45 |
|
|
@@ -85,4 +91,6 @@ if document_path is not None and query:
|
|
| 85 |
result = answer_question(rag_chain, query)
|
| 86 |
st.write("Result:", result)
|
| 87 |
|
|
|
|
|
|
|
| 88 |
st.write("Note: Replace `llm` with an appropriate language model.")
|
|
|
|
| 7 |
from langchain_text_splitters import RecursiveCharacterTextSplitter
|
| 8 |
from sentence_transformers import SentenceTransformer
|
| 9 |
import torch
|
| 10 |
+
import os
|
| 11 |
+
import tempfile
|
| 12 |
|
| 13 |
|
| 14 |
# Define the embedding class
|
|
|
|
| 37 |
|
| 38 |
def load_document(document_path):
|
| 39 |
try:
|
| 40 |
+
with tempfile.TemporaryDirectory() as tmp_dir:
|
| 41 |
+
tmp_file = os.path.join(tmp_dir, 'temp.pdf')
|
| 42 |
+
with open(tmp_file, 'wb') as f:
|
| 43 |
+
f.write(document_path.getvalue())
|
| 44 |
+
loader = PyPDFLoader(tmp_file)
|
| 45 |
+
docs = loader.load()
|
| 46 |
+
text_splitter = RecursiveCharacterTextSplitter(chunk_size=4000, chunk_overlap=200)
|
| 47 |
+
splits = text_splitter.split_documents(docs)
|
| 48 |
+
return splits
|
| 49 |
except Exception as e:
|
| 50 |
return str(e)
|
| 51 |
|
|
|
|
| 91 |
result = answer_question(rag_chain, query)
|
| 92 |
st.write("Result:", result)
|
| 93 |
|
| 94 |
+
|
| 95 |
+
|
| 96 |
st.write("Note: Replace `llm` with an appropriate language model.")
|