Spaces:
Building
Building
Update src/main.py
Browse files- src/main.py +12 -2
src/main.py
CHANGED
@@ -4,19 +4,29 @@ from NLP_Spacy_base_translator import NlpSpacyBaseTranslator
|
|
4 |
from flask import Flask, render_template, Response, request
|
5 |
from transformers import pipeline
|
6 |
import torch
|
|
|
7 |
|
8 |
app = Flask(__name__)
|
9 |
app.config['TITLE'] = 'ASL Translator'
|
10 |
|
|
|
|
|
|
|
|
|
11 |
# Force CPU usage
|
12 |
device = torch.device('cpu')
|
13 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
14 |
|
15 |
nlp, dict_docs_spacy = sp.load_spacy_values()
|
16 |
dataset, list_2000_tokens = dg.load_data()
|
17 |
|
18 |
def translate_korean_to_english(text):
|
19 |
-
# Check if input is Korean using Unicode range
|
20 |
if any('\u3131' <= char <= '\u318F' or '\uAC00' <= char <= '\uD7A3' for char in text):
|
21 |
translation = translator(text)[0]['translation_text']
|
22 |
return translation
|
|
|
4 |
from flask import Flask, render_template, Response, request
|
5 |
from transformers import pipeline
|
6 |
import torch
|
7 |
+
import os
|
8 |
|
9 |
app = Flask(__name__)
|
10 |
app.config['TITLE'] = 'ASL Translator'
|
11 |
|
12 |
+
# Set cache directory
|
13 |
+
os.environ['TRANSFORMERS_CACHE'] = '/tmp/transformers_cache'
|
14 |
+
os.makedirs('/tmp/transformers_cache', exist_ok=True)
|
15 |
+
|
16 |
# Force CPU usage
|
17 |
device = torch.device('cpu')
|
18 |
+
os.environ['CUDA_VISIBLE_DEVICES'] = ''
|
19 |
+
|
20 |
+
# Initialize translator with local cache
|
21 |
+
translator = pipeline("translation",
|
22 |
+
model="Helsinki-NLP/opus-mt-ko-en",
|
23 |
+
device=device,
|
24 |
+
model_kwargs={"cache_dir": "/tmp/transformers_cache"})
|
25 |
|
26 |
nlp, dict_docs_spacy = sp.load_spacy_values()
|
27 |
dataset, list_2000_tokens = dg.load_data()
|
28 |
|
29 |
def translate_korean_to_english(text):
|
|
|
30 |
if any('\u3131' <= char <= '\u318F' or '\uAC00' <= char <= '\uD7A3' for char in text):
|
31 |
translation = translator(text)[0]['translation_text']
|
32 |
return translation
|