-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtext-to-speech.py
217 lines (176 loc) · 6.81 KB
/
text-to-speech.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
import os
import sys
import time
import yaml
from pathlib import Path
from openai import OpenAI
from textual.app import App, ComposeResult
from textual.containers import Container, Horizontal
from textual.widgets import Button, Footer, Header, Input, Label, ProgressBar, Static
from textual import work
class FileInput(Input):
def __init__(self, **kwargs):
super().__init__(**kwargs)
self.last_click_time = 0
def on_click(self, event):
current_time = time.time()
if current_time - self.last_click_time < 0.3: # Double-click detected
self.show_file_dialog()
self.last_click_time = current_time
def show_file_dialog(self):
import tkinter as tk
from tkinter import filedialog
root = tk.Tk()
root.withdraw()
file_path = filedialog.askopenfilename(filetypes=[("Text files", "*.txt")])
if file_path:
self.value = file_path
self.refresh()
class TextToSpeechApp(App):
CSS = """
Screen {
background: $surface;
align: center middle;
}
#file_input, #voice_input, #speed_input {
width: 100%;
margin: 1 0;
}
#progress {
height: 1;
margin: 1 0;
}
#status {
height: 1;
}
.container {
margin: 1 1;
}
Horizontal {
height: 6;
margin: 1 0;
}
Button {
margin: 1 0;
}
"""
def compose(self) -> ComposeResult:
yield Header()
yield Container(
Label("Enter the path to the text file:"),
FileInput(id="file_input", placeholder="Double click to browse or enter path..."),
Horizontal(
Container(
Label("Voice:"),
Input(id="voice_input", placeholder="Enter voice..."),
),
Container(
Label("Speed:"),
Input(id="speed_input", placeholder="Enter speed..."),
),
),
Button("Generate Speech", id="generate_button", variant="primary"),
ProgressBar(id="progress", total=100, show_eta=False),
Static(id="status", expand=True),
classes="container"
)
def on_mount(self) -> None:
self.load_config()
self.title = "Text-to-Speech Generator"
self.sub_title = "Generate speech from text files with OpenAI"
# Set default values
if self.config['default']['tts_voice']:
self.query_one("#voice_input").value = self.config['default']['tts_voice']
if self.config['default']['tts_speed']:
self.query_one("#speed_input").value = str(self.config['default']['tts_speed'])
def on_button_pressed(self, event: Button.Pressed) -> None:
if event.button.id == "generate_button":
self.generate_speech()
@work(thread=True)
def generate_speech(self) -> None:
file_path = self.query_one("#file_input").value
voice = self.query_one("#voice_input").value
speed = self.query_one("#speed_input").value
if not file_path:
self.query_one("#status").update("Please enter a file path.")
return
file_path = file_path.strip("\"'")
if not os.path.exists(file_path):
self.query_one("#status").update("File not found. Please check the path.")
return
self.query_one("#progress").update(progress=0)
self.query_one("#status").update("Initializing...")
self.update_progress(10)
# Load config and initialize OpenAI client
client = OpenAI(api_key=self.config['openai']['api_key'])
# Read text from file
with open(file_path, 'r') as file:
text = file.read()
self.query_one("#status").update("Generating speech...")
self.update_progress(20)
try:
audio_content = self.generate_speech_content(
client=client,
text=text,
model=self.config['openai']['tts_model'],
voice=voice,
speed=float(speed) if speed else 1.0
)
self.update_progress(80)
# Save the audio file
output_path = file_path.rsplit('.', 1)[0] + '.tts.mp3'
self.save_audio_file(audio_content, output_path)
self.update_progress(100)
self.query_one("#status").update(f"Audio saved to {output_path}")
except Exception as e:
self.query_one("#status").update(f"Error during speech generation: {e}")
def update_progress(self, percentage):
self.query_one("#progress").update(progress=percentage)
def load_config(self):
self.config = {}
if getattr(sys, 'frozen', False):
base_path = os.path.dirname(sys.executable)
else:
base_path = os.path.dirname(os.path.abspath(__file__))
config_path = os.path.join(base_path, 'config.yaml')
try:
with open(config_path, 'r') as config_file:
self.config = yaml.safe_load(config_file)
if 'openai' not in self.config:
raise KeyError("'openai' key not found in config")
required_keys = ['api_key', 'tts_model']
for key in required_keys:
if key not in self.config['openai']:
raise KeyError(f"Config file missing required key: openai.{key}")
if 'default' not in self.config:
raise KeyError("'default' key not found in config")
default_keys = ['tts_voice', 'tts_speed']
for key in default_keys:
if key not in self.config['default']:
raise KeyError(f"Config file missing required key: default.{key}")
except FileNotFoundError:
error_msg = f"Config file not found: {config_path}"
self.query_one("#status").update(error_msg)
except yaml.YAMLError as e:
error_msg = f"Error parsing config file: {e}"
self.query_one("#status").update(error_msg)
except KeyError as e:
error_msg = f"Configuration error: {e}"
self.query_one("#status").update(error_msg)
def generate_speech_content(self, client, text, model, voice, speed):
response = client.audio.speech.create(
model=model,
input=text,
voice=voice,
response_format="mp3",
speed=speed
)
return response.content
def save_audio_file(self, audio_content, output_path):
with open(output_path, "wb") as audio_file:
audio_file.write(audio_content)
def main():
app = TextToSpeechApp()
app.run()
if __name__ == "__main__":
main()