youngtsai commited on
Commit
c2544ea
·
1 Parent(s): 20da85e

response = OPEN_AI_CLIENT.audio.speech.create(

Browse files
Files changed (2) hide show
  1. app.py +18 -11
  2. requirements.txt +0 -2
app.py CHANGED
@@ -269,22 +269,29 @@ def paragraph_save(refine_paragraph):
269
 
270
  def paragraph_save_and_tts(paragraph_text):
271
  """
272
- Saves the paragraph text and generates an audio file using Hugging Face's TTS.
273
  """
274
- # Instantiate the text-to-speech pipeline
275
- tts = pipeline("text-to-speech", model="facebook/fastspeech2-en-ljspeech")
 
 
 
 
 
276
 
277
- # Generate speech
278
- speech = tts(paragraph_text)
279
 
280
- # Define the path for the audio file
281
- audio_path = "/mnt/data/generated_audio.wav"
282
 
283
- # Save the audio file
284
- sf.write(audio_path, speech["array"], speech["sampling_rate"])
285
 
286
- # Return the path to the audio file along with the text
287
- return paragraph_text, audio_path
 
 
288
 
289
  with gr.Blocks() as demo:
290
  with gr.Row():
 
269
 
270
  def paragraph_save_and_tts(paragraph_text):
271
  """
272
+ Saves the paragraph text and generates an audio file using OpenAI's TTS.
273
  """
274
+ try:
275
+ # Call OpenAI's TTS API to generate speech from text
276
+ response = OPEN_AI_CLIENT.audio.speech.create(
277
+ model="tts-1",
278
+ voice="alloy",
279
+ input=paragraph_text,
280
+ )
281
 
282
+ # Define the path for the audio file
283
+ audio_path = "/mnt/data/generated_audio_openai.mp3"
284
 
285
+ # Save the audio stream to a file
286
+ response.stream_to_file(audio_path)
287
 
288
+ # Return the path to the audio file along with the text
289
+ return paragraph_text, audio_path
290
 
291
+ except Exception as e:
292
+ print(f"An error occurred while generating TTS: {e}")
293
+ # Handle the error appropriately (e.g., return an error message or a default audio path)
294
+ return paragraph_text, None
295
 
296
  with gr.Blocks() as demo:
297
  with gr.Row():
requirements.txt CHANGED
@@ -1,4 +1,2 @@
1
  gradio
2
  openai>=1.0.0
3
- transformers
4
- soundfile
 
1
  gradio
2
  openai>=1.0.0