Spaces:
Sleeping
Sleeping
RillSvai commited on
Commit ·
7140fa1
1
Parent(s): 896993c
update
Browse files
app.py
CHANGED
|
@@ -68,10 +68,10 @@ except Exception as e:
|
|
| 68 |
def is_valid_url(url):
|
| 69 |
"""
|
| 70 |
Validates the given URL.
|
| 71 |
-
|
| 72 |
Parameters:
|
| 73 |
- url: The URL string to validate.
|
| 74 |
-
|
| 75 |
Returns:
|
| 76 |
- True if the URL is valid, False otherwise.
|
| 77 |
"""
|
|
@@ -81,12 +81,12 @@ def is_valid_url(url):
|
|
| 81 |
def extract_audio_segment(audio_path, start_time, end_time):
|
| 82 |
"""
|
| 83 |
Extracts a segment from the audio file.
|
| 84 |
-
|
| 85 |
Parameters:
|
| 86 |
- audio_path: Path to the audio file.
|
| 87 |
- start_time: Start time in seconds.
|
| 88 |
- end_time: End time in seconds.
|
| 89 |
-
|
| 90 |
Returns:
|
| 91 |
- BytesIO object containing the audio segment in WAV format.
|
| 92 |
"""
|
|
@@ -105,11 +105,11 @@ def extract_audio_segment(audio_path, start_time, end_time):
|
|
| 105 |
def transcribe_segment(audio_segment, method='gradio'):
|
| 106 |
"""
|
| 107 |
Transcribes an audio segment using the specified method.
|
| 108 |
-
|
| 109 |
Parameters:
|
| 110 |
- audio_segment: BytesIO object containing the audio segment.
|
| 111 |
- method: 'gradio' or 'openai'
|
| 112 |
-
|
| 113 |
Returns:
|
| 114 |
- Transcribed text as a string.
|
| 115 |
"""
|
|
@@ -159,13 +159,13 @@ def process_audio(audio_path, resourceId, webhookUrl, transcription_method='grad
|
|
| 159 |
"""
|
| 160 |
Processes the audio file by performing speaker diarization and transcription.
|
| 161 |
Sends the results to the specified webhook URL along with the resourceId.
|
| 162 |
-
|
| 163 |
Parameters:
|
| 164 |
- audio_path: Path to the uploaded audio file.
|
| 165 |
- resourceId: Identifier for the resource.
|
| 166 |
- webhookUrl: URL to send the processed results.
|
| 167 |
- transcription_method: Method to use for transcription ('gradio' or 'openai')
|
| 168 |
-
|
| 169 |
Returns:
|
| 170 |
- Confirmation message indicating the status of the operation.
|
| 171 |
"""
|
|
@@ -173,76 +173,79 @@ def process_audio(audio_path, resourceId, webhookUrl, transcription_method='grad
|
|
| 173 |
unique_id = uuid.uuid4().hex
|
| 174 |
audio_extension = os.path.splitext(audio_path)[1] if audio_path else '.wav'
|
| 175 |
temp_audio_path = f"temp_audio_{unique_id}{audio_extension}"
|
| 176 |
-
|
| 177 |
try:
|
| 178 |
# Validate webhook URL
|
| 179 |
if not is_valid_url(webhookUrl):
|
| 180 |
error_msg = f"Invalid webhook URL provided: {webhookUrl}"
|
| 181 |
logger.error(error_msg)
|
| 182 |
return error_msg
|
| 183 |
-
|
| 184 |
-
#
|
| 185 |
shutil.copy(audio_path, temp_audio_path)
|
| 186 |
logger.info(f"Copied uploaded audio to {temp_audio_path}.")
|
| 187 |
-
|
| 188 |
-
#
|
| 189 |
-
|
| 190 |
-
|
| 191 |
-
|
| 192 |
-
|
| 193 |
-
|
| 194 |
-
|
| 195 |
-
|
| 196 |
-
|
| 197 |
-
|
| 198 |
-
|
| 199 |
-
|
| 200 |
-
|
| 201 |
-
|
| 202 |
-
|
| 203 |
-
|
| 204 |
-
|
| 205 |
-
|
| 206 |
-
|
| 207 |
-
|
| 208 |
-
|
| 209 |
-
|
| 210 |
-
)
|
| 211 |
-
|
| 212 |
-
transcribed_text
|
| 213 |
-
|
| 214 |
-
|
| 215 |
-
|
| 216 |
-
|
| 217 |
-
|
| 218 |
-
|
| 219 |
-
|
| 220 |
-
|
| 221 |
-
|
| 222 |
-
|
| 223 |
-
"
|
| 224 |
-
|
| 225 |
-
|
| 226 |
-
|
| 227 |
-
|
| 228 |
-
|
| 229 |
-
|
| 230 |
-
|
| 231 |
-
"
|
| 232 |
-
|
| 233 |
-
|
| 234 |
-
|
| 235 |
-
|
| 236 |
-
|
| 237 |
-
|
| 238 |
-
|
| 239 |
-
|
| 240 |
-
|
| 241 |
-
|
| 242 |
-
|
| 243 |
-
|
| 244 |
-
|
| 245 |
-
|
|
|
|
|
|
|
|
|
|
| 246 |
except requests.exceptions.RequestException as req_err:
|
| 247 |
error_message = f"Failed to send data to webhook: {req_err}"
|
| 248 |
logger.error(error_message, exc_info=True)
|
|
|
|
| 68 |
def is_valid_url(url):
|
| 69 |
"""
|
| 70 |
Validates the given URL.
|
| 71 |
+
|
| 72 |
Parameters:
|
| 73 |
- url: The URL string to validate.
|
| 74 |
+
|
| 75 |
Returns:
|
| 76 |
- True if the URL is valid, False otherwise.
|
| 77 |
"""
|
|
|
|
| 81 |
def extract_audio_segment(audio_path, start_time, end_time):
|
| 82 |
"""
|
| 83 |
Extracts a segment from the audio file.
|
| 84 |
+
|
| 85 |
Parameters:
|
| 86 |
- audio_path: Path to the audio file.
|
| 87 |
- start_time: Start time in seconds.
|
| 88 |
- end_time: End time in seconds.
|
| 89 |
+
|
| 90 |
Returns:
|
| 91 |
- BytesIO object containing the audio segment in WAV format.
|
| 92 |
"""
|
|
|
|
| 105 |
def transcribe_segment(audio_segment, method='gradio'):
|
| 106 |
"""
|
| 107 |
Transcribes an audio segment using the specified method.
|
| 108 |
+
|
| 109 |
Parameters:
|
| 110 |
- audio_segment: BytesIO object containing the audio segment.
|
| 111 |
- method: 'gradio' or 'openai'
|
| 112 |
+
|
| 113 |
Returns:
|
| 114 |
- Transcribed text as a string.
|
| 115 |
"""
|
|
|
|
| 159 |
"""
|
| 160 |
Processes the audio file by performing speaker diarization and transcription.
|
| 161 |
Sends the results to the specified webhook URL along with the resourceId.
|
| 162 |
+
|
| 163 |
Parameters:
|
| 164 |
- audio_path: Path to the uploaded audio file.
|
| 165 |
- resourceId: Identifier for the resource.
|
| 166 |
- webhookUrl: URL to send the processed results.
|
| 167 |
- transcription_method: Method to use for transcription ('gradio' or 'openai')
|
| 168 |
+
|
| 169 |
Returns:
|
| 170 |
- Confirmation message indicating the status of the operation.
|
| 171 |
"""
|
|
|
|
| 173 |
unique_id = uuid.uuid4().hex
|
| 174 |
audio_extension = os.path.splitext(audio_path)[1] if audio_path else '.wav'
|
| 175 |
temp_audio_path = f"temp_audio_{unique_id}{audio_extension}"
|
| 176 |
+
|
| 177 |
try:
|
| 178 |
# Validate webhook URL
|
| 179 |
if not is_valid_url(webhookUrl):
|
| 180 |
error_msg = f"Invalid webhook URL provided: {webhookUrl}"
|
| 181 |
logger.error(error_msg)
|
| 182 |
return error_msg
|
| 183 |
+
|
| 184 |
+
# Generate unique temporary filename and copy the uploaded audio file
|
| 185 |
shutil.copy(audio_path, temp_audio_path)
|
| 186 |
logger.info(f"Copied uploaded audio to {temp_audio_path}.")
|
| 187 |
+
|
| 188 |
+
# Initialize progress
|
| 189 |
+
progress = gr.Progress(steps=4)
|
| 190 |
+
|
| 191 |
+
with progress:
|
| 192 |
+
# Step 1: Speaker Diarization
|
| 193 |
+
progress(1, "Performing speaker diarization...")
|
| 194 |
+
diarization_start = time.time()
|
| 195 |
+
diarization_result = diarization_pipeline(temp_audio_path)
|
| 196 |
+
diarization_time = time.time() - diarization_start
|
| 197 |
+
logger.info(f"Speaker diarization completed in {diarization_time:.2f} seconds.")
|
| 198 |
+
|
| 199 |
+
# Step 2: Parse diarization results
|
| 200 |
+
speaker_segments = []
|
| 201 |
+
for turn, _, speaker in diarization_result.itertracks(yield_label=True):
|
| 202 |
+
speaker_segments.append({
|
| 203 |
+
"speaker": speaker,
|
| 204 |
+
"start": turn.start,
|
| 205 |
+
"end": turn.end
|
| 206 |
+
})
|
| 207 |
+
logger.debug(f"Speaker segments: {speaker_segments}")
|
| 208 |
+
|
| 209 |
+
# Step 3: Transcription
|
| 210 |
+
progress(2, "Transcribing audio segments...")
|
| 211 |
+
transcription_start = time.time()
|
| 212 |
+
transcribed_text = ""
|
| 213 |
+
for segment in speaker_segments:
|
| 214 |
+
segment_audio = extract_audio_segment(
|
| 215 |
+
temp_audio_path, segment["start"], segment["end"]
|
| 216 |
+
)
|
| 217 |
+
transcription = transcribe_segment(segment_audio, method=transcription_method)
|
| 218 |
+
transcribed_text += f"Speaker {segment['speaker']} [{segment['start']:.2f}s - {segment['end']:.2f}s]: {transcription}\n"
|
| 219 |
+
transcription_time = time.time() - transcription_start
|
| 220 |
+
logger.info(f"Transcription completed in {transcription_time:.2f} seconds.")
|
| 221 |
+
|
| 222 |
+
# Step 4: Sending to Webhook
|
| 223 |
+
progress(3, "Sending results to webhook...")
|
| 224 |
+
total_execution_time = time.time() - start_time_total
|
| 225 |
+
metrics = {
|
| 226 |
+
"total_execution_time_sec": total_execution_time,
|
| 227 |
+
"audio_conversion_time_sec": 0.1, # Approximate value
|
| 228 |
+
"diarization_time_sec": diarization_time,
|
| 229 |
+
"transcription_time_sec": transcription_time
|
| 230 |
+
}
|
| 231 |
+
logger.debug(f"Metrics: {metrics}")
|
| 232 |
+
|
| 233 |
+
result_payload = {
|
| 234 |
+
"resourceId": resourceId,
|
| 235 |
+
"transcribed_text": transcribed_text.strip(),
|
| 236 |
+
"speaker_labels": list({f"Speaker {seg['speaker']}" for seg in speaker_segments}),
|
| 237 |
+
"metrics": metrics
|
| 238 |
+
}
|
| 239 |
+
logger.info(f"Prepared result payload: {result_payload}")
|
| 240 |
+
|
| 241 |
+
headers = {"Content-Type": "application/json"}
|
| 242 |
+
response = requests.post(webhookUrl, json=result_payload, headers=headers)
|
| 243 |
+
response.raise_for_status() # Raise an error for bad status codes
|
| 244 |
+
logger.info(f"Successfully sent data to webhook: {webhookUrl}")
|
| 245 |
+
progress(4, "Results sent successfully.")
|
| 246 |
+
|
| 247 |
+
return "Processing complete. Results have been sent to the webhook."
|
| 248 |
+
|
| 249 |
except requests.exceptions.RequestException as req_err:
|
| 250 |
error_message = f"Failed to send data to webhook: {req_err}"
|
| 251 |
logger.error(error_message, exc_info=True)
|