Spaces:
Sleeping
Sleeping
ASG Models
commited on
Commit
•
45e2ed5
1
Parent(s):
5cf26a3
Update app.py
Browse files
app.py
CHANGED
@@ -166,17 +166,17 @@ def generate_audio(text,speaker_id=None):
|
|
166 |
yield 16000,chunk#.squeeze().cpu().numpy()#.astype(np.int16).tobytes()
|
167 |
|
168 |
|
169 |
-
def get_answer_ai(text):
|
170 |
-
|
171 |
try:
|
172 |
-
response =
|
173 |
return response
|
174 |
|
175 |
|
176 |
except :
|
177 |
-
|
178 |
-
response =
|
179 |
-
return response
|
180 |
|
181 |
def modelspeech(text):
|
182 |
with torch.no_grad():
|
@@ -194,29 +194,29 @@ def clean_text(text):
|
|
194 |
return cleaned_text.strip() # Remove leading/trailing spaces
|
195 |
|
196 |
|
197 |
-
def text_to_speech(text):
|
198 |
|
199 |
-
response = dash(text,False)
|
200 |
pad_text=''
|
201 |
k=0
|
202 |
for chunk in response:
|
203 |
-
|
204 |
pad_text+=str(clean_text(chunk))
|
205 |
|
206 |
if pad_text!='' and len(pad_text)>10:
|
207 |
out=pad_text
|
208 |
pad_text=''
|
209 |
k+=1
|
210 |
-
yield modelspeech(out)
|
211 |
# for stream_wav in generate_audio(out):
|
212 |
# yield stream_wav
|
213 |
if pad_text!='':
|
214 |
-
yield modelspeech(pad_text)
|
215 |
# for stream_wav in generate_audio(pad_text):
|
216 |
# yield stream_wav
|
217 |
-
def dash(text,is_state=True):
|
218 |
|
219 |
-
response=get_answer_ai(text)
|
220 |
txt=' '
|
221 |
for chunk in response:
|
222 |
if is_state:
|
@@ -225,7 +225,7 @@ def dash(text,is_state=True):
|
|
225 |
txt=chunk.text
|
226 |
|
227 |
|
228 |
-
yield txt
|
229 |
|
230 |
|
231 |
|
@@ -234,17 +234,18 @@ def dash(text,is_state=True):
|
|
234 |
# demo.launch()
|
235 |
|
236 |
with gr.Blocks() as demo:
|
|
|
237 |
with gr.Tab("AI Text "):
|
238 |
gr.Markdown("# Text to Speech")
|
239 |
text_input = gr.Textbox(label="Enter Text")
|
240 |
text_out = gr.Textbox()
|
241 |
|
242 |
-
text_input.submit(dash, [text_input],
|
243 |
with gr.Tab("AI Speech"):
|
244 |
gr.Markdown("# Text to Speech")
|
245 |
text_input2 = gr.Textbox(label="Enter Text")
|
246 |
audio_output = gr.Audio(streaming=True,autoplay=True)
|
247 |
-
text_input2.submit(text_to_speech, text_input2, audio_output)
|
248 |
|
249 |
|
250 |
demo.launch(show_error=True)
|
|
|
166 |
yield 16000,chunk#.squeeze().cpu().numpy()#.astype(np.int16).tobytes()
|
167 |
|
168 |
|
169 |
+
def get_answer_ai(text,session_ai):
|
170 |
+
|
171 |
try:
|
172 |
+
response = session_ai.send_message(text,stream=True)
|
173 |
return response
|
174 |
|
175 |
|
176 |
except :
|
177 |
+
session_ai=create_chat_session()
|
178 |
+
response = session_ai.send_message(text,stream=True)
|
179 |
+
return response,session_ai
|
180 |
|
181 |
def modelspeech(text):
|
182 |
with torch.no_grad():
|
|
|
194 |
return cleaned_text.strip() # Remove leading/trailing spaces
|
195 |
|
196 |
|
197 |
+
def text_to_speech(text,session_ai):
|
198 |
|
199 |
+
response = dash(text,session_ai,False)
|
200 |
pad_text=''
|
201 |
k=0
|
202 |
for chunk in response:
|
203 |
+
chunk,session_ai=chunk
|
204 |
pad_text+=str(clean_text(chunk))
|
205 |
|
206 |
if pad_text!='' and len(pad_text)>10:
|
207 |
out=pad_text
|
208 |
pad_text=''
|
209 |
k+=1
|
210 |
+
yield modelspeech(out),session_ai
|
211 |
# for stream_wav in generate_audio(out):
|
212 |
# yield stream_wav
|
213 |
if pad_text!='':
|
214 |
+
yield modelspeech(pad_text),session_ai
|
215 |
# for stream_wav in generate_audio(pad_text):
|
216 |
# yield stream_wav
|
217 |
+
def dash(text,session_ai,is_state=True):
|
218 |
|
219 |
+
response,session_ai=get_answer_ai(text,session_ai)
|
220 |
txt=' '
|
221 |
for chunk in response:
|
222 |
if is_state:
|
|
|
225 |
txt=chunk.text
|
226 |
|
227 |
|
228 |
+
yield txt,session_ai
|
229 |
|
230 |
|
231 |
|
|
|
234 |
# demo.launch()
|
235 |
|
236 |
with gr.Blocks() as demo:
|
237 |
+
session_ai=gr.State()
|
238 |
with gr.Tab("AI Text "):
|
239 |
gr.Markdown("# Text to Speech")
|
240 |
text_input = gr.Textbox(label="Enter Text")
|
241 |
text_out = gr.Textbox()
|
242 |
|
243 |
+
text_input.submit(dash, [text_input,session_ai],[text_out,session_ai])
|
244 |
with gr.Tab("AI Speech"):
|
245 |
gr.Markdown("# Text to Speech")
|
246 |
text_input2 = gr.Textbox(label="Enter Text")
|
247 |
audio_output = gr.Audio(streaming=True,autoplay=True)
|
248 |
+
text_input2.submit(text_to_speech, [text_input2,session_ai], [audio_output,session_ai])
|
249 |
|
250 |
|
251 |
demo.launch(show_error=True)
|