qqwjq1981 commited on
Commit
2b22edd
·
verified ·
1 Parent(s): a6cf7f8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -23
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
- def translate_text(text, target_language):
33
- """
34
- Translates the given text into the specified target language.
35
-
36
- Args:
37
- text (str): The text to translate.
38
- target_language (str): The target language code (e.g., 'es' for Spanish, 'fr' for French).
 
 
 
39
 
40
- Returns:
41
- str: Translated text if successful, otherwise None.
42
- """
43
- try:
44
- # Dynamically construct the model ID
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):