Bildad commited on
Commit
9234c46
·
verified ·
1 Parent(s): 269f282

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +4 -2
app.py CHANGED
@@ -24,13 +24,14 @@ class UnifiedTranslator:
24
  for pattern, translation in self.phrase_mapping.items():
25
  try:
26
  # Use regex to match the pattern with placeholders
27
- # Make sure pattern is properly escaped and case insensitive
28
  pattern_regex = re.compile(
29
  re.escape(pattern).replace(r"\{name\}", r"([\w'-]+)").strip(),
30
  re.IGNORECASE
31
  )
 
32
  match = pattern_regex.fullmatch(text_lower)
33
  if match:
 
34
  # Replace the placeholder with the actual value if needed
35
  if '{name}' in pattern:
36
  return translation.format(name=match.group(1))
@@ -41,6 +42,7 @@ class UnifiedTranslator:
41
 
42
  # Fallback to model translation if no pattern matches
43
  try:
 
44
  translation = self.model(text)[0]
45
  return translation['translation_text']
46
  except Exception as e:
@@ -48,7 +50,7 @@ class UnifiedTranslator:
48
  return "Translation error occurred"
49
 
50
  # Initialize the UnifiedTranslator with your model and custom phrases
51
- translator = UnifiedTranslator("Bildad/Swahili-English_Translation", phrase_mapping)
52
 
53
  # Define the Gradio interface
54
  def translate_text(text):
 
24
  for pattern, translation in self.phrase_mapping.items():
25
  try:
26
  # Use regex to match the pattern with placeholders
 
27
  pattern_regex = re.compile(
28
  re.escape(pattern).replace(r"\{name\}", r"([\w'-]+)").strip(),
29
  re.IGNORECASE
30
  )
31
+ print(f"Checking pattern: {pattern}")
32
  match = pattern_regex.fullmatch(text_lower)
33
  if match:
34
+ print(f"Match found: {match.group(0)}")
35
  # Replace the placeholder with the actual value if needed
36
  if '{name}' in pattern:
37
  return translation.format(name=match.group(1))
 
42
 
43
  # Fallback to model translation if no pattern matches
44
  try:
45
+ print(f"Fallback to model translation for text: {text}")
46
  translation = self.model(text)[0]
47
  return translation['translation_text']
48
  except Exception as e:
 
50
  return "Translation error occurred"
51
 
52
  # Initialize the UnifiedTranslator with your model and custom phrases
53
+ translator = UnifiedTranslator("Bildad/English-Swahili_Translation", phrase_mapping)
54
 
55
  # Define the Gradio interface
56
  def translate_text(text):