@@ -60,6 +60,67 @@ def encode_image(image_file):
60
60
aws_secret_access_key = os .getenv ('AWS_SECRET_ACCESS_KEY' )
61
61
)
62
62
63
+ # Initialize S3 client
64
+ s3_client = boto3 .client (
65
+ 's3' ,
66
+ region_name = os .getenv ('AWS_REGION' ),
67
+ aws_access_key_id = os .getenv ('AWS_ACCESS_KEY_ID' ),
68
+ aws_secret_access_key = os .getenv ('AWS_SECRET_ACCESS_KEY' )
69
+ )
70
+
71
+ def process_audio_message (data , mobile , manish ):
72
+ try :
73
+ audio = manish .get_audio (data )
74
+ audio_id , mime_type = audio ["id" ], audio ["mime_type" ]
75
+ audio_url = manish .query_media_url (audio_id )
76
+ audio_filename = manish .download_media (audio_url , mime_type )
77
+
78
+ # Upload the audio file to S3
79
+ s3_audio_url = upload_file_to_s3 (audio_filename , 'meta-llama-bucket' , object_name = f"audio_{ audio_id } " )
80
+
81
+ if s3_audio_url :
82
+ logger .info (f"{ mobile } sent audio { audio_filename } , uploaded to S3: { s3_audio_url } " )
83
+ else :
84
+ logger .error (f"Failed to upload audio { audio_filename } to S3" )
85
+
86
+ # Determine the sample rate (you might need to extract this from the audio file)
87
+ sample_rate = 16000 # Example: 16kHz
88
+
89
+ # Stream the audio file to Amazon Transcribe
90
+ transcription = "" # You would implement the transcription logic here
91
+
92
+ return audio_filename , s3_audio_url , transcription
93
+ except Exception as e :
94
+ logger .error (f"Error processing audio message: { e } " )
95
+ return None , None , None
96
+
97
+ def upload_file_to_s3 (file_path , bucket_name , object_name = None ):
98
+ """
99
+ Upload a file to an S3 bucket and return its public URL.
100
+
101
+ :param file_path: File to upload
102
+ :param bucket_name: Bucket to upload to
103
+ :param object_name: S3 object name. If not specified, file_name is used
104
+ :return: Public URL of the uploaded file if successful, else None
105
+ """
106
+ # If S3 object_name was not specified, use file_name
107
+ if object_name is None :
108
+ object_name = os .path .basename (file_path )
109
+
110
+ try :
111
+ # Upload the file
112
+ s3_client .upload_file (file_path , bucket_name , object_name )
113
+
114
+ # Make the object public
115
+ s3_client .put_object_acl (Bucket = bucket_name , Key = object_name , ACL = 'public-read' )
116
+
117
+ # Generate the public URL
118
+ public_url = f"https://{ bucket_name } .s3.amazonaws.com/{ object_name } "
119
+ return public_url
120
+ except ClientError as e :
121
+ print (f"Error uploading file to S3: { e } " )
122
+ return None
123
+
63
124
transcribe_client = boto3 .client ('transcribe' ,
64
125
region_name = os .getenv ('AWS_REGION' ), # AWS_REGION,
65
126
aws_access_key_id = os .getenv ('AWS_ACCESS_KEY' ),
@@ -327,43 +388,15 @@ async def webhook(request: Request):
327
388
logger .info (f"{ mobile } sent video { video_filename } " )
328
389
manish .send_message (f"Video: { video_filename } " , mobile )
329
390
elif message_type == "audio" :
330
- try :
331
- audio = manish .get_audio (data )
332
- audio_id , mime_type = audio ["id" ], audio ["mime_type" ]
333
- audio_url = manish .query_media_url (audio_id )
334
- ogg_filename = manish .download_media (audio_url , mime_type )
335
- logger .info (f"{ mobile } sent audio { ogg_filename } " )
336
-
337
- # Convert OGG to MP3
338
- mp3_filename = await convert_ogg_to_mp3 (ogg_filename )
339
- logger .info (f"Converted { ogg_filename } to MP3: { mp3_filename } " )
340
-
341
- # Determine the sample rate (you might need to extract this from the audio file)
342
- sample_rate = 16000 # Example: 16kHz
343
-
344
- # Stream the MP3 file to Amazon Transcribe
345
- transcription = ""
346
- with open (mp3_filename , 'rb' ) as audio_file :
347
- async for event in stream_audio_to_transcribe (audio_file , sample_rate ):
348
- if event and 'TranscriptEvent' in event :
349
- for result in event ['TranscriptEvent' ]['Transcript' ]['Results' ]:
350
- if not result ['IsPartial' ]:
351
- transcription += result ['Alternatives' ][0 ]['Transcript' ] + " "
352
-
353
- # Send the transcription back to the user
391
+ audio_filename , s3_audio_url , transcription = process_audio_message (data , mobile , manish )
392
+ if audio_filename and s3_audio_url :
393
+ # Process the transcription or perform other tasks
354
394
if transcription :
355
395
manish .send_message (f"Here's the transcription of your audio message:\n \n { transcription .strip ()} " , mobile )
356
396
else :
357
397
manish .send_message ("I'm sorry, but I couldn't transcribe your audio message. It might be too short or unclear." , mobile )
358
-
359
- # Clean up the files
360
- os .unlink (ogg_filename )
361
- os .unlink (mp3_filename )
362
-
363
- except Exception as e :
364
- logger .error (f"Error processing audio: { str (e )} " )
365
- manish .send_message ("I'm sorry, but I encountered an error while processing your audio message. Please try again later." , mobile )
366
-
398
+ else :
399
+ logger .error ("Failed to process audio message" )
367
400
elif message_type == "file" :
368
401
file = manish .get_file (data )
369
402
logger .info (file )
@@ -451,4 +484,4 @@ async def verify(request: Request):
451
484
452
485
if __name__ == "__main__" :
453
486
import uvicorn
454
- uvicorn .run (app , host = "0.0.0.0" , port = 8000 , log_level = "info" )
487
+ uvicorn .run (app , host = "0.0.0.0" , port = 8000 , log_level = "info" )
0 commit comments