-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMyChatInterface.py
63 lines (51 loc) · 1.85 KB
/
MyChatInterface.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import gradio as gr
from gradio.components.multimodal_textbox import MultimodalData
from gradio.components.chatbot import FileDataDict, Message, MessageDict, TupleFormat
from typing import AsyncGenerator, Callable, Literal, Union, cast
from gradio.events import Dependency, on
from gradio.utils import SyncToAsyncIterator, async_iteration, async_lambda
from gradio.routes import Request
from gradio.helpers import special_args
import anyio
from gradio.components import (
Button,
Chatbot,
Component,
Markdown,
MultimodalTextbox,
State,
Textbox,
get_component_instance,
)
class MyMultimodalData(MultimodalData):
other_params: str | MultimodalData | None
class MyChatInterface(gr.ChatInterface):
def __init__(self,*args,**kwargs):
super().__init__(*args,**kwargs)
async def _delete_prev_fn(
self,
message: str | MultimodalData | None,
history: list[MessageDict] | TupleFormat,
) -> tuple[
list[MessageDict] | TupleFormat,
str | MultimodalData,
list[MessageDict] | TupleFormat,
]:
# temp = MyMultimodalData()
extra = 1 if self.type == "messages" else 0
if self.multimodal and isinstance(message, MultimodalData):
remove_input = (
len(message.files) + 1
if message.text is not None
else len(message.files)
) + extra
# temp.__dict__.update(message.__dict__)
message.__class__ = MyMultimodalData
message.other_params = history[-remove_input:][0][-1]
# message = temp
history = history[:-remove_input]
else:
message.__class__ = MyMultimodalData
message.other_params = history[-(1 + extra):][0][-1]
history = history[: -(1 + extra)]
return history, message or "" # type: ignore