tommy24 commited on
Commit
b837136
·
1 Parent(s): ce74a88

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +71 -20
app.py CHANGED
@@ -37,39 +37,90 @@
37
  # iface.launch()
38
 
39
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
40
  import gradio as gr
41
  import requests
42
  import urllib.request
43
  from pydub import AudioSegment
44
  import numpy as np
45
  import os
 
 
 
 
 
 
 
 
 
 
 
 
46
 
47
  def function1(prompt):
48
  response = requests.post("https://tommy24-testing3.hf.space/run/predict", json={
49
  "data": [
50
  prompt,
51
  ]}).json()
52
- data = response["data"][0]
53
- response = requests.post("https://matthijs-speecht5-tts-demo.hf.space/run/predict", json={
54
- "data": [
55
- data,
56
- "KSP (male)",
57
- ]
58
- }).json()
59
- data = response["data"][0]["name"]
60
- data = "https://matthijs-speecht5-tts-demo.hf.space/file="+data
61
- file_name, headers = urllib.request.urlretrieve(data, "speech.mp3")
62
- # code = random.randint(1,1000)
63
- # generated_file = f"output{code}"
64
- filename = "output.mp3"
65
 
66
- if os.path.exists(filename):
67
- os.remove(filename)
68
- else:
69
- pass
70
- command = f"ffmpeg -i {file_name} -vn -ar 44100 -ac 2 -b:a 192k output.mp3"
71
- os.system(command)
72
- return "output.mp3"
 
 
 
 
 
 
 
 
 
 
73
 
74
  iface = gr.Interface(fn=function1, inputs="text", outputs=[gr.Audio(label="Audio",type="numpy")])
75
  iface.launch()
 
37
  # iface.launch()
38
 
39
 
40
+ # import gradio as gr
41
+ # import requests
42
+ # import urllib.request
43
+ # from pydub import AudioSegment
44
+ # import numpy as np
45
+ # import os
46
+
47
+ # def function1(prompt):
48
+ # response = requests.post("https://tommy24-testing3.hf.space/run/predict", json={
49
+ # "data": [
50
+ # prompt,
51
+ # ]}).json()
52
+ # data = response["data"][0]
53
+ # response = requests.post("https://matthijs-speecht5-tts-demo.hf.space/run/predict", json={
54
+ # "data": [
55
+ # data,
56
+ # "KSP (male)",
57
+ # ]
58
+ # }).json()
59
+ # data = response["data"][0]["name"]
60
+ # data = "https://matthijs-speecht5-tts-demo.hf.space/file="+data
61
+ # file_name, headers = urllib.request.urlretrieve(data, "speech.mp3")
62
+ # # code = random.randint(1,1000)
63
+ # # generated_file = f"output{code}"
64
+ # filename = "output.mp3"
65
+
66
+ # if os.path.exists(filename):
67
+ # os.remove(filename)
68
+ # else:
69
+ # pass
70
+ # command = f"ffmpeg -i {file_name} -vn -ar 44100 -ac 2 -b:a 192k output.mp3"
71
+ # os.system(command)
72
+ # return "output.mp3"
73
+
74
+ # iface = gr.Interface(fn=function1, inputs="text", outputs=[gr.Audio(label="Audio",type="numpy")])
75
+ # iface.launch()
76
+
77
  import gradio as gr
78
  import requests
79
  import urllib.request
80
  from pydub import AudioSegment
81
  import numpy as np
82
  import os
83
+ import sys
84
+ import wave
85
+ import io
86
+ import base64
87
+ import azure.cognitiveservices.speech as speechsdk
88
+
89
+ speech_key = os.environ.get("test3")
90
+ service_region = os.environ.get("test4")
91
+
92
+ speech_config = speechsdk.SpeechConfig(subscription=speech_key, region=service_region)
93
+ # Note: the voice setting will not overwrite the voice element in input SSML.
94
+ speech_config.speech_synthesis_voice_name = os.environ.get("test5")
95
 
96
  def function1(prompt):
97
  response = requests.post("https://tommy24-testing3.hf.space/run/predict", json={
98
  "data": [
99
  prompt,
100
  ]}).json()
101
+ message = response["data"][0]
102
+ speech_synthesizer = speechsdk.SpeechSynthesizer(speech_config=speech_config)
103
+ result = speech_synthesizer.speak_text_async(message).get()
104
+ if result.reason == speechsdk.ResultReason.SynthesizingAudioCompleted:
105
+ audio_stream = io.BytesIO(result.audio_data)
 
 
 
 
 
 
 
 
106
 
107
+ # Create a wave file object and write the audio data to it
108
+ with wave.open("audio.wav", 'wb') as wave_file:
109
+ wave_file.setnchannels(1)
110
+ wave_file.setsampwidth(2)
111
+ wave_file.setframerate(16000)
112
+ wave_file.writeframesraw(audio_stream.getvalue())
113
+
114
+ # Use ffmpeg to convert the wave file to an mp3 file
115
+ filename = "output.mp3"
116
+
117
+ if os.path.exists(filename):
118
+ os.remove(filename)
119
+ else:
120
+ pass
121
+ command = f"ffmpeg -i audio.wav -y -codec:a libmp3lame -qscale:a 2 {filename}"
122
+ os.system(command)
123
+ return "output.mp3"
124
 
125
  iface = gr.Interface(fn=function1, inputs="text", outputs=[gr.Audio(label="Audio",type="numpy")])
126
  iface.launch()