jonathanagustin commited on
Commit
5c3b1e4
1 Parent(s): 09aa99e

Upload folder using huggingface_hub

Browse files
Files changed (1) hide show
  1. app.py +50 -3
app.py CHANGED
@@ -14,10 +14,57 @@ def tts(
14
  speed: float = 1.0,
15
  ) -> str:
16
  """
17
- [Function remains unchanged]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
18
  """
19
- # [Function body remains unchanged]
20
- # ...
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
21
 
22
  def main():
23
  """
 
14
  speed: float = 1.0,
15
  ) -> str:
16
  """
17
+ Convert input text to speech using OpenAI's Text-to-Speech API.
18
+
19
+ :param input_text: The text to be converted to speech.
20
+ :type input_text: str
21
+ :param model: The model to use for synthesis (e.g., 'tts-1', 'tts-1-hd').
22
+ :type model: str
23
+ :param voice: The voice profile to use (e.g., 'alloy', 'echo', 'fable', etc.).
24
+ :type voice: str
25
+ :param api_key: OpenAI API key.
26
+ :type api_key: str
27
+ :param response_format: The audio format of the output file, defaults to 'mp3'.
28
+ :type response_format: str, optional
29
+ :param speed: The speed of the synthesized speech (0.25 to 4.0), defaults to 1.0.
30
+ :type speed: float, optional
31
+ :return: File path to the generated audio file.
32
+ :rtype: str
33
+ :raises gr.Error: If input parameters are invalid or API call fails.
34
  """
35
+ if not api_key.strip():
36
+ raise gr.Error(
37
+ "API key is required. Get an API key at: https://platform.openai.com/account/api-keys"
38
+ )
39
+
40
+ if not input_text.strip():
41
+ raise gr.Error("Input text cannot be empty.")
42
+
43
+ openai.api_key = api_key
44
+
45
+ try:
46
+ response = openai.audio.speech.create(
47
+ model=model,
48
+ voice=voice,
49
+ input=input_text,
50
+ response_format=response_format,
51
+ speed=speed,
52
+ )
53
+ except openai.error.OpenAIError as e:
54
+ # Catch OpenAI exceptions
55
+ raise gr.Error(f"An OpenAI error occurred: {e}")
56
+ except Exception as e:
57
+ # Catch any other exceptions
58
+ raise gr.Error(f"An unexpected error occurred: {e}")
59
+
60
+ # Save the audio content to a temporary file
61
+ file_extension = f".{response_format}"
62
+ with tempfile.NamedTemporaryFile(suffix=file_extension, delete=False) as temp_file:
63
+ response.stream_to_file(temp_file.name)
64
+ temp_file_path = temp_file.name
65
+
66
+ return temp_file_path
67
+
68
 
69
  def main():
70
  """