Spaces:
Paused
Paused
Commit
·
cc60679
1
Parent(s):
0159ca5
file loading testing
Browse files- helper_functions.py +24 -11
helper_functions.py
CHANGED
@@ -9,18 +9,31 @@ import os
|
|
9 |
import functools
|
10 |
import requests
|
11 |
|
12 |
-
def process_file(
|
13 |
-
#
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
21 |
else:
|
22 |
-
|
23 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
24 |
|
25 |
def load_documents_from_url(url):
|
26 |
try:
|
|
|
9 |
import functools
|
10 |
import requests
|
11 |
|
12 |
+
def process_file(uploaded_file):
|
13 |
+
# Save the file temporarily to process it
|
14 |
+
temp_file_path = f"/tmp/{uploaded_file.name}"
|
15 |
+
|
16 |
+
with open(temp_file_path, "wb") as temp_file:
|
17 |
+
# Write the uploaded file content to the temporary file
|
18 |
+
temp_file.write(uploaded_file.read())
|
19 |
+
|
20 |
+
# Determine the file type and load it accordingly
|
21 |
+
if uploaded_file.name.endswith(".pdf"):
|
22 |
+
# Load PDF with PyMuPDFLoader
|
23 |
+
loader = PyMuPDFLoader(temp_file_path)
|
24 |
+
elif uploaded_file.name.endswith(".txt"):
|
25 |
+
# Load text file with TextLoader
|
26 |
+
loader = TextLoader(temp_file_path)
|
27 |
else:
|
28 |
+
raise ValueError("Unsupported file format. Only PDF and TXT are supported.")
|
29 |
+
|
30 |
+
# Load documents from the file
|
31 |
+
documents = loader.load()
|
32 |
+
|
33 |
+
# Clean up the temporary file
|
34 |
+
os.remove(temp_file_path)
|
35 |
+
|
36 |
+
return documents
|
37 |
|
38 |
def load_documents_from_url(url):
|
39 |
try:
|