Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: tts tool phi 329 #111

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
51 changes: 51 additions & 0 deletions phi/tools/tts.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import json
from typing import List, Optional

from phi.document import Document
from phi.knowledge.website import WebsiteKnowledgeBase
from phi.tools import ToolRegistry
from phi.utils.log import logger


class TTSTools(ToolRegistry):
def __init__(self):
super().__init__(name="TTS_tools")
self.register(self.save_audio)
self.register(self.read_audio)

def save_audio(self, text: str) -> str:
"""
"""
from openai import OpenAI
client = OpenAI()
from playsound import playsound

speech_file_path = "speech.mp3"
response = client.audio.speech.create(
model="tts-1",
voice="alloy",
input=text,
)

response.stream_to_file(speech_file_path)

playsound(speech_file_path)

# with openai.audio.speech.with_streaming_response.create(
# model="tts-1",
# voice="alloy",
# input=text,
# ) as response:
# response.stream_to_file(speech_file_path)
# playsound(speech_file_path)

return speech_file_path

def read_audio(self, speech_file_path: str) -> str:
"""
"""
from playsound import playsound
playsound(speech_file_path)

return "Audio played"