jonathanagustin commited on
Commit
2c9d387
1 Parent(s): 0bb3772

Upload folder using huggingface_hub

Browse files
Files changed (1) hide show
  1. app.py +17 -15
app.py CHANGED
@@ -44,23 +44,25 @@ def tts(
44
  openai.api_key = api_key
45
 
46
  try:
47
- # Use the context manager for streaming response
48
- with openai.audio.speech.create(
49
- model=model,
50
- voice=voice,
51
- input=input_text,
52
- response_format=response_format,
53
- speed=speed,
54
- stream=True, # Enable streaming
55
- ) as response:
56
-
57
- # Save the audio content to a temporary file
58
- file_extension = f".{response_format}"
59
- with tempfile.NamedTemporaryFile(suffix=file_extension, delete=False) as temp_file:
60
- temp_file_path = temp_file.name
 
 
 
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}")