denizaybey commited on
Commit
a8189eb
·
1 Parent(s): 7b41181

Refactor: Switch to Modal framework for audio processing and storage

Browse files
Files changed (1) hide show
  1. app.py +39 -67
app.py CHANGED
@@ -1,8 +1,17 @@
1
- import gradio as gr
2
- import requests
 
 
 
 
 
 
 
3
  import wave
 
4
  import contextlib
5
- import os
 
6
 
7
  def process_audio(original_audio_path, dubbed_audio_path, email, company_name, tolerance):
8
  """
@@ -10,6 +19,11 @@ def process_audio(original_audio_path, dubbed_audio_path, email, company_name, t
10
  file upload to presigned URLs, and triggering the processing.
11
  """
12
  # 1. Check the duration of both audio files.
 
 
 
 
 
13
  try:
14
  with contextlib.closing(wave.open(original_audio_path, 'r')) as f:
15
  frames = f.getnframes()
@@ -26,71 +40,29 @@ def process_audio(original_audio_path, dubbed_audio_path, email, company_name, t
26
 
27
  except Exception as e:
28
  return f"Error reading audio files: {e}"
29
-
30
- # --- ACTION REQUIRED ---
31
- # Please replace the following placeholder URLs with your actual API endpoints.
32
- presigned_url_endpoint = "https://your-api.com/get-presigned-urls" # TODO: Change URL
33
- processing_endpoint = "https://your-api.com/trigger-processing" # TODO: Change URL
34
- # --------------------------
35
-
36
- # 2.1. Get presigned URLs from your endpoint. # TODO: Change Payload
37
- payload = {
38
- "files": [
39
- {"name": os.path.basename(original_audio_path), "type": "audio/wav"},
40
- {"name": os.path.basename(dubbed_audio_path), "type": "audio/wav"}
41
- ]
42
- }
43
- try:
44
- print(f"Requesting presigned URLs from: {presigned_url_endpoint}")
45
- response = requests.post(presigned_url_endpoint, json=payload)
46
- response.raise_for_status() # Raise an exception for bad status codes
47
- presigned_data = response.json()
48
-
49
- # IMPORTANT: Adjust the following lines based on the actual JSON response
50
- # structure of your presigned URL endpoint.
51
- # This example assumes a response like:
52
- # {"original_url": "...", "dubbed_url": "..."}
53
- original_upload_url = presigned_data['original_url']
54
- dubbed_upload_url = presigned_data['dubbed_url']
55
-
56
- except requests.exceptions.RequestException as e:
57
- return f"Error getting presigned URLs: {e}"
58
- except KeyError:
59
- return "Error: Could not parse the presigned URL response. Please check the JSON structure."
60
-
61
- # 2.2. Upload the audio files to the presigned URLs. # TODO: Check for PUT accuracy
62
  try:
63
- print(f"Uploading original file to: {original_upload_url}")
64
- with open(original_audio_path, 'rb') as f:
65
- upload_response = requests.put(original_upload_url, data=f)
66
- upload_response.raise_for_status()
67
-
68
- print(f"Uploading dubbed file to: {dubbed_upload_url}")
69
- with open(dubbed_audio_path, 'rb') as f:
70
- upload_response = requests.put(dubbed_upload_url, data=f)
71
- upload_response.raise_for_status()
72
-
73
- except requests.exceptions.RequestException as e:
74
- return f"Error uploading files: {e}"
75
-
76
- # 3. Call the endpoint to trigger the processing.
77
- processing_payload = { # TODO: Change Payload
78
- "email": email,
79
- "company_name": company_name,
80
- "tolerance": tolerance,
81
- # The keys here ('original_file', 'dubbed_file') should match what your
82
- # processing API expects.
83
- "original_file": original_upload_url,
84
- "dubbed_file": dubbed_upload_url
85
- }
86
  try:
87
- print(f"Triggering processing at: {processing_endpoint}")
88
- processing_response = requests.post(processing_endpoint, json=processing_payload)
89
- processing_response.raise_for_status()
90
- # 4. Show the response as output.
91
- return f"Processing triggered successfully. Server response: {processing_response.text}"
92
- except requests.exceptions.RequestException as e:
93
- return f"Error triggering processing: {e}"
 
 
 
 
 
94
 
95
 
96
  # Create the Gradio interface for the application.
@@ -112,4 +84,4 @@ demo = gr.Interface(
112
  if __name__ == "__main__":
113
  # To run this file locally, you'll need to install gradio and requests:
114
  # pip install gradio requests
115
- demo.launch()
 
1
+ ## ENVIRONMENT VARIABLES
2
+ # MODAL_VOLUME
3
+ # MODAL_TOKEN_ID
4
+ # MODAL_ENVIRONMENT
5
+ # MODAL_TOKEN_SECRET
6
+
7
+
8
+ import os
9
+ import time
10
  import wave
11
+ import modal
12
  import contextlib
13
+ import gradio as gr
14
+
15
 
16
  def process_audio(original_audio_path, dubbed_audio_path, email, company_name, tolerance):
17
  """
 
19
  file upload to presigned URLs, and triggering the processing.
20
  """
21
  # 1. Check the duration of both audio files.
22
+ modal_token_id = os.environ['MODAL_TOKEN_ID']
23
+ modal_token_secret = os.environ['MODAL_TOKEN_SECRET']
24
+ modal_environment = os.environ['MODAL_ENVIRONMENT']
25
+ modal_volume = os.environ['MODAL_VOLUME']
26
+ processing_id = str(int(time.time()))
27
  try:
28
  with contextlib.closing(wave.open(original_audio_path, 'r')) as f:
29
  frames = f.getnframes()
 
40
 
41
  except Exception as e:
42
  return f"Error reading audio files: {e}"
43
+ # 2. Upload Audio Files to Modal Storage
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
44
  try:
45
+ bsodtv_storage = modal.Volume.from_name(modal_volume)
46
+ with bsodtv_storage.batch_upload() as batch:
47
+ batch.put_file(original_audio_path, "/{}/original_audio.wav".format(processing_id))
48
+ batch.put_file(dubbed_audio_path, "/{}/original_audio.wav".format(processing_id))
49
+ bsodtv_storage.commit()
50
+ except:
51
+ return "Error uploading audio files to Cloud Storage."
52
+ # 3. Call modal to trigger processing
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
53
  try:
54
+ waveform_matching_function = modal.Function.from_name("Waveform-Matching", "reception_handler")
55
+ waveform_matching_function.spawn(
56
+ processing_id=processing_id,
57
+ original_file="/{}/original_audio.wav".format(processing_id),
58
+ dubbed_file="/{}/original_audio.wav".format(processing_id),
59
+ email=email,
60
+ company_name=company_name,
61
+ tolerance_percentage=tolerance
62
+ )
63
+ except:
64
+ return "Error calling Outpost to trigger processing."
65
+ return "Processing started. Results will be emailed to you shortly."
66
 
67
 
68
  # Create the Gradio interface for the application.
 
84
  if __name__ == "__main__":
85
  # To run this file locally, you'll need to install gradio and requests:
86
  # pip install gradio requests
87
+ demo.launch()