cleanup examples and remove requests library
This commit is contained in:
@@ -6,15 +6,13 @@
|
||||
|
||||
import aiohttp
|
||||
|
||||
from typing import AsyncGenerator
|
||||
from typing import Any, AsyncGenerator, Dict
|
||||
|
||||
from pipecat.frames.frames import AudioRawFrame, ErrorFrame, Frame
|
||||
from pipecat.frames.frames import AudioRawFrame, ErrorFrame, Frame, StartFrame
|
||||
from pipecat.services.ai_services import TTSService
|
||||
|
||||
from loguru import logger
|
||||
|
||||
import requests
|
||||
|
||||
import numpy as np
|
||||
|
||||
try:
|
||||
@@ -48,13 +46,25 @@ class XTTSService(TTSService):
|
||||
self._voice_id = voice_id
|
||||
self._language = language
|
||||
self._base_url = base_url
|
||||
self._studio_speakers = requests.get(self._base_url + "/studio_speakers").json()
|
||||
self._studio_speakers: Dict[str, Any] | None = None
|
||||
self._aiohttp_session = aiohttp_session or aiohttp.ClientSession()
|
||||
self._close_aiohttp_session = aiohttp_session is None
|
||||
|
||||
def can_generate_metrics(self) -> bool:
|
||||
return True
|
||||
|
||||
async def start(self, frame: StartFrame):
|
||||
await super().start(frame)
|
||||
async with self._aiohttp_session.get(self._base_url + "/studio_speakers") as r:
|
||||
if r.status != 200:
|
||||
text = await r.text()
|
||||
logger.error(
|
||||
f"{self} error getting studio speakers (status: {r.status}, error: {text})")
|
||||
await self.push_error(
|
||||
ErrorFrame(f"Error error getting studio speakers (status: {r.status}, error: {text})"))
|
||||
return
|
||||
self._studio_speakers = await r.json()
|
||||
|
||||
async def cleanup(self):
|
||||
await super().cleanup()
|
||||
if self._close_aiohttp_session:
|
||||
@@ -66,6 +76,11 @@ class XTTSService(TTSService):
|
||||
|
||||
async def run_tts(self, text: str) -> AsyncGenerator[Frame, None]:
|
||||
logger.debug(f"Generating TTS: [{text}]")
|
||||
|
||||
if not self._studio_speakers:
|
||||
logger.error(f"{self} no studio speakers available")
|
||||
return
|
||||
|
||||
embeddings = self._studio_speakers[self._voice_id]
|
||||
|
||||
url = self._base_url + "/tts_stream"
|
||||
|
||||
@@ -11,7 +11,7 @@ Methods that wrap the Daily API to create rooms, check room URLs, and get meetin
|
||||
|
||||
"""
|
||||
|
||||
import requests
|
||||
import aiohttp
|
||||
import time
|
||||
|
||||
from urllib.parse import urlparse
|
||||
@@ -61,24 +61,25 @@ class DailyRoomObject(BaseModel):
|
||||
|
||||
|
||||
class DailyRESTHelper:
|
||||
def __init__(self, daily_api_key: str, daily_api_url: str = "https://api.daily.co/v1"):
|
||||
def __init__(self,
|
||||
daily_api_key: str,
|
||||
daily_api_url: str = "https://api.daily.co/v1"):
|
||||
self.daily_api_key = daily_api_key
|
||||
self.daily_api_url = daily_api_url
|
||||
|
||||
def _get_name_from_url(self, room_url: str) -> str:
|
||||
return urlparse(room_url).path[1:]
|
||||
|
||||
def create_room(self, params: DailyRoomParams) -> DailyRoomObject:
|
||||
res = requests.post(
|
||||
f"{self.daily_api_url}/rooms",
|
||||
headers={"Authorization": f"Bearer {self.daily_api_key}"},
|
||||
json={**params.model_dump(exclude_none=True)}
|
||||
)
|
||||
async def create_room(self, params: DailyRoomParams) -> DailyRoomObject:
|
||||
async with aiohttp.ClientSession() as session:
|
||||
headers = {"Authorization": f"Bearer {self.daily_api_key}"}
|
||||
json = {**params.model_dump(exclude_none=True)}
|
||||
async with session.post(f"{self.daily_api_url}/rooms", headers=headers, json=json) as r:
|
||||
if r.status != 200:
|
||||
text = await r.text()
|
||||
raise Exception(f"Unable to create room: {text}")
|
||||
|
||||
if res.status_code != 200:
|
||||
raise Exception(f"Unable to create room: {res.text}")
|
||||
|
||||
data = res.json()
|
||||
data = await r.json()
|
||||
|
||||
try:
|
||||
room = DailyRoomObject(**data)
|
||||
@@ -87,29 +88,30 @@ class DailyRESTHelper:
|
||||
|
||||
return room
|
||||
|
||||
def _get_room_from_name(self, room_name: str) -> DailyRoomObject:
|
||||
res: requests.Response = requests.get(
|
||||
f"{self.daily_api_url}/rooms/{room_name}",
|
||||
headers={"Authorization": f"Bearer {self.daily_api_key}"}
|
||||
)
|
||||
async def _get_room_from_name(self, room_name: str) -> DailyRoomObject:
|
||||
async with aiohttp.ClientSession() as session:
|
||||
headers = {"Authorization": f"Bearer {self.daily_api_key}"}
|
||||
async with session.get(f"{self.daily_api_url}/rooms/{room_name}", headers=headers) as r:
|
||||
if r.status != 200:
|
||||
raise Exception(f"Room not found: {room_name}")
|
||||
|
||||
if res.status_code != 200:
|
||||
raise Exception(f"Room not found: {room_name}")
|
||||
|
||||
data = res.json()
|
||||
|
||||
try:
|
||||
room = DailyRoomObject(**data)
|
||||
except ValidationError as e:
|
||||
raise Exception(f"Invalid response: {e}")
|
||||
data = await r.json()
|
||||
try:
|
||||
room = DailyRoomObject(**data)
|
||||
except ValidationError as e:
|
||||
raise Exception(f"Invalid response: {e}")
|
||||
|
||||
return room
|
||||
|
||||
def get_room_from_url(self, room_url: str,) -> DailyRoomObject:
|
||||
async def get_room_from_url(self, room_url: str,) -> DailyRoomObject:
|
||||
room_name = self._get_name_from_url(room_url)
|
||||
return self._get_room_from_name(room_name)
|
||||
return await self._get_room_from_name(room_name)
|
||||
|
||||
def get_token(self, room_url: str, expiry_time: float = 60 * 60, owner: bool = True) -> str:
|
||||
async def get_token(
|
||||
self,
|
||||
room_url: str,
|
||||
expiry_time: float = 60 * 60,
|
||||
owner: bool = True) -> str:
|
||||
if not room_url:
|
||||
raise Exception(
|
||||
"No Daily room specified. You must specify a Daily room in order a token to be generated.")
|
||||
@@ -118,22 +120,21 @@ class DailyRESTHelper:
|
||||
|
||||
room_name = self._get_name_from_url(room_url)
|
||||
|
||||
res: requests.Response = requests.post(
|
||||
f"{self.daily_api_url}/meeting-tokens",
|
||||
headers={
|
||||
"Authorization": f"Bearer {self.daily_api_key}"},
|
||||
json={
|
||||
async with aiohttp.ClientSession() as session:
|
||||
headers = {"Authorization": f"Bearer {self.daily_api_key}"}
|
||||
json = {
|
||||
"properties": {
|
||||
"room_name": room_name,
|
||||
"is_owner": owner,
|
||||
"exp": expiration
|
||||
}},
|
||||
)
|
||||
}
|
||||
}
|
||||
async with session.post(f"{self.daily_api_url}/meeting-tokens", headers=headers, json=json) as r:
|
||||
if r.status != 200:
|
||||
text = await r.text()
|
||||
raise Exception(f"Failed to create meeting token: {r.status} {text}")
|
||||
|
||||
if res.status_code != 200:
|
||||
raise Exception(
|
||||
f"Failed to create meeting token: {res.status_code} {res.text}")
|
||||
|
||||
token: str = res.json()["token"]
|
||||
data = await r.json()
|
||||
token: str = data["token"]
|
||||
|
||||
return token
|
||||
|
||||
Reference in New Issue
Block a user