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

feat: added local TTS support using Kokoro #401

Merged
merged 8 commits into from
Jan 14, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ SRCDIRS = gptme tests scripts
SRCFILES = $(shell find ${SRCDIRS} -name '*.py')

# exclude files
EXCLUDES = tests/output scripts/build_changelog.py
EXCLUDES = tests/output scripts/build_changelog.py scripts/tts_server.py
SRCFILES = $(shell find ${SRCDIRS} -name '*.py' $(foreach EXCLUDE,$(EXCLUDES),-not -path $(EXCLUDE)))

build:
Expand Down
31 changes: 27 additions & 4 deletions gptme/chat.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
import re
import sys
import termios
from typing import cast
import urllib.parse
from collections.abc import Generator
from pathlib import Path
from typing import cast

from .commands import action_descriptions, execute_cmd
from .config import get_config
Expand All @@ -19,15 +19,16 @@
from .message import Message
from .prompts import get_workspace_prompt
from .tools import (
ConfirmFunc,
ToolFormat,
ToolUse,
has_tool,
get_tools,
execute_msg,
ConfirmFunc,
get_tools,
has_tool,
set_tool_format,
)
from .tools.browser import read_url
from .tools.tts import speak
from .util import console, path_with_tilde, print_bell
from .util.ask_execute import ask_execute
from .util.context import use_fresh_context
Expand Down Expand Up @@ -247,6 +248,11 @@ def step(
if os.environ.get("GPTME_COSTS") in ["1", "true"]:
log_costs(msgs + [msg_response])

# speak if TTS tool is available
if has_tool("tts"):
if speech := _clean_content_for_speech(msg_response.content).strip():
speak(speech)

# log response and run tools
if msg_response:
yield msg_response.replace(quiet=True)
Expand Down Expand Up @@ -454,6 +460,23 @@ def _parse_prompt(prompt: str) -> str | None:
return result


def _clean_content_for_speech(content: str) -> str:
"""
Clean content for speech by removing:
- <thinking> tags and their content
- Tool use blocks (```tool ...```)

Returns the cleaned content suitable for speech.
"""
# Remove <thinking> tags and their content
content = re.sub(r"<thinking>.*?(</thinking>|$)", "", content, flags=re.DOTALL)

# Remove tool use blocks
content = re.sub(r"```\w+[^`]*(```|$)", "", content, flags=re.DOTALL)

return content.strip()


def _parse_prompt_files(prompt: str) -> Path | None:
"""
Takes a string that might be a supported file path (image, text, PDF) and returns the path.
Expand Down
Loading
Loading