kevinwang676 commited on
Commit
2b48c61
1 Parent(s): 1deb9a5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -7
app.py CHANGED
@@ -4,7 +4,7 @@ import tempfile
4
  from openai import OpenAI
5
 
6
  # Set an environment variable for key
7
- os.environ['OPENAI_API_KEY'] = os.environ.get('OPENAI_API_KEY')
8
 
9
  client = OpenAI() # add api_key
10
 
@@ -29,12 +29,23 @@ def voice_change(audio_in, audio_ref):
29
  return 'output.wav'
30
 
31
 
32
- def tts(text, model, voice):
33
- response = client.audio.speech.create(
34
- model=model, #"tts-1","tts-1-hd"
35
- voice=voice, #'alloy', 'echo', 'fable', 'onyx', 'nova', 'shimmer'
36
- input=text,
37
- )
 
 
 
 
 
 
 
 
 
 
 
38
 
39
  # Create a temp file to save the audio
40
  with tempfile.NamedTemporaryFile(suffix=".mp3", delete=False) as temp_file:
 
4
  from openai import OpenAI
5
 
6
  # Set an environment variable for key
7
+ #os.environ['OPENAI_API_KEY'] = os.environ.get('OPENAI_API_KEY')
8
 
9
  client = OpenAI() # add api_key
10
 
 
29
  return 'output.wav'
30
 
31
 
32
+ def tts(text, model, voice, api_key):
33
+ if api_key == '':
34
+ raise gr.Error('Please enter your OpenAI API Key')
35
+ else:
36
+ try:
37
+ client = OpenAI(api_key=api_key)
38
+
39
+ response = client.audio.speech.create(
40
+ model=model, # "tts-1","tts-1-hd"
41
+ voice=voice, # 'alloy', 'echo', 'fable', 'onyx', 'nova', 'shimmer'
42
+ input=text,
43
+ )
44
+
45
+ except Exception as error:
46
+ # Handle any exception that occurs
47
+ raise gr.Error("An error occurred while generating speech. Please check your API key and try again.")
48
+ print(str(error))
49
 
50
  # Create a temp file to save the audio
51
  with tempfile.NamedTemporaryFile(suffix=".mp3", delete=False) as temp_file: