File size: 1,233 Bytes
8d7f55c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#
# Copyright (c) 2024, Daily
#
# SPDX-License-Identifier: BSD 2-Clause License
#

from typing import Coroutine

from pipecat.frames.frames import Frame, TextFrame
from pipecat.processors.frame_processor import FrameDirection, FrameProcessor


class StatelessTextTransformer(FrameProcessor):
    """This processor calls the given function on any text in a text frame.



    >>> async def print_frames(aggregator, frame):

    ...     async for frame in aggregator.process_frame(frame):

    ...         print(frame.text)



    >>> aggregator = StatelessTextTransformer(lambda x: x.upper())

    >>> asyncio.run(print_frames(aggregator, TextFrame("Hello")))

    HELLO

    """

    def __init__(self, transform_fn):
        super().__init__()
        self._transform_fn = transform_fn

    async def process_frame(self, frame: Frame, direction: FrameDirection):
        await super().process_frame(frame, direction)

        if isinstance(frame, TextFrame):
            result = self._transform_fn(frame.text)
            if isinstance(result, Coroutine):
                result = await result
            await self.push_frame(result)
        else:
            await self.push_frame(frame, direction)