Bildad commited on
Commit
121d7fd
·
verified ·
1 Parent(s): 4621f79

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -24
app.py CHANGED
@@ -2,44 +2,37 @@ import gradio as gr
2
  from transformers import pipeline
3
  import json
4
  import re
5
- import os
6
 
7
- # Load the phrase mapping from the JSON file
8
  with open('phrase_mappings.json', 'r') as f:
9
  phrase_mapping = json.load(f)
10
 
11
- # Define the UnifiedTranslator class
12
  class UnifiedTranslator:
13
- def __init__(self, model_path, phrase_mapping):
14
- """
15
- model_path: Local folder path to the model (e.g., 'translation_model/')
16
- phrase_mapping: dict of custom phrases
17
- """
18
- if not os.path.exists(model_path):
19
- raise FileNotFoundError(f"Model folder not found: {model_path}")
20
-
21
- # Load translation pipeline from local folder
22
- self.model = pipeline(
23
- "translation",
24
- model=model_path,
25
- tokenizer=model_path
26
- )
27
  self.phrase_mapping = phrase_mapping
28
 
29
  def translate(self, text):
30
  # Normalize text to lowercase and strip extra spaces
31
  text_lower = text.lower().strip()
32
 
33
- # Check custom phrase mappings first
 
 
 
34
  for pattern, translation in self.phrase_mapping.items():
35
  try:
36
- # Convert placeholders to regex
37
  pattern_regex = re.compile(
38
- re.escape(pattern).replace(r"\{name\}", r"([\w'-]+)"),
39
  re.IGNORECASE
40
  )
 
41
  match = pattern_regex.fullmatch(text_lower)
42
  if match:
 
 
43
  if '{name}' in pattern:
44
  return translation.format(name=match.group(1))
45
  else:
@@ -47,16 +40,17 @@ class UnifiedTranslator:
47
  except re.error as e:
48
  print(f"Regex error with pattern {pattern}: {e}")
49
 
50
- # Fallback to model translation
51
  try:
 
52
  translation = self.model(text)[0]
53
  return translation['translation_text']
54
  except Exception as e:
55
  print(f"Model translation error: {e}")
56
  return "Translation error occurred"
57
 
58
- # Initialize the translator with the local model folder
59
- translator = UnifiedTranslator("translation_model", phrase_mapping)
60
 
61
  # Define the Gradio interface
62
  def translate_text(text):
@@ -71,4 +65,4 @@ iface = gr.Interface(
71
  )
72
 
73
  # Launch the interface
74
- iface.launch(share=True)
 
2
  from transformers import pipeline
3
  import json
4
  import re
 
5
 
6
+ # Load the phrase mapping from the JSON file (English to Swahili)
7
  with open('phrase_mappings.json', 'r') as f:
8
  phrase_mapping = json.load(f)
9
 
10
+ # Define the UnifiedTranslator class for English-to-Swahili translation
11
  class UnifiedTranslator:
12
+ def __init__(self, model_name, phrase_mapping):
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:
 
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}")
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):
 
65
  )
66
 
67
  # Launch the interface
68
+ iface.launch(share=True)