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

Create scripts/api.py

Browse files
Files changed (1) hide show
  1. scripts/api.py +77 -0
scripts/api.py ADDED
@@ -0,0 +1,77 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # It is helpful if you want to use it in a voice assistant project.
2
+ # Know more about {your gradio app url}/?view=api. Example: http://127.0.0.1:7860/?view=api
3
+ import shutil
4
+ import os
5
+ from gradio_client import Client
6
+
7
+ # Ensure the output directory exists
8
+ output_dir = "api_output"
9
+ os.makedirs(output_dir, exist_ok=True)
10
+
11
+ # Initialize the Gradio client
12
+ api_url = "http://127.0.0.1:7860/"
13
+ client = Client(api_url)
14
+
15
+
16
+
17
+
18
+ def text_to_speech(
19
+ text="Hello!!",
20
+ model_name="kokoro-v0_19.pth",
21
+ voice_name="af_bella",
22
+ speed=1,
23
+ pad_between_segments=0,
24
+ remove_silence=False,
25
+ minimum_silence=0.05,
26
+ custom_voicepack=None,
27
+ ):
28
+ """
29
+ Generates speech from text using a specified model and saves the audio file.
30
+
31
+ Parameters:
32
+ text (str): The text to convert to speech.
33
+ model_name (str): The name of the model to use for synthesis.
34
+ voice_name (str): The name of the voice to use.
35
+ speed (float): The speed of speech.
36
+ pad_between_segments (int): Padding between audio segments.
37
+ remove_silence (bool): Whether to remove silence from the audio.
38
+ minimum_silence (float): Minimum silence duration to consider.
39
+ custom_voicepack (str): Path to the custom voice pack to use.
40
+ Returns:
41
+ str: Path to the saved audio file.
42
+ """
43
+ # Call the API with provided parameters
44
+ result = client.predict(
45
+ text=text,
46
+ model_name=model_name,
47
+ voice_name=voice_name,
48
+ speed=speed,
49
+ pad_between_segments=pad_between_segments,
50
+ remove_silence=remove_silence,
51
+ minimum_silence=minimum_silence,
52
+ custom_voicepack=custom_voicepack,
53
+ api_name="/text_to_speech"
54
+ )
55
+ # Save the audio file in the specified directory
56
+ save_at = f"{output_dir}/{os.path.basename(result)}"
57
+ shutil.move(result, save_at)
58
+ print(f"Saved at {save_at}")
59
+
60
+ return save_at
61
+
62
+ # Example usage
63
+ if __name__ == "__main__":
64
+ text="This is Kokoro TTS. I am a text-to-speech model and Super Fast."
65
+ model_name="kokoro-v0_19.pth" #kokoro-v0_19-half.pth
66
+ voice_name="af_bella" #get voice names
67
+ speed=1
68
+ add_silence_between_segments=0 #it use in large text
69
+ remove_silence=False
70
+ keep_silence_upto=0.05 #in seconds
71
+ custom_voicepack=None
72
+ audio_path = text_to_speech(text=text, model_name=model_name,
73
+ voice_name=voice_name, speed=speed,
74
+ pad_between_segments=add_silence_between_segments,
75
+ remove_silence=remove_silence,
76
+ minimum_silence=keep_silence_upto)
77
+ print(f"Audio file saved at: {audio_path}")