Spaces:
Runtime error
Runtime error
Update main.py
Browse files
main.py
CHANGED
|
@@ -18,14 +18,28 @@ PDF_DIRECTORY = 'data'
|
|
| 18 |
# Chat history
|
| 19 |
chat_history = []
|
| 20 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
def load_data_from_directory():
|
| 22 |
"""Load raw text data from the directory."""
|
| 23 |
data = ""
|
| 24 |
for filename in os.listdir(PDF_DIRECTORY):
|
| 25 |
file_path = os.path.join(PDF_DIRECTORY, filename)
|
| 26 |
-
if os.path.isfile(file_path):
|
| 27 |
-
|
| 28 |
-
|
|
|
|
|
|
|
|
|
|
| 29 |
return data
|
| 30 |
|
| 31 |
# Load hotel data (context) from the directory
|
|
|
|
| 18 |
# Chat history
|
| 19 |
chat_history = []
|
| 20 |
|
| 21 |
+
def is_text_file(file_path):
|
| 22 |
+
"""Check if the file is a text file."""
|
| 23 |
+
try:
|
| 24 |
+
with open(file_path, 'r', encoding='utf-8') as file:
|
| 25 |
+
file.read(1024) # Read the first 1KB to check encoding
|
| 26 |
+
return True
|
| 27 |
+
except UnicodeDecodeError:
|
| 28 |
+
return False
|
| 29 |
+
except Exception:
|
| 30 |
+
return False
|
| 31 |
+
|
| 32 |
def load_data_from_directory():
|
| 33 |
"""Load raw text data from the directory."""
|
| 34 |
data = ""
|
| 35 |
for filename in os.listdir(PDF_DIRECTORY):
|
| 36 |
file_path = os.path.join(PDF_DIRECTORY, filename)
|
| 37 |
+
if os.path.isfile(file_path) and is_text_file(file_path):
|
| 38 |
+
try:
|
| 39 |
+
with open(file_path, 'r', encoding='utf-8') as file:
|
| 40 |
+
data += file.read() + "\n"
|
| 41 |
+
except Exception as e:
|
| 42 |
+
print(f"Error reading file {filename}: {str(e)}")
|
| 43 |
return data
|
| 44 |
|
| 45 |
# Load hotel data (context) from the directory
|