Spaces:
Sleeping
Sleeping
File size: 17,191 Bytes
6123728 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 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 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 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 | # audio_processor.py - Enhanced with AI Translation Support
import os
from dotenv import load_dotenv
import tempfile
from typing import List, Dict, Optional, Tuple
import json
import traceback
# --- DEFINITIVE NUMBA FIX ---
# This MUST be done BEFORE importing librosa
os.environ["NUMBA_CACHE_DIR"] = "/tmp"
# Now, import librosa safely
import librosa
# --- END OF FIX ---
import google.generativeai as genai
from translator import AITranslator
import requests
from google.api_core import exceptions as google_exceptions
class AudioProcessor:
def __init__(self):
self.translator = None
self.init_error = None
self._initialize_translator()
def _initialize_translator(self):
"""Initialize AI translator for multi-language support"""
try:
self.translator = AITranslator()
if self.translator.init_error:
print(f"--- WARNING: Translator has initialization error: {self.translator.init_error} ---")
except Exception as e:
print(f"--- WARNING: Translator initialization failed: {str(e)} ---")
self.translator = None
def transcribe_audio(self, audio_file_path: str) -> Tuple[Optional[str], Optional[str]]:
"""
Transcribes audio. Returns (text, error_message).
Uses Gemini first (if available), then falls back to Groq Whisper.
"""
if not os.path.exists(audio_file_path):
return None, f"--- ERROR: Audio file for transcription not found at: {audio_file_path} ---"
# Try Gemini first if available
gemini_err = None
try:
if self.translator and self.translator.model:
audio_file = genai.upload_file(path=audio_file_path)
prompt = (
"You are an ASR system. Transcribe the audio accurately. "
"Auto-detect the spoken language and return ONLY the verbatim transcript in that same language. "
"Do not translate. Do not add labels or timestamps."
)
response = self.translator.model.generate_content([prompt, audio_file])
if response and hasattr(response, 'text') and response.text:
return response.text.strip(), None
else:
gemini_err = "--- WARNING: Gemini returned an empty response for transcription. ---"
except google_exceptions.ResourceExhausted:
gemini_err = "--- QUOTA ERROR: You have exceeded the daily free usage limit for the AI service. Please wait for your quota to reset (usually within 24 hours) or upgrade your Google AI plan. ---"
except Exception:
gemini_err = f"--- FATAL ERROR during Gemini transcription: {traceback.format_exc()} ---"
# Fallback: Groq Whisper
text, groq_err = self._transcribe_with_groq(audio_file_path)
if text:
return text, None
# If all failed
combined_err = groq_err or gemini_err or "--- ERROR: No transcription provider available. ---"
return None, combined_err
def _transcribe_with_groq(self, audio_file_path: str) -> Tuple[Optional[str], Optional[str]]:
"""Transcribe using Groq Whisper-compatible endpoint. Returns (text, error)."""
try:
load_dotenv()
groq_key = os.getenv("GROQ_API_KEY")
if not groq_key:
return None, "--- ERROR: GROQ_API_KEY not set. ---"
model = os.getenv("GROQ_WHISPER_MODEL", "whisper-large-v3")
url = "https://api.groq.com/openai/v1/audio/transcriptions"
headers = {"Authorization": f"Bearer {groq_key}"}
# Guess mime type by extension
filename = os.path.basename(audio_file_path)
mime = "audio/wav"
if filename.lower().endswith(".mp3"):
mime = "audio/mpeg"
elif filename.lower().endswith(".m4a"):
mime = "audio/mp4"
data = {
"model": model,
"response_format": "json",
}
with open(audio_file_path, "rb") as f:
files = {"file": (filename, f, mime)}
resp = requests.post(url, headers=headers, files=files, data=data, timeout=60)
if not resp.ok:
try:
err = resp.json()
except Exception:
err = {"error": resp.text}
return None, f"--- ERROR: Groq transcription error {resp.status_code}: {err} ---"
out = resp.json()
text = out.get("text")
if not text:
return None, "--- ERROR: Groq transcription returned no text. ---"
return text.strip(), None
except Exception:
return None, f"--- FATAL ERROR during Groq transcription: {traceback.format_exc()} ---"
def get_audio_duration(self, audio_file_path: str) -> Tuple[Optional[float], Optional[str]]:
"""
Gets audio duration. Returns (duration, error_message).
"""
try:
if not os.path.exists(audio_file_path):
return None, f"--- ERROR: Audio file for duration not found at: {audio_file_path} ---"
duration = librosa.get_duration(path=audio_file_path)
if duration is None or duration < 0.1:
return None, f"--- ERROR: librosa returned an invalid duration: {duration}s ---"
return duration, None
except Exception as e:
error_msg = f"--- FATAL ERROR getting audio duration with librosa: {traceback.format_exc()} ---"
return None, error_msg
def get_word_timestamps(self, audio_file_path: str) -> Tuple[List[Dict], List[str]]:
"""
Generates timestamps. Returns (timestamps, log_messages).
"""
logs = ["--- INFO: Starting get_word_timestamps... ---"]
transcription, error = self.transcribe_audio(audio_file_path)
if error:
logs.append(error)
return [], logs
logs.append(f"--- DEBUG: Transcription successful. Text: '{transcription[:50]}...'")
audio_duration, error = self.get_audio_duration(audio_file_path)
if error:
logs.append(error)
return [], logs
logs.append(f"--- DEBUG: Audio duration successful. Duration: {audio_duration:.2f}s")
words = transcription.split()
if not words:
logs.append("--- WARNING: Transcription resulted in zero words. ---")
return [], logs
logs.append(f"--- INFO: Distributing {len(words)} words across the duration. ---")
word_timestamps = []
total_words = len(words)
usable_duration = max(0, audio_duration - 1.0)
for i, word in enumerate(words):
start_time = 0.5 + (i * (usable_duration / total_words))
end_time = 0.5 + ((i + 1) * (usable_duration / total_words))
word_timestamps.append({'word': word.strip(), 'start': round(start_time, 3), 'end': round(end_time, 3)})
logs.append(f"--- SUCCESS: Generated {len(word_timestamps)} word timestamps. ---")
return word_timestamps, logs
def get_word_timestamps_with_translation(self, audio_file_path: str, target_language: str = 'ar') -> Tuple[Dict, List[str]]:
"""
Enhanced function that provides both transcription and translation
Args:
audio_file_path: Path to audio file
target_language: Target language for translation ('ar' for Arabic)
Returns:
Tuple of (result_dict, log_messages)
result_dict contains: {
'original_text': str,
'translated_text': str,
'word_timestamps': List[Dict],
'translated_timestamps': List[Dict],
'language_detected': str,
'target_language': str
}
"""
logs = ["--- INFO: Starting enhanced transcription with translation... ---"]
# Get original transcription and timestamps
word_timestamps, transcription_logs = self.get_word_timestamps(audio_file_path)
logs.extend(transcription_logs)
if not word_timestamps:
# Fallback: try plain transcription (Gemini → Groq) then synthesize timestamps
logs.append("--- INFO: Falling back to plain transcription because timestamps are empty. ---")
plain_text, err = self.transcribe_audio(audio_file_path)
if not plain_text:
logs.append(err or "--- ERROR: Plain transcription fallback failed ---")
return {}, logs
logs.append("--- SUCCESS: Plain transcription fallback succeeded. ---")
# Synthesize naive word-level timestamps across duration
try:
duration, derr = self.get_audio_duration(audio_file_path)
if derr:
logs.append(derr)
duration = 0.0
words = plain_text.split()
if not words:
logs.append("--- WARNING: Fallback transcription produced zero words. ---")
return {}, logs
if duration and duration > 0.1:
usable_duration = max(0, duration - 1.0)
start_offset = 0.5
else:
# If duration not available, assume ~0.4s per word
usable_duration = 0.4 * max(1, len(words))
start_offset = 0.0
word_timestamps = []
total_words = len(words)
for i, w in enumerate(words):
start_time = start_offset + (i * (usable_duration / total_words))
end_time = start_offset + ((i + 1) * (usable_duration / total_words))
word_timestamps.append({'word': w.strip(), 'start': round(start_time, 3), 'end': round(end_time, 3)})
logs.append(f"--- INFO: Synthesized {len(word_timestamps)} timestamps from fallback transcript. ---")
except Exception:
logs.append(f"--- FATAL ERROR synthesizing timestamps: {traceback.format_exc()} ---")
return {}, logs
# Extract original text
original_text = " ".join([d['word'] for d in word_timestamps])
logs.append(f"--- INFO: Original transcription: '{original_text[:50]}...' ---")
# Initialize result dictionary
result = {
'original_text': original_text,
'translated_text': '',
'word_timestamps': word_timestamps,
'translated_timestamps': [],
'language_detected': 'unknown',
'target_language': target_language,
'translation_success': False
}
# Check if translator is available
if not self.translator:
logs.append("--- WARNING: Translator not available, returning original text only ---")
result['translated_text'] = original_text
return result, logs
try:
# Translate the text
translated_text, translation_error = self.translator.translate_text(
original_text,
target_language=target_language
)
if translated_text:
result['translated_text'] = translated_text
result['translation_success'] = True
logs.append(f"--- SUCCESS: Translation completed: '{translated_text[:50]}...' ---")
# Create translated timestamps by mapping words
translated_timestamps = self._create_translated_timestamps(
word_timestamps,
original_text,
translated_text
)
result['translated_timestamps'] = translated_timestamps
logs.append(f"--- INFO: Created {len(translated_timestamps)} translated timestamps ---")
else:
logs.append(f"--- ERROR: Translation failed: {translation_error} ---")
result['translated_text'] = original_text # Fallback to original
result['translated_timestamps'] = word_timestamps # Use original timestamps
except Exception as e:
error_msg = f"--- FATAL ERROR during translation process: {traceback.format_exc()} ---"
logs.append(error_msg)
result['translated_text'] = original_text # Fallback
result['translated_timestamps'] = word_timestamps
return result, logs
def _create_translated_timestamps(self, original_timestamps: List[Dict], original_text: str, translated_text: str) -> List[Dict]:
"""
Create timestamps for translated text by proportional mapping
Args:
original_timestamps: Original word timestamps
original_text: Original transcribed text
translated_text: Translated text
Returns:
List of translated word timestamps
"""
try:
translated_words = translated_text.split()
if not translated_words:
return []
# Get total duration from original timestamps
if not original_timestamps:
return []
start_time = original_timestamps[0]['start']
end_time = original_timestamps[-1]['end']
total_duration = end_time - start_time
# Create proportional timestamps for translated words
translated_timestamps = []
word_count = len(translated_words)
for i, word in enumerate(translated_words):
# Calculate proportional timing
word_start = start_time + (i * total_duration / word_count)
word_end = start_time + ((i + 1) * total_duration / word_count)
translated_timestamps.append({
'word': word.strip(),
'start': round(word_start, 3),
'end': round(word_end, 3)
})
return translated_timestamps
except Exception as e:
print(f"--- ERROR creating translated timestamps: {str(e)} ---")
return []
def batch_translate_transcription(self, audio_file_path: str, target_languages: List[str]) -> Tuple[Dict, List[str]]:
"""
Transcribe audio and translate to multiple languages
Args:
audio_file_path: Path to audio file
target_languages: List of target language codes
Returns:
Tuple of (results_dict, log_messages)
"""
logs = ["--- INFO: Starting batch translation process... ---"]
# Get original transcription
word_timestamps, transcription_logs = self.get_word_timestamps(audio_file_path)
logs.extend(transcription_logs)
if not word_timestamps:
return {}, logs
original_text = " ".join([d['word'] for d in word_timestamps])
# Initialize results
results = {
'original': {
'text': original_text,
'timestamps': word_timestamps,
'language': 'detected'
},
'translations': {}
}
# Translate to each target language
if self.translator:
for lang_code in target_languages:
try:
translated_text, error = self.translator.translate_text(original_text, lang_code)
if translated_text:
translated_timestamps = self._create_translated_timestamps(
word_timestamps, original_text, translated_text
)
results['translations'][lang_code] = {
'text': translated_text,
'timestamps': translated_timestamps,
'success': True
}
logs.append(f"--- SUCCESS: Translation to {lang_code} completed ---")
else:
results['translations'][lang_code] = {
'text': original_text,
'timestamps': word_timestamps,
'success': False,
'error': error
}
logs.append(f"--- ERROR: Translation to {lang_code} failed: {error} ---")
except Exception as e:
logs.append(f"--- FATAL ERROR translating to {lang_code}: {str(e)} ---")
else:
logs.append("--- WARNING: Translator not available for batch translation ---")
return results, logs
|