rafaa commited on
Commit
8d1d1e1
·
verified ·
1 Parent(s): f84fa53

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +28 -15
app.py CHANGED
@@ -3,16 +3,26 @@ import gradio as gr
3
  from datetime import datetime
4
  import pytz
5
  import os
6
- import elevenlabs
 
7
 
8
- elevenlabsKey = os.environ.get("ELEVEN_KEY_TOKEN", None)
9
- voice = os.environ.get("VOICE_ID", None)
 
10
  model = "eleven_multilingual_v2"
11
  ttsPassword = os.environ.get("TTS_PASSWORD", None)
12
  enable_tts = False
13
 
14
  client = InferenceClient("mistralai/Mixtral-8x7B-Instruct-v0.1")
15
 
 
 
 
 
 
 
 
 
16
  def format_prompt(message, history, system_prompt=None):
17
  prompt = "<s>"
18
 
@@ -27,6 +37,19 @@ def format_prompt(message, history, system_prompt=None):
27
  prompt += f"[INST] {message} [/INST]"
28
  return prompt
29
 
 
 
 
 
 
 
 
 
 
 
 
 
 
30
  def generate(
31
  prompt, history, enable_tts, tts_password, temperature=0.2, max_new_tokens=256, top_p=0.95, repetition_penalty=1.0
32
  ):
@@ -115,18 +138,8 @@ def generate(
115
  return output
116
 
117
  # Check if the checkbox is selected and the correct password is provided
118
- if enable_tts and passwordInput == ttsPassword:
119
- # Generate the audio file
120
- audio = elevenlabs.generate(response, voice, model, api_key=elevenlabsKey)
121
-
122
- # Play the audio file
123
- elevenlabs.play(audio)
124
-
125
- # Return the audio file as a numpy array
126
- return audio
127
- else:
128
- # Return None if the TTS is not enabled or the password is incorrect
129
- return None
130
 
131
  checkbox = gr.Checkbox(label="Enable TTS")
132
  passwordInput = gr.Textbox(label="TTS Password", type="password")
 
3
  from datetime import datetime
4
  import pytz
5
  import os
6
+ import numpy
7
+ from elevenlabs import voices, generate, set_api_key, UnauthenticatedRateLimitError
8
 
9
+
10
+ elevenlabs_api_key = os.environ.get("ELEVEN_KEY_TOKEN", None)
11
+ voice_id = os.environ.get("VOICE_ID", None)
12
  model = "eleven_multilingual_v2"
13
  ttsPassword = os.environ.get("TTS_PASSWORD", None)
14
  enable_tts = False
15
 
16
  client = InferenceClient("mistralai/Mixtral-8x7B-Instruct-v0.1")
17
 
18
+ def pad_buffer(audio):
19
+ # Pad buffer to multiple of 2 bytes
20
+ buffer_size = len(audio)
21
+ element_size = np.dtype(np.int16).itemsize
22
+ if buffer_size % element_size != 0:
23
+ audio = audio + b'\0' * (element_size - (buffer_size % element_size))
24
+ return audio
25
+
26
  def format_prompt(message, history, system_prompt=None):
27
  prompt = "<s>"
28
 
 
37
  prompt += f"[INST] {message} [/INST]"
38
  return prompt
39
 
40
+ def generate_voice(text, voice_name, api_key):
41
+ set_api_key(elevenlabs_api_key) #set API key
42
+ try:
43
+ audio = generate(
44
+ voice=voice_name,
45
+ model="eleven_multilingual_v2"
46
+ )
47
+ return (44100, np.frombuffer(pad_buffer(audio), dtype=np.int16))
48
+ except UnauthenticatedRateLimitError as e:
49
+ raise gr.Error("Thanks for trying out ElevenLabs TTS! You've reached the free tier limit. Please provide an API key to continue.")
50
+ except Exception as e:
51
+ raise gr.Error(e)
52
+
53
  def generate(
54
  prompt, history, enable_tts, tts_password, temperature=0.2, max_new_tokens=256, top_p=0.95, repetition_penalty=1.0
55
  ):
 
138
  return output
139
 
140
  # Check if the checkbox is selected and the correct password is provided
141
+ if enable_tts and passwordInput == ttsPassword:
142
+ generate_voice(output, voice_id)
 
 
 
 
 
 
 
 
 
 
143
 
144
  checkbox = gr.Checkbox(label="Enable TTS")
145
  passwordInput = gr.Textbox(label="TTS Password", type="password")