Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,3 +1,66 @@
|
|
1 |
import gradio as gr
|
|
|
|
|
|
|
2 |
|
3 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
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 |
+
# 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))
|
37 |
+
else:
|
38 |
+
return translation
|
39 |
+
except re.error as e:
|
40 |
+
print(f"Regex error with pattern {pattern}: {e}")
|
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:
|
47 |
+
print(f"Model translation error: {e}")
|
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):
|
55 |
+
return translator.translate(text)
|
56 |
+
|
57 |
+
iface = gr.Interface(
|
58 |
+
fn=translate_text,
|
59 |
+
inputs="text",
|
60 |
+
outputs="text",
|
61 |
+
title="English to Swahili Translation",
|
62 |
+
description="Translate English to Swahili with custom phrase mappings."
|
63 |
+
)
|
64 |
+
|
65 |
+
# Launch the interface
|
66 |
+
iface.launch(share=True)
|