-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprocessors.py
More file actions
63 lines (53 loc) · 2.95 KB
/
processors.py
File metadata and controls
63 lines (53 loc) · 2.95 KB
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 datetime
from loguru import logger
from pipecat.frames.frames import LLMTextFrame, TranscriptionFrame
from pipecat.processors.frame_processor import FrameDirection, FrameProcessor
class TextTranscriptionProcessor(FrameProcessor):
"""
Processor that converts LLM text frames to transcription frames with 'ai' user_id.
This allows AI responses to be displayed in the UI transcript.
"""
def __init__(self, transport, serializer):
super().__init__()
self.transport = transport
self.serializer = serializer
logger.info("TextTranscriptionProcessor initialized")
async def process_frame(self, frame, direction=None):
# Call the parent's process_frame method first
await super().process_frame(frame, direction)
# If this is an LLMTextFrame, create a transcription frame
if isinstance(frame, LLMTextFrame):
text = frame.text if hasattr(frame, 'text') else str(frame)
logger.debug(f"TextTranscriptionProcessor: Processing LLMTextFrame: {text}")
# First pass through the original frame for TTS conversion
await self.push_frame(frame, direction)
try:
# Create a TranscriptionFrame with explicit field values
timestamp = datetime.datetime.now().isoformat()
# Debug the frame creation
logger.debug(f"Creating TranscriptionFrame with: text='{text}', user_id='ai', timestamp='{timestamp}'")
transcription_frame = TranscriptionFrame(
id=123456, # Add an ID field
name="AI Transcription", # Add a name field
text=text,
user_id="ai", # This must be explicitly set to 'ai'
timestamp=timestamp
)
# Debug the created frame
logger.debug(f"Created TranscriptionFrame: {transcription_frame}")
# Try to use the transport's output method
if hasattr(self.transport, 'output'):
logger.debug(f"Pushing TranscriptionFrame to transport output")
await self.transport.output().push_frame(transcription_frame, FrameDirection.DOWNSTREAM)
else:
# Fallback: push directly to pipeline
logger.debug(f"Pushing TranscriptionFrame directly to pipeline")
await self.push_frame(transcription_frame, direction)
logger.info(f"Sent AI transcription: '{text}'")
except Exception as e:
logger.error(f"Error creating or sending transcription frame: {e}")
import traceback
logger.error(traceback.format_exc())
else:
# For any non-LLMTextFrame, just pass it through
await self.push_frame(frame, direction)