Spaces:
Sleeping
Sleeping
Update Utils/handle_file.py
Browse files- Utils/handle_file.py +76 -76
Utils/handle_file.py
CHANGED
|
@@ -1,76 +1,76 @@
|
|
| 1 |
-
import os
|
| 2 |
-
import mimetypes
|
| 3 |
-
from langchain_core.messages import HumanMessage, SystemMessage
|
| 4 |
-
from pathlib import Path
|
| 5 |
-
from
|
| 6 |
-
|
| 7 |
-
# Function to handle attachments based on file type
|
| 8 |
-
def handle_attachment(task_id, file_name):
|
| 9 |
-
"""
|
| 10 |
-
Handles file attachments for a task.
|
| 11 |
-
|
| 12 |
-
Args:
|
| 13 |
-
task_id: The ID of the task
|
| 14 |
-
file_name: The name of the attachment file
|
| 15 |
-
|
| 16 |
-
Returns:
|
| 17 |
-
Dictionary with attachment details, including how it should be handled
|
| 18 |
-
"""
|
| 19 |
-
if not file_name or file_name.strip() == "":
|
| 20 |
-
return {"status": "no_attachment"}
|
| 21 |
-
|
| 22 |
-
# Get file content using existing function
|
| 23 |
-
file_data = get_files_by_task_id(task_id)
|
| 24 |
-
|
| 25 |
-
if file_data["status"] == "error":
|
| 26 |
-
print(f"Error fetching attachment: {file_data.get('error')}")
|
| 27 |
-
return {"status": "error", "message": file_data.get("error")}
|
| 28 |
-
|
| 29 |
-
content_type = file_data["content_type"]
|
| 30 |
-
raw_content = file_data["content"]
|
| 31 |
-
|
| 32 |
-
# Determine if this file type can be directly processed by Claude
|
| 33 |
-
# Images, spreadsheets, and PDFs can be processed directly
|
| 34 |
-
direct_process = (
|
| 35 |
-
content_type.startswith("image/") or
|
| 36 |
-
"spreadsheet" in content_type or
|
| 37 |
-
"excel" in content_type or
|
| 38 |
-
content_type == "application/pdf" or
|
| 39 |
-
"csv" in content_type
|
| 40 |
-
)
|
| 41 |
-
|
| 42 |
-
if direct_process:
|
| 43 |
-
# For files that Claude can handle directly, return content for direct inclusion
|
| 44 |
-
print(f"Attachment {file_name} (type: {content_type}) will be passed directly to Claude")
|
| 45 |
-
return {
|
| 46 |
-
"status": "success",
|
| 47 |
-
"handling": "direct",
|
| 48 |
-
"content_type": content_type,
|
| 49 |
-
"raw_content": raw_content,
|
| 50 |
-
"file_name": file_name
|
| 51 |
-
}
|
| 52 |
-
else:
|
| 53 |
-
# For other files, save to disk and return path
|
| 54 |
-
save_dir = os.path.join(os.getcwd(), "task_files")
|
| 55 |
-
os.makedirs(save_dir, exist_ok=True)
|
| 56 |
-
|
| 57 |
-
# Determine file extension
|
| 58 |
-
file_extension = Path(file_name).suffix
|
| 59 |
-
if not file_extension:
|
| 60 |
-
extension = mimetypes.guess_extension(content_type) or ".bin"
|
| 61 |
-
file_path = os.path.join(save_dir, f"{task_id}{extension}")
|
| 62 |
-
else:
|
| 63 |
-
file_path = os.path.join(save_dir, file_name)
|
| 64 |
-
|
| 65 |
-
# Save the file
|
| 66 |
-
with open(file_path, 'wb') as f:
|
| 67 |
-
f.write(raw_content)
|
| 68 |
-
|
| 69 |
-
print(f"Attachment {file_name} (type: {content_type}) saved to {file_path}")
|
| 70 |
-
return {
|
| 71 |
-
"status": "success",
|
| 72 |
-
"handling": "tool",
|
| 73 |
-
"content_type": content_type,
|
| 74 |
-
"file_path": file_path,
|
| 75 |
-
"file_name": file_name
|
| 76 |
-
}
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import mimetypes
|
| 3 |
+
from langchain_core.messages import HumanMessage, SystemMessage
|
| 4 |
+
from pathlib import Path
|
| 5 |
+
from fetch_question import get_files_by_task_id
|
| 6 |
+
|
| 7 |
+
# Function to handle attachments based on file type
|
| 8 |
+
def handle_attachment(task_id, file_name):
|
| 9 |
+
"""
|
| 10 |
+
Handles file attachments for a task.
|
| 11 |
+
|
| 12 |
+
Args:
|
| 13 |
+
task_id: The ID of the task
|
| 14 |
+
file_name: The name of the attachment file
|
| 15 |
+
|
| 16 |
+
Returns:
|
| 17 |
+
Dictionary with attachment details, including how it should be handled
|
| 18 |
+
"""
|
| 19 |
+
if not file_name or file_name.strip() == "":
|
| 20 |
+
return {"status": "no_attachment"}
|
| 21 |
+
|
| 22 |
+
# Get file content using existing function
|
| 23 |
+
file_data = get_files_by_task_id(task_id)
|
| 24 |
+
|
| 25 |
+
if file_data["status"] == "error":
|
| 26 |
+
print(f"Error fetching attachment: {file_data.get('error')}")
|
| 27 |
+
return {"status": "error", "message": file_data.get("error")}
|
| 28 |
+
|
| 29 |
+
content_type = file_data["content_type"]
|
| 30 |
+
raw_content = file_data["content"]
|
| 31 |
+
|
| 32 |
+
# Determine if this file type can be directly processed by Claude
|
| 33 |
+
# Images, spreadsheets, and PDFs can be processed directly
|
| 34 |
+
direct_process = (
|
| 35 |
+
content_type.startswith("image/") or
|
| 36 |
+
"spreadsheet" in content_type or
|
| 37 |
+
"excel" in content_type or
|
| 38 |
+
content_type == "application/pdf" or
|
| 39 |
+
"csv" in content_type
|
| 40 |
+
)
|
| 41 |
+
|
| 42 |
+
if direct_process:
|
| 43 |
+
# For files that Claude can handle directly, return content for direct inclusion
|
| 44 |
+
print(f"Attachment {file_name} (type: {content_type}) will be passed directly to Claude")
|
| 45 |
+
return {
|
| 46 |
+
"status": "success",
|
| 47 |
+
"handling": "direct",
|
| 48 |
+
"content_type": content_type,
|
| 49 |
+
"raw_content": raw_content,
|
| 50 |
+
"file_name": file_name
|
| 51 |
+
}
|
| 52 |
+
else:
|
| 53 |
+
# For other files, save to disk and return path
|
| 54 |
+
save_dir = os.path.join(os.getcwd(), "task_files")
|
| 55 |
+
os.makedirs(save_dir, exist_ok=True)
|
| 56 |
+
|
| 57 |
+
# Determine file extension
|
| 58 |
+
file_extension = Path(file_name).suffix
|
| 59 |
+
if not file_extension:
|
| 60 |
+
extension = mimetypes.guess_extension(content_type) or ".bin"
|
| 61 |
+
file_path = os.path.join(save_dir, f"{task_id}{extension}")
|
| 62 |
+
else:
|
| 63 |
+
file_path = os.path.join(save_dir, file_name)
|
| 64 |
+
|
| 65 |
+
# Save the file
|
| 66 |
+
with open(file_path, 'wb') as f:
|
| 67 |
+
f.write(raw_content)
|
| 68 |
+
|
| 69 |
+
print(f"Attachment {file_name} (type: {content_type}) saved to {file_path}")
|
| 70 |
+
return {
|
| 71 |
+
"status": "success",
|
| 72 |
+
"handling": "tool",
|
| 73 |
+
"content_type": content_type,
|
| 74 |
+
"file_path": file_path,
|
| 75 |
+
"file_name": file_name
|
| 76 |
+
}
|