services(riva): cleanup

This commit is contained in:
vipyne
2024-12-09 15:40:29 -06:00
parent 5a467a30a3
commit 9222d9f721
2 changed files with 11 additions and 117 deletions

View File

@@ -1,94 +0,0 @@
#
# Copyright (c) 2024, Daily
#
# SPDX-License-Identifier: BSD 2-Clause License
#
import asyncio
import os
import sys
import aiohttp
from dotenv import load_dotenv
from loguru import logger
from runner import configure
from pipecat.audio.vad.silero import SileroVADAnalyzer
from pipecat.frames.frames import LLMMessagesFrame
from pipecat.pipeline.pipeline import Pipeline
from pipecat.pipeline.runner import PipelineRunner
from pipecat.pipeline.task import PipelineParams, PipelineTask
from pipecat.processors.aggregators.openai_llm_context import OpenAILLMContext
from pipecat.services.deepgram import DeepgramSTTService
from pipecat.services.riva import FastpitchTTSService, ParakeetSTTService
from pipecat.services.openai import OpenAILLMService
from pipecat.transports.services.daily import DailyParams, DailyTransport
load_dotenv(override=True)
logger.remove(0)
logger.add(sys.stderr, level="DEBUG")
async def main():
async with aiohttp.ClientSession() as session:
(room_url, _) = await configure(session)
transport = DailyTransport(
room_url,
None,
"Respond bot",
DailyParams(
audio_out_enabled=True,
vad_enabled=True,
vad_analyzer=SileroVADAnalyzer(),
vad_audio_passthrough=True,
),
)
# stt = DeepgramSTTService(api_key=os.getenv("DEEPGRAM_API_KEY"))
stt = ParakeetSTTService(api_key=os.getenv("NVIDIA_API_KEY"))
tts = FastpitchTTSService(api_key=os.getenv("NVIDIA_API_KEY"))
llm = OpenAILLMService(api_key=os.getenv("OPENAI_API_KEY"), model="gpt-4o")
messages = [
{
"role": "system",
"content": "You are a helpful LLM in a WebRTC call. Your goal is to demonstrate your capabilities in a succinct way. Your output will be converted to audio so don't include special characters in your answers. Respond to what the user said in a creative and helpful way.",
},
]
context = OpenAILLMContext(messages)
context_aggregator = llm.create_context_aggregator(context)
pipeline = Pipeline(
[
transport.input(), # Transport user input
stt, # STT
context_aggregator.user(), # User responses
llm, # LLM
tts, # TTS
transport.output(), # Transport bot output
context_aggregator.assistant(), # Assistant spoken responses
]
)
task = PipelineTask(pipeline, PipelineParams(allow_interruptions=True))
@transport.event_handler("on_first_participant_joined")
async def on_first_participant_joined(transport, participant):
# Kick off the conversation.
messages.append({"role": "system", "content": "Please introduce yourself to the user."})
await task.queue_frames([LLMMessagesFrame(messages)])
runner = PipelineRunner()
await runner.run(task)
if __name__ == "__main__":
asyncio.run(main())

View File

@@ -5,11 +5,7 @@
# #
import asyncio import asyncio
<<<<<<< HEAD
from typing import AsyncGenerator, Optional from typing import AsyncGenerator, Optional
=======
from typing import AsyncGenerator, List, Optional, Union, Iterator
>>>>>>> 07e3942e (add nvidia riva - fastpitch)
from loguru import logger from loguru import logger
from pydantic.main import BaseModel from pydantic.main import BaseModel
@@ -17,10 +13,6 @@ from pydantic.main import BaseModel
from pipecat.frames.frames import ( from pipecat.frames.frames import (
CancelFrame, CancelFrame,
EndFrame, EndFrame,
<<<<<<< HEAD
=======
ErrorFrame,
>>>>>>> 07e3942e (add nvidia riva - fastpitch)
Frame, Frame,
InterimTranscriptionFrame, InterimTranscriptionFrame,
StartFrame, StartFrame,
@@ -46,11 +38,8 @@ except ModuleNotFoundError as e:
class FastpitchTTSService(TTSService): class FastpitchTTSService(TTSService):
class InputParams(BaseModel): class InputParams(BaseModel):
<<<<<<< HEAD
language: Optional[Language] = Language.EN_US language: Optional[Language] = Language.EN_US
======= quality: Optional[int] = 20
language: Optional[str] = "en-US"
>>>>>>> 07e3942e (add nvidia riva - fastpitch)
def __init__( def __init__(
self, self,
@@ -58,19 +47,18 @@ class FastpitchTTSService(TTSService):
api_key: str, api_key: str,
server: str = "grpc.nvcf.nvidia.com:443", server: str = "grpc.nvcf.nvidia.com:443",
voice_id: str = "English-US.Female-1", voice_id: str = "English-US.Female-1",
sample_rate_hz: int = 24000, sample_rate: int = 24000,
# nvidia riva calls this 'function-id' # nvidia riva calls this 'function-id'
model: str = "0149dedb-2be8-4195-b9a0-e57e0e14f972", model: str = "0149dedb-2be8-4195-b9a0-e57e0e14f972",
params: InputParams = InputParams(), params: InputParams = InputParams(),
quality: int = 20,
**kwargs, **kwargs,
): ):
super().__init__(sample_rate=sample_rate_hz, **kwargs) super().__init__(sample_rate=sample_rate, **kwargs)
self._api_key = api_key self._api_key = api_key
self._voice_id = voice_id self._voice_id = voice_id
self._sample_rate_hz = sample_rate_hz self._sample_rate = sample_rate
self._language_code = params.language self._language_code = params.language
self.quality = quality self.quality = params.quality
self.set_model_name("fastpitch-hifigan-tts") self.set_model_name("fastpitch-hifigan-tts")
self.set_voice(voice_id) self.set_voice(voice_id)
@@ -81,16 +69,16 @@ class FastpitchTTSService(TTSService):
] ]
auth = riva.client.Auth(None, True, server, metadata) auth = riva.client.Auth(None, True, server, metadata)
self.service = riva.client.SpeechSynthesisService(auth) self._service = riva.client.SpeechSynthesisService(auth)
async def run_tts(self, text: str) -> AsyncGenerator[Frame, None]: async def run_tts(self, text: str) -> AsyncGenerator[Frame, None]:
def read_audio_responses(): def read_audio_responses():
try: try:
responses = self.service.synthesize_online( responses = self._service.synthesize_online(
text, text,
self._voice_id, self._voice_id,
self._language_code, self._language_code,
sample_rate_hz=self._sample_rate_hz, sample_rate_hz=self._sample_rate,
audio_prompt_file=None, audio_prompt_file=None,
quality=self.quality, quality=self.quality,
custom_dictionary={}, custom_dictionary={},
@@ -112,7 +100,7 @@ class FastpitchTTSService(TTSService):
frame = TTSAudioRawFrame( frame = TTSAudioRawFrame(
audio=resp.audio, audio=resp.audio,
sample_rate=self._sample_rate_hz, sample_rate=self._sample_rate,
num_channels=1, num_channels=1,
) )
yield frame yield frame
@@ -150,7 +138,7 @@ class ParakeetSTTService(STTService):
self._stop_history_eou = -1 self._stop_history_eou = -1
self._stop_threshold_eou = -1.0 self._stop_threshold_eou = -1.0
self._custom_configuration = "" self._custom_configuration = ""
self._sample_rate_hz: int = 16000 self._sample_rate: int = 16000
self.set_model_name("parakeet-ctc-1.1b-asr") self.set_model_name("parakeet-ctc-1.1b-asr")
@@ -171,7 +159,7 @@ class ParakeetSTTService(STTService):
profanity_filter=self._profanity_filter, profanity_filter=self._profanity_filter,
enable_automatic_punctuation=self._automatic_punctuation, enable_automatic_punctuation=self._automatic_punctuation,
verbatim_transcripts=not self._no_verbatim_transcripts, verbatim_transcripts=not self._no_verbatim_transcripts,
sample_rate_hertz=self._sample_rate_hz, sample_rate_hertz=self._sample_rate,
audio_channel_count=1, audio_channel_count=1,
), ),
interim_results=True, interim_results=True,