Update app.py
Browse files
app.py
CHANGED
|
@@ -29,30 +29,22 @@ def transcribe_audio_from_file(file_path):
|
|
| 29 |
# Initialize the translation pipeline
|
| 30 |
translation_pipeline = pipeline("translation", model="Helsinki-NLP/opus-mt-en-{target_language}")
|
| 31 |
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
|
|
|
|
|
|
|
|
|
| 39 |
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
model_id = f"Helsinki-NLP/opus-mt-en-{target_language}"
|
| 46 |
-
|
| 47 |
-
# Initialize the translation pipeline for the target language
|
| 48 |
-
translator = pipeline("translation", model=model_id)
|
| 49 |
-
translation = translator(text, max_length=1000)
|
| 50 |
-
translated_text = translation[0]["translation_text"]
|
| 51 |
-
logger.debug(f"Translation to {target_language}: {translated_text}")
|
| 52 |
-
return translated_text
|
| 53 |
-
except Exception as e:
|
| 54 |
-
logger.error(f"An error occurred during translation: {e}")
|
| 55 |
-
return None
|
| 56 |
|
| 57 |
# Mock functions for platform actions and analytics
|
| 58 |
def mock_post_to_platform(platform, content_title):
|
|
|
|
| 29 |
# Initialize the translation pipeline
|
| 30 |
translation_pipeline = pipeline("translation", model="Helsinki-NLP/opus-mt-en-{target_language}")
|
| 31 |
|
| 32 |
+
# Function to get the appropriate translation model based on target language
|
| 33 |
+
def get_translation_model(target_language):
|
| 34 |
+
# Map of target languages to their corresponding model names
|
| 35 |
+
model_map = {
|
| 36 |
+
"es": "Helsinki-NLP/opus-mt-en-es", # English to Spanish
|
| 37 |
+
"fr": "Helsinki-NLP/opus-mt-en-fr", # English to French
|
| 38 |
+
"zh": "Helsinki-NLP/opus-mt-en-zh", # English to Chinese
|
| 39 |
+
# Add more languages as needed
|
| 40 |
+
}
|
| 41 |
+
return model_map.get(target_language, "Helsinki-NLP/opus-mt-en-fr") # Default to French if not found
|
| 42 |
|
| 43 |
+
# Example usage in your application
|
| 44 |
+
def translate_text(text, target_language):
|
| 45 |
+
translation_model_id = get_translation_model(target_language)
|
| 46 |
+
translator = pipeline("translation", model=translation_model_id)
|
| 47 |
+
return translator(text)[0]['translation_text']
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
|
| 49 |
# Mock functions for platform actions and analytics
|
| 50 |
def mock_post_to_platform(platform, content_title):
|