Skip to content

Commit

Permalink
Dont move files to cache automatically in chatbot postprocess (#9303)
Browse files Browse the repository at this point in the history
* Fix

* add changeset

* Add code

---------

Co-authored-by: gradio-pr-bot <[email protected]>
  • Loading branch information
2 people authored and dawoodkhan82 committed Sep 12, 2024
1 parent 152f630 commit ee6f8f5
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 28 deletions.
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 @@ -479,7 +478,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 @@ -495,30 +494,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 @@ -540,9 +516,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

0 comments on commit ee6f8f5

Please sign in to comment.