Spaces:
Runtime error
Runtime error
EC2 Default User
commited on
Commit
•
b27938b
1
Parent(s):
e98544f
change polly to real time
Browse files
app.py
CHANGED
@@ -125,61 +125,63 @@ def download_file(bucket_name, object_key, file_path):
|
|
125 |
except Exception as e:
|
126 |
print(f"Error downloading file: {str(e)}")
|
127 |
|
|
|
128 |
|
129 |
-
|
130 |
-
response = polly.start_speech_synthesis_task(
|
131 |
-
OutputS3BucketName='lingo-audio-materials', #this bucket is in us-east-1
|
132 |
-
OutputS3KeyPrefix='answers/',
|
133 |
-
OutputFormat='mp3',
|
134 |
Text=text,
|
|
|
|
|
135 |
VoiceId='Zhiyu',
|
136 |
LanguageCode='cmn-CN',
|
137 |
-
Engine='neural'
|
138 |
-
|
139 |
-
|
140 |
-
# Print the task ID and status
|
141 |
-
task_id = response['SynthesisTask']['TaskId']
|
142 |
-
print('Task ID:', task_id)
|
143 |
-
|
144 |
-
while True:
|
145 |
-
task = polly.get_speech_synthesis_task(TaskId=task_id)
|
146 |
-
task_status = task['SynthesisTask']['TaskStatus']
|
147 |
|
148 |
-
|
149 |
-
|
150 |
-
|
151 |
-
|
152 |
-
|
153 |
-
|
154 |
-
|
155 |
-
|
156 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
157 |
|
158 |
-
|
159 |
-
|
160 |
-
|
161 |
|
162 |
-
|
163 |
-
|
164 |
-
|
165 |
-
|
166 |
-
print("bucket name:"+bucket_name)
|
167 |
-
print("key name:"+key_name)
|
168 |
|
169 |
-
|
|
|
|
|
|
|
170 |
print("mp3_pre_signed_url:"+mp3_pre_signed_url)
|
171 |
|
172 |
-
output_file = "/tmp/from-s3.mp3"
|
173 |
current_dir = os.getcwd()
|
174 |
-
#file_absolute_path = current_dir+'/'+output_file
|
175 |
print("current dir:"+current_dir)
|
176 |
print("output_file_location: "+output_file)
|
177 |
-
download_file(bucket_name, key_name, output_file)
|
178 |
-
#encoded_path = file_absolute_path.encode("utf-8")
|
179 |
-
|
180 |
-
#tmp_aud_file_url = output_file
|
181 |
-
#htm_audio = f'<audio><source src={tmp_aud_file_url} type="audio/mp3" autoplay></audio>'
|
182 |
-
#audio_htm = gr.HTML(htm_audio)
|
183 |
return output_file, mp3_pre_signed_url
|
184 |
|
185 |
def generate_talk_with_audio(input, avatar_url, api_key = did_api_key):
|
|
|
125 |
except Exception as e:
|
126 |
print(f"Error downloading file: {str(e)}")
|
127 |
|
128 |
+
def polly_text_to_audio(audio_file_name, text, audio_format):
|
129 |
|
130 |
+
polly_response = polly.synthesize_speech(
|
|
|
|
|
|
|
|
|
131 |
Text=text,
|
132 |
+
OutputFormat=audio_format,
|
133 |
+
SampleRate='16000',
|
134 |
VoiceId='Zhiyu',
|
135 |
LanguageCode='cmn-CN',
|
136 |
+
Engine='neural',
|
137 |
+
LexiconNames=['tigoCN']
|
138 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
139 |
|
140 |
+
# Access the audio stream from the response
|
141 |
+
if "AudioStream" in polly_response:
|
142 |
+
# Note: Closing the stream is important because the service throttles on the
|
143 |
+
# number of parallel connections. Here we are using contextlib.closing to
|
144 |
+
# ensure the close method of the stream object will be called automatically
|
145 |
+
# at the end of the with statement's scope.
|
146 |
+
with closing(polly_response["AudioStream"]) as stream:
|
147 |
+
try:
|
148 |
+
# Open a file for writing the output as a binary stream
|
149 |
+
with open(audio_file_name, "ab") as file:
|
150 |
+
file.write(stream.read())
|
151 |
+
except IOError as error:
|
152 |
+
# Could not write to file, exit gracefully
|
153 |
+
print(error)
|
154 |
+
sys.exit(-1)
|
155 |
+
|
156 |
+
else:
|
157 |
+
# The response didn't contain audio data, exit gracefully
|
158 |
+
print("Could not stream audio")
|
159 |
+
sys.exit(-1)
|
160 |
+
|
161 |
+
|
162 |
+
def play_s3_voice(text):
|
163 |
+
|
164 |
+
output_file = "/tmp/response.mp3"
|
165 |
+
polly_text_to_audio(output_file, text, "mp3")
|
166 |
|
167 |
+
# Upload the file to an S3 bucket
|
168 |
+
audio_output_bucket_name = "lingo-audio-materials"
|
169 |
+
audio_output_s3_key = "answers/response.mp3"
|
170 |
|
171 |
+
s3.upload_file(file_name, audio_output_bucket_name, audio_output_s3_key)
|
172 |
+
|
173 |
+
# Construct the S3 bucket URI
|
174 |
+
s3_uri = f"s3://{audio_output_bucket_name}/{audio_output_s3_key}"
|
|
|
|
|
175 |
|
176 |
+
print("audio output bucket name:"+audio_output_bucket_name)
|
177 |
+
print("audio output key name:"+audio_output_s3_key)
|
178 |
+
|
179 |
+
mp3_pre_signed_url = s3.generate_presigned_url('get_object',Params={'Bucket': audio_output_bucket_name,'Key': audio_output_s3_key},ExpiresIn=3600)
|
180 |
print("mp3_pre_signed_url:"+mp3_pre_signed_url)
|
181 |
|
|
|
182 |
current_dir = os.getcwd()
|
|
|
183 |
print("current dir:"+current_dir)
|
184 |
print("output_file_location: "+output_file)
|
|
|
|
|
|
|
|
|
|
|
|
|
185 |
return output_file, mp3_pre_signed_url
|
186 |
|
187 |
def generate_talk_with_audio(input, avatar_url, api_key = did_api_key):
|