4
4
import logging
5
5
import inspect
6
6
from collections import defaultdict
7
- from pydantic import BaseModel
7
+ from pydantic . v1 import BaseModel
8
8
from typing import Tuple , Union , Optional , Dict , Type , List
9
9
from pydub import AudioSegment
10
10
from telegram import Update
76
76
77
77
# Define a Voice model with id, name and description fields
78
78
class Voice (BaseModel ):
79
- id : Optional [str ] = None # Optional id for the voice
80
- name : Optional [str ] = None # Optional name for the voice
81
- description : Optional [str ] = None # Optional description for the voice
79
+ id : Optional [str ] = None # Optional id for the voice
80
+ name : Optional [str ] = None # Optional name for the voice
81
+ description : Optional [str ] = None # Optional description for the voice
82
+
82
83
83
84
# Array of tuples (synthesizer's voice id, nickname, description if text to voice)
84
85
DEFAULT_VOICES : List [Voice ] = [Voice (id = None , name = "Coqui Default" , description = None )]
85
86
87
+
86
88
# Define a Chat model with voices, current_voice and current_conversation fields
87
89
class Chat (BaseModel ):
88
- voices : List [Voice ] = DEFAULT_VOICES # List of available voices for the chat
89
- current_voice : Voice = DEFAULT_VOICES [0 ] # Current voice for the chat
90
- current_conversation : Optional [bytes ] = None # Current conversation as a pickled object
90
+ voices : List [Voice ] = DEFAULT_VOICES # List of available voices for the chat
91
+ current_voice : Voice = DEFAULT_VOICES [0 ] # Current voice for the chat
92
+ current_conversation : Optional [
93
+ bytes
94
+ ] = None # Current conversation as a pickled object
91
95
92
96
93
97
class VocodeBotResponder :
94
98
def __init__ (
95
99
self ,
96
100
transcriber : BaseTranscriber ,
97
101
system_prompt : str ,
98
- synthesizer : BaseSynthesizer
102
+ synthesizer : BaseSynthesizer ,
99
103
) -> None :
100
104
self .transcriber = transcriber
101
105
self .system_prompt = system_prompt
102
106
self .synthesizer = synthesizer
103
107
self .db : Dict [int , Chat ] = defaultdict (Chat )
104
108
105
-
106
109
def get_agent (self , chat_id : int ) -> ChatGPTAgent :
107
110
# Get current voice name and description from DB
108
111
user = self .db [chat_id ]
@@ -142,7 +145,9 @@ async def get_response(
142
145
voice_description = user .current_voice .description
143
146
144
147
# If we have a Coqui voice prompt, use that. Otherwise, set ID as synthesizer expects.
145
- if voice_description is not None and isinstance (self .synthesizer , CoquiSynthesizer ):
148
+ if voice_description is not None and isinstance (
149
+ self .synthesizer , CoquiSynthesizer
150
+ ):
146
151
self .synthesizer .voice_prompt = voice_description
147
152
elif voice_id is not None :
148
153
setattr (self .synthesizer , voice_attr_of [type (self .synthesizer )], voice_id )
@@ -162,7 +167,9 @@ async def handle_telegram_start(
162
167
start_text = """
163
168
I'm a voice chatbot, send a voice message to me and I'll send one back!" Use /help to see available commands.
164
169
"""
165
- await context .bot .send_message (chat_id = update .effective_chat .id , text = start_text )
170
+ await context .bot .send_message (
171
+ chat_id = update .effective_chat .id , text = start_text
172
+ )
166
173
167
174
async def handle_telegram_message (
168
175
self , update : Update , context : ContextTypes .DEFAULT_TYPE
@@ -302,7 +309,7 @@ async def handle_telegram_help(
302
309
- Use /help to see this help message again.
303
310
"""
304
311
assert update .effective_chat , "Chat must be defined!"
305
- if isinstance (self .synthesizer , CoquiSynthesizer ):
312
+ if isinstance (self .synthesizer , CoquiSynthesizer ):
306
313
help_text += "\n - Use /create <voice_description> to create a new Coqui voice from a text prompt and switch to it."
307
314
await context .bot .send_message (chat_id = update .effective_chat .id , text = help_text )
308
315
0 commit comments