NeuralFalcon commited on
Commit
c920f65
·
verified ·
1 Parent(s): 2efdbdf

Create cli.py

Browse files
Files changed (1) hide show
  1. scripts/cli.py +129 -0
scripts/cli.py ADDED
@@ -0,0 +1,129 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ gradio_url="http://127.0.0.1:7860/"
2
+
3
+ from gradio_client import Client
4
+ import json
5
+ import os
6
+ import shutil
7
+ from rich.console import Console
8
+ from rich.text import Text
9
+ import simpleaudio as sa
10
+
11
+ # Initialize the Gradio client
12
+ client = Client(gradio_url)
13
+
14
+ console = Console()
15
+
16
+ voice_dict = None
17
+ voices = {}
18
+ output_dir = "api_output"
19
+
20
+ def get_voice_names():
21
+ global voice_dict
22
+ # Get the result from the client
23
+ result = client.predict(api_name="/get_voice_names")
24
+ # Convert the result string into a Python dictionary
25
+ voice_dict = json.loads(result)
26
+ id = 1
27
+ for key in voice_dict:
28
+ if len(voice_dict[key]) > 0:
29
+ for i in voice_dict[key]:
30
+ voices[id] = i
31
+ id += 1
32
+ return voice_dict
33
+
34
+ def display_voice_names():
35
+ global voice_dict
36
+ id_num = 1
37
+ for key in voice_dict:
38
+ if len(voice_dict[key]) > 0:
39
+ gender = key.replace("_", " ").capitalize()
40
+ console.print(f"[bold cyan]{gender}:[/bold cyan]")
41
+ for voice in voice_dict[key]:
42
+ console.print(f"[green]{id_num}. {voice}[/green]")
43
+ id_num += 1
44
+ # console.print()
45
+
46
+ def text_to_speech(
47
+ text="Hello!!",
48
+ model_name="kokoro-v0_19.pth",
49
+ voice_name="af_bella",
50
+ speed=1,
51
+ pad_between_segments=0,
52
+ remove_silence=False,
53
+ minimum_silence=0.05,
54
+ custom_voicepack=None,
55
+ ):
56
+ # Call the API with provided parameters
57
+ result = client.predict(
58
+ text=text,
59
+ model_name=model_name,
60
+ voice_name=voice_name,
61
+ speed=speed,
62
+ pad_between_segments=pad_between_segments,
63
+ remove_silence=remove_silence,
64
+ minimum_silence=minimum_silence,
65
+ custom_voicepack=custom_voicepack,
66
+ api_name="/text_to_speech"
67
+ )
68
+ # Save the audio file in the specified directory
69
+ save_at = f"{output_dir}/{os.path.basename(result)}"
70
+ shutil.move(result, save_at)
71
+ return save_at
72
+
73
+ def choose_voice():
74
+ while True:
75
+ display_voice_names()
76
+
77
+ user_input = console.input("[bold yellow]Choose a voice ID number or type 'exit' to quit: [/bold yellow]")
78
+ try:
79
+ voice_number = int(user_input)
80
+ if voice_number in voices:
81
+ selected_voice = voices[voice_number]
82
+ console.print(f"[bold green]You selected: {selected_voice}[/bold green]\n")
83
+ return selected_voice
84
+ else:
85
+ console.print("[bold red]Invalid number. Please choose a valid voice number.[/bold red]")
86
+ except ValueError:
87
+ if user_input.lower() == "exit":
88
+ console.print("[bold red]Exiting voice selection.[/bold red]")
89
+ return None
90
+ else:
91
+ console.print("[bold red]Invalid input. Please enter a valid number or 'exit'.[/bold red]")
92
+
93
+ def play_audio(filename):
94
+ wave_obj = sa.WaveObject.from_wave_file(filename)
95
+ play_obj = wave_obj.play()
96
+ play_obj.wait_done()
97
+
98
+ # Ensure the output directory exists
99
+ os.makedirs(output_dir, exist_ok=True)
100
+
101
+ voice_dict = get_voice_names()
102
+
103
+ def text_to_speech_cli():
104
+ while True:
105
+ voice_name = choose_voice()
106
+ if not voice_name:
107
+ break
108
+ # print(
109
+ # "Type 'C' to change voice or 'q' to quit"
110
+ # )
111
+ while True:
112
+
113
+ text = console.input(
114
+ "[bold green]Enter Text ('C' to change voice, 'q' for quit): [/bold green]"
115
+ )
116
+ if text.lower() == "q":
117
+ console.print("[bold red]Exiting text-to-speech CLI.[/bold red]")
118
+ return
119
+ elif text.lower() == "c":
120
+ console.print("[bold yellow]Changing voice...[/bold yellow]")
121
+ break # Break the inner loop to select a new voice
122
+ else:
123
+ audio_path = text_to_speech(text=text, voice_name=voice_name)
124
+ play_audio(audio_path)
125
+ # console.print("[bold green]Audio played successfully![/bold green]\n")
126
+
127
+ if __name__ == "__main__":
128
+ console.print("[bold blue]Welcome to the Text-to-Speech CLI![/bold blue]\n")
129
+ text_to_speech_cli()