app-knmojt-13 / src /media /message_adapter.py
AiCoderv2's picture
Deploy Gradio app with multiple files
9f9394b verified
#
# SPDX-FileCopyrightText: Hadad <hadad@linuxmail.org>
# SPDX-License-Identifier: Apache-2.0
#
from .bytes_loader import load_file_bytes
from .encoding_converter import convert_to_base64
from .filetype_resolver import resolve_filetype
from .url_composer import compose_data_url
from .content_assembler import assemble_content_parts
async def adapt_message_format(incoming_message):
if isinstance(incoming_message, str):
return incoming_message
if not isinstance(incoming_message, dict):
return str(incoming_message)
text_value = incoming_message.get("text", "")
attached_files = incoming_message.get("files", [])
if not attached_files:
return text_value if text_value else ""
url_collection = []
for file_entry in attached_files:
file_location = file_entry if isinstance(file_entry, str) else file_entry.get("path")
if not file_location:
continue
binary_data = await load_file_bytes(file_location)
encoded_string = await convert_to_base64(binary_data)
file_type = await resolve_filetype(file_location)
url_item = await compose_data_url(encoded_string, file_type)
url_collection.append(url_item)
if not url_collection:
return text_value if text_value else ""
content_parts = await assemble_content_parts(text_value, url_collection)
return content_parts