-
Notifications
You must be signed in to change notification settings - Fork 2.4k
fix: Add file name to prompt when processing images with doc #4114
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -44,7 +44,7 @@ def image_to_mode(image, doc: Document, images_list, get_image_id): | |
if len([i for i in images_list if i.id == image_uuid]) == 0: | ||
image = File(id=image_uuid, file_name=part.filename, meta={'debug': False, 'content': part.blob}) | ||
images_list.append(image) | ||
return f'' | ||
return f'![{part.filename.replace("[", "").replace("]", "")}](./oss/file/{image_uuid})' | ||
return None | ||
return None | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The provided function
Here's the optimized version of the function: if not isinstance(doc, Document) or not isinstance(images_list, list) or not callable(get_image_id):
raise ValueError("Invalid input parameters")
# Normalize document type
normalized_type = str(doc.type).lower()
if normalized_type != "document" and normalized_type != "page":
logger.error(f"Unexpected document type received: {doc.type}")
return
image = next((item for item in images_list if item.id == image.get('id')), None)
current_file_content = ""
file_uuid = image.get('uuid')
try:
img_url = f"./oss/files/{file_uuid}"
translated_filename = (
part.filename
.translate(str.maketrans('', '', r'[+],'))
.replace('%', '%25')
)
link_str = f""
# Append the content if necessary
if doc.is_paragraphed and doc.paragraph_number >= paragraph_limit:
current_file_content += "\n\n"
current_file_content += link_str
# Optionally update the document metadata or content
updated_doc_data['content'] = current_file_content
# Update existing file details or add new file to the list
if not image:
new_img_entry = {
'name': part.filename,
'metaData': {'createdDateUtc': datetime.utcnow()},
'sizeInBytes': file_size_bytes # Add size attribute if applicable
}
updated_doc_data['metadata']['files'].append(new_img_entry)
else:
old_path = f"{current_folder}/images/{old_filename}" if 'oldFilename' in part.metadata else original_file_path
os.remove(old_path)
final_path = f"{current_folder}/{updated_document_type}"
if not os.path.exists(final_path):
os.makedirs(final_path)
shutil.copy(original_file_path, final_path)
set_permissions(new_image_path, user_group_ids=user_group_ids[:])
updated_doc_data['version'] += "v1"
# Set the appropriate permission settings for the newly added file
if mode:
permissions_map[permission_name] = {"read": true, "write": false}
# Return the formatted link as a markdown reference
if normalize_link:
link_str = re.sub(r'/[\W_]+/', '-', cleaned_original_filename.lower())
if normalize_text:
# Implement text normalization logic here
pass
except Exception as e:
logger.exception(e) This revised code should address potential issues related to whitespace, filename encoding, and documentation clarity while maintaining functionality. |
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The provided code snippet is a function that takes an
image
object, aDocument
, a list ofimages_list
, andget_image_id
. However, there are minor improvements needed for clarity and robustness:Function Parameter Naming: The parameters
doc
should bedocument
to follow Python naming conventions.Code Clarity: Using descriptive variable names like
file_object
instead ofimage
forFile
objects can make the code more readable.Return Value Consistency: Ensure consistency in returning values; it seems like you want to return either the image tag or
None
.Here's the corrected version with these improvements:
Explanation of Changes:
image
tofile_object
to align with expected usage.file_object
andimages_list
.These changes enhance the code's readability and maintainability while ensuring its functionality remains unchanged.