jonathanagustin commited on
Commit
c455349
1 Parent(s): 6edfac9

Upload folder using huggingface_hub

Browse files
Files changed (1) hide show
  1. app.py +13 -11
app.py CHANGED
@@ -44,23 +44,25 @@ def tts(
44
  openai.api_key = api_key
45
 
46
  try:
47
- response = openai.audio.speech.create(
48
- model=model,
49
- voice=voice,
50
- input=input_text,
51
- response_format=response_format,
52
- speed=speed,
53
- )
54
-
55
  # Save the audio content to a temporary file
56
  file_extension = f".{response_format}"
57
- with tempfile.NamedTemporaryFile(suffix=file_extension, delete=False) as temp_file:
 
 
58
  temp_file_path = temp_file.name
59
 
60
- with response.with_streaming_response():
 
 
 
 
 
 
 
 
 
61
  for chunk in response:
62
  temp_file.write(chunk)
63
-
64
  except openai.OpenAIError as e:
65
  # Catch OpenAI exceptions
66
  raise gr.Error(f"An OpenAI error occurred: {e}")
 
44
  openai.api_key = api_key
45
 
46
  try:
 
 
 
 
 
 
 
 
47
  # Save the audio content to a temporary file
48
  file_extension = f".{response_format}"
49
+ with tempfile.NamedTemporaryFile(
50
+ suffix=file_extension, delete=False, mode="wb"
51
+ ) as temp_file:
52
  temp_file_path = temp_file.name
53
 
54
+ # Use a context manager to handle the streaming response
55
+ with openai.audio.speech.create(
56
+ model=model,
57
+ voice=voice,
58
+ input=input_text,
59
+ response_format=response_format,
60
+ speed=speed,
61
+ stream=True,
62
+ ) as response:
63
+ # Stream the response directly to the temp file
64
  for chunk in response:
65
  temp_file.write(chunk)
 
66
  except openai.OpenAIError as e:
67
  # Catch OpenAI exceptions
68
  raise gr.Error(f"An OpenAI error occurred: {e}")