Bildad commited on
Commit
312e880
·
verified ·
1 Parent(s): 0692233

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -22
app.py CHANGED
@@ -13,46 +13,37 @@ class UnifiedTranslator:
13
  self.model = pipeline("translation", model=model_name)
14
  self.phrase_mapping = phrase_mapping
15
 
16
- def is_name(self, word):
17
- # A heuristic: a word is considered a name if it starts with an uppercase letter
18
- # and is not one of the common lowercase words
19
- return word[0].isupper()
20
-
21
  def translate(self, text):
22
- # Normalize text and strip extra spaces
23
- text_clean = text.strip()
24
-
25
  # Debugging output
26
- print(f"Input text: {text_clean}")
27
 
28
  # Check if the text matches any pattern in the phrase_mapping (English to Swahili)
29
  for pattern, translation in self.phrase_mapping.items():
30
  try:
31
  # Use regex to match the pattern with placeholders
32
  pattern_regex = re.compile(
33
- re.escape(pattern).replace(r"\{name\}", r"([\w'-]+)").strip(),
34
  re.IGNORECASE
35
  )
36
  print(f"Checking pattern: {pattern}")
37
- match = pattern_regex.fullmatch(text_clean)
38
  if match:
39
  print(f"Match found: {match.group(0)}")
40
- matched_word = match.group(1)
41
-
42
- # Check if the matched word is a name
43
- if '{name}' in pattern and self.is_name(matched_word):
44
- # If it's a name, replace the placeholder with the actual value
45
- return translation.format(name=matched_word)
46
  else:
47
- # If it's not a name, replace the placeholder with the matched word directly
48
- return translation.replace('{name}', matched_word)
49
  except re.error as e:
50
  print(f"Regex error with pattern {pattern}: {e}")
51
 
52
  # Fallback to model translation if no pattern matches
53
  try:
54
- print(f"Fallback to model translation for text: {text_clean}")
55
- translation = self.model(text_clean)[0]
56
  return translation['translation_text']
57
  except Exception as e:
58
  print(f"Model translation error: {e}")
@@ -74,4 +65,4 @@ iface = gr.Interface(
74
  )
75
 
76
  # Launch the interface
77
- iface.launch(share=True)
 
13
  self.model = pipeline("translation", model=model_name)
14
  self.phrase_mapping = phrase_mapping
15
 
 
 
 
 
 
16
  def translate(self, text):
17
+ # Normalize text to lowercase and strip extra spaces
18
+ text_lower = text.lower().strip()
19
+
20
  # Debugging output
21
+ print(f"Input text: {text_lower}")
22
 
23
  # Check if the text matches any pattern in the phrase_mapping (English to Swahili)
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))
 
 
 
38
  else:
39
+ return translation
 
40
  except re.error as e:
41
  print(f"Regex error with pattern {pattern}: {e}")
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:
49
  print(f"Model translation error: {e}")
 
65
  )
66
 
67
  # Launch the interface
68
+ iface.launch(share=True)