Spaces:
Running
on
Zero
Running
on
Zero
Leonard Püttmann
commited on
Upload 2 files
Browse files- app.py +42 -16
- requirements.txt +1 -0
app.py
CHANGED
@@ -1,34 +1,60 @@
|
|
1 |
import gradio as gr
|
2 |
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
|
3 |
import spacy
|
|
|
4 |
|
5 |
-
#
|
6 |
-
|
7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
8 |
|
9 |
-
|
10 |
-
|
11 |
-
model_en_it = AutoModelForSeq2SeqLM.from_pretrained("LeonardPuettmann/Quadrifoglio-mt-en-it")
|
12 |
|
13 |
-
|
14 |
-
|
|
|
|
|
15 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
16 |
def generate_response_en_it(input_text):
|
17 |
-
input_ids = tokenizer_en_it("translate English to Italian: " + input_text, return_tensors="pt").input_ids
|
18 |
-
output = model_en_it.generate(input_ids, max_new_tokens=256)
|
19 |
-
return tokenizer_en_it.decode(output[0], skip_special_tokens=True)
|
20 |
|
|
|
21 |
def generate_response_it_en(input_text):
|
22 |
-
input_ids = tokenizer_it_en("translate Italian to English: " + input_text, return_tensors="pt").input_ids
|
23 |
-
output = model_it_en.generate(input_ids, max_new_tokens=256)
|
24 |
-
return tokenizer_it_en.decode(output[0], skip_special_tokens=True)
|
25 |
|
26 |
def translate_text(input_text, direction):
|
27 |
if direction == "en-it":
|
28 |
-
nlp = nlp_en
|
29 |
generate_response = generate_response_en_it
|
30 |
elif direction == "it-en":
|
31 |
-
nlp = nlp_it
|
32 |
generate_response = generate_response_it_en
|
33 |
else:
|
34 |
return "Invalid direction selected."
|
|
|
1 |
import gradio as gr
|
2 |
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
|
3 |
import spacy
|
4 |
+
import spaces
|
5 |
|
6 |
+
# Placeholder for the GPU decorator
|
7 |
+
def GPU(duration):
|
8 |
+
def decorator(func):
|
9 |
+
def wrapper(*args, **kwargs):
|
10 |
+
# Placeholder for GPU usage logic
|
11 |
+
print(f"Using GPU for {duration} seconds")
|
12 |
+
return func(*args, **kwargs)
|
13 |
+
return wrapper
|
14 |
+
return decorator
|
15 |
|
16 |
+
class ModelSingleton:
|
17 |
+
_instance = None
|
|
|
18 |
|
19 |
+
def __new__(cls, *args, **kwargs):
|
20 |
+
if not cls._instance:
|
21 |
+
cls._instance = super(ModelSingleton, cls).__new__(cls, *args, **kwargs)
|
22 |
+
return cls._instance
|
23 |
|
24 |
+
def __init__(self):
|
25 |
+
if not hasattr(self, 'initialized'):
|
26 |
+
self.nlp_en = spacy.load("en_core_web_sm")
|
27 |
+
self.nlp_it = spacy.load("it_core_news_sm")
|
28 |
+
|
29 |
+
# Load translation models and tokenizers
|
30 |
+
self.tokenizer_en_it = AutoTokenizer.from_pretrained("LeonardPuettmann/Quadrifoglio-mt-en-it")
|
31 |
+
self.model_en_it = AutoModelForSeq2SeqLM.from_pretrained("LeonardPuettmann/Quadrifoglio-mt-en-it")
|
32 |
+
|
33 |
+
self.tokenizer_it_en = AutoTokenizer.from_pretrained("LeonardPuettmann/Quadrifoglio-mt-it-en")
|
34 |
+
self.model_it_en = AutoModelForSeq2SeqLM.from_pretrained("LeonardPuettmann/Quadrifoglio-mt-it-en")
|
35 |
+
|
36 |
+
self.initialized = True
|
37 |
+
|
38 |
+
model_singleton = ModelSingleton()
|
39 |
+
|
40 |
+
@spaces.GPU(duration=120)
|
41 |
def generate_response_en_it(input_text):
|
42 |
+
input_ids = model_singleton.tokenizer_en_it("translate English to Italian: " + input_text, return_tensors="pt").input_ids
|
43 |
+
output = model_singleton.model_en_it.generate(input_ids, max_new_tokens=256)
|
44 |
+
return model_singleton.tokenizer_en_it.decode(output[0], skip_special_tokens=True)
|
45 |
|
46 |
+
@spaces.GPU(duration=120)
|
47 |
def generate_response_it_en(input_text):
|
48 |
+
input_ids = model_singleton.tokenizer_it_en("translate Italian to English: " + input_text, return_tensors="pt").input_ids
|
49 |
+
output = model_singleton.model_it_en.generate(input_ids, max_new_tokens=256)
|
50 |
+
return model_singleton.tokenizer_it_en.decode(output[0], skip_special_tokens=True)
|
51 |
|
52 |
def translate_text(input_text, direction):
|
53 |
if direction == "en-it":
|
54 |
+
nlp = model_singleton.nlp_en
|
55 |
generate_response = generate_response_en_it
|
56 |
elif direction == "it-en":
|
57 |
+
nlp = model_singleton.nlp_it
|
58 |
generate_response = generate_response_it_en
|
59 |
else:
|
60 |
return "Invalid direction selected."
|
requirements.txt
CHANGED
@@ -1,6 +1,7 @@
|
|
1 |
gradio
|
2 |
transformers
|
3 |
spacy
|
|
|
4 |
torch
|
5 |
en-core-web-sm @ https://github.com/explosion/spacy-models/releases/download/en_core_web_sm-3.7.1/en_core_web_sm-3.7.1-py3-none-any.whl
|
6 |
it-core-news-sm @ https://github.com/explosion/spacy-models/releases/download/it_core_news_sm-3.8.0/it_core_news_sm-3.8.0-py3-none-any.whl
|
|
|
1 |
gradio
|
2 |
transformers
|
3 |
spacy
|
4 |
+
spaces
|
5 |
torch
|
6 |
en-core-web-sm @ https://github.com/explosion/spacy-models/releases/download/en_core_web_sm-3.7.1/en_core_web_sm-3.7.1-py3-none-any.whl
|
7 |
it-core-news-sm @ https://github.com/explosion/spacy-models/releases/download/it_core_news_sm-3.8.0/it_core_news_sm-3.8.0-py3-none-any.whl
|