Skip to content
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

Dont move files to cache automatically in chatbot postprocess #9303

Merged
merged 4 commits into from
Sep 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/khaki-ducks-stare.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"gradio": minor
---

feat:Dont move files to cache automatically in chatbot postprocess
31 changes: 3 additions & 28 deletions gradio/components/chatbot.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
from gradio.data_classes import FileData, GradioModel, GradioRootModel
from gradio.events import Events
from gradio.exceptions import Error
from gradio.processing_utils import move_resource_to_block_cache


class MetadataDict(TypedDict):
Expand Down Expand Up @@ -434,7 +433,7 @@ def _postprocess_messages_tuples(self, value: TupleFormat) -> ChatbotDataTuples:

def _postprocess_message_messages(
self, message: MessageDict | ChatMessage
) -> list[Message]:
) -> Message:
if isinstance(message, dict):
message["content"] = self._postprocess_content(message["content"])
msg = Message(**message) # type: ignore
Expand All @@ -450,30 +449,7 @@ def _postprocess_message_messages(
f"Invalid message for Chatbot component: {message}", visible=False
)

# extract file path from message
new_messages = []
if isinstance(msg.content, str):
for word in msg.content.split(" "):
filepath = Path(word)
try:
is_file = filepath.is_file() and filepath.exists()
except OSError:
is_file = False
if is_file:
filepath = cast(
str, move_resource_to_block_cache(filepath, block=self)
)
mime_type = client_utils.get_mimetype(filepath)
new_messages.append(
Message(
role=msg.role,
metadata=msg.metadata,
content=FileMessage(
file=FileData(path=filepath, mime_type=mime_type)
),
),
)
return [msg, *new_messages]
return msg

def postprocess(
self,
Expand All @@ -495,9 +471,8 @@ def postprocess(
return self._postprocess_messages_tuples(cast(TupleFormat, value))
self._check_format(value, "messages")
processed_messages = [
msg
self._postprocess_message_messages(cast(MessageDict, message))
for message in value
for msg in self._postprocess_message_messages(cast(MessageDict, message))
]
return ChatbotDataMessages(root=processed_messages)

Expand Down
Loading