Spaces:
Edmond98
/
Running on A100

Afrinetwork7 commited on
Commit
efc7284
1 Parent(s): 5639044

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +16 -14
app.py CHANGED
@@ -16,7 +16,6 @@ from logging.handlers import RotatingFileHandler
16
  import os
17
  import boto3
18
  from botocore.exceptions import NoCredentialsError
19
- from urllib.parse import quote
20
  import time
21
 
22
  # Import functions from other modules
@@ -160,25 +159,28 @@ async def synthesize_speech(request: TTSRequest):
160
  # Generate a unique filename
161
  filename = f"synthesized_audio_{int(time.time())}.wav"
162
 
163
- # Upload to S3
164
  try:
165
- s3_client.upload_fileobj(buffer, S3_BUCKET, filename)
 
 
 
 
 
166
  logger.info(f"File uploaded successfully to S3: {filename}")
167
 
168
- # Generate a presigned URL
169
- url = s3_client.generate_presigned_url('get_object',
170
- Params={'Bucket': S3_BUCKET,
171
- 'Key': filename},
172
- ExpiresIn=3600) # URL expires in 1 hour
173
 
174
- encoded_url = quote(url, safe=':/?&=')
175
- logger.info(f"Presigned URL generated: {encoded_url}")
176
-
177
- return JSONResponse(content={"audio_url": encoded_url})
178
 
179
  except NoCredentialsError:
180
- logger.error("AWS credentials not available")
181
- raise HTTPException(status_code=500, detail="Could not upload file to S3")
 
 
 
182
 
183
  except ValueError as ve:
184
  logger.error(f"ValueError in synthesize_speech: {str(ve)}", exc_info=True)
 
16
  import os
17
  import boto3
18
  from botocore.exceptions import NoCredentialsError
 
19
  import time
20
 
21
  # Import functions from other modules
 
159
  # Generate a unique filename
160
  filename = f"synthesized_audio_{int(time.time())}.wav"
161
 
162
+ # Upload to S3 with public-read ACL
163
  try:
164
+ s3_client.upload_fileobj(
165
+ buffer,
166
+ S3_BUCKET,
167
+ filename,
168
+ ExtraArgs={'ACL': 'public-read', 'ContentType': 'audio/wav'}
169
+ )
170
  logger.info(f"File uploaded successfully to S3: {filename}")
171
 
172
+ # Generate the public URL
173
+ url = f"https://{S3_BUCKET}.s3.{S3_REGION}.amazonaws.com/{filename}"
174
+ logger.info(f"Public URL generated: {url}")
 
 
175
 
176
+ return JSONResponse(content={"audio_url": url})
 
 
 
177
 
178
  except NoCredentialsError:
179
+ logger.error("AWS credentials not available or invalid")
180
+ raise HTTPException(status_code=500, detail="Could not upload file to S3: Missing or invalid credentials")
181
+ except Exception as e:
182
+ logger.error(f"Failed to upload to S3: {str(e)}")
183
+ raise HTTPException(status_code=500, detail=f"Could not upload file to S3: {str(e)}")
184
 
185
  except ValueError as ve:
186
  logger.error(f"ValueError in synthesize_speech: {str(ve)}", exc_info=True)