Spaces:
Runtime error
Runtime error
rajeshradhakrishnan
commited on
Commit
•
6343e99
1
Parent(s):
f29440d
English-Malayalam Translate v1
Browse files- Dockerfile +14 -0
- code/requirements.txt +7 -0
- code/translate.py +57 -0
Dockerfile
ADDED
@@ -0,0 +1,14 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# read the doc: https://huggingface.co/docs/hub/spaces-sdks-docker
|
2 |
+
# you will also find guides on how best to write your Dockerfile
|
3 |
+
|
4 |
+
FROM python:3.9
|
5 |
+
|
6 |
+
WORKDIR /code
|
7 |
+
|
8 |
+
COPY ./requirements.txt /code/requirements.txt
|
9 |
+
|
10 |
+
RUN pip install --no-cache-dir --upgrade -r /code/requirements.txt
|
11 |
+
|
12 |
+
COPY . .
|
13 |
+
|
14 |
+
CMD ["uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "7860"]
|
code/requirements.txt
ADDED
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
torch
|
2 |
+
transformers
|
3 |
+
sentencepiece
|
4 |
+
flask
|
5 |
+
flask_restful
|
6 |
+
flask-cors
|
7 |
+
uvicorn
|
code/translate.py
ADDED
@@ -0,0 +1,57 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import torch
|
2 |
+
import os
|
3 |
+
from transformers.pipelines import pipeline
|
4 |
+
from transformers import MBartForConditionalGeneration, MBart50TokenizerFast
|
5 |
+
from flask import Flask, request
|
6 |
+
from flask_restful import Api, Resource
|
7 |
+
from flask_cors import CORS
|
8 |
+
|
9 |
+
app = Flask(__name__)
|
10 |
+
CORS(app)
|
11 |
+
cors = CORS(app, resource={
|
12 |
+
r"/*": {
|
13 |
+
"origins": "*"
|
14 |
+
}
|
15 |
+
})
|
16 |
+
api = Api(app)
|
17 |
+
|
18 |
+
app.config['CORS_HEADERS'] = 'Content-Type'
|
19 |
+
|
20 |
+
|
21 |
+
class Classifier():
|
22 |
+
|
23 |
+
def __init__(self, data_en):
|
24 |
+
self.model = MBartForConditionalGeneration.from_pretrained("facebook/mbart-large-50-one-to-many-mmt")
|
25 |
+
self.tokenizer = MBart50TokenizerFast.from_pretrained("facebook/mbart-large-50-one-to-many-mmt", src_lang="en_XX")
|
26 |
+
self.model_inputs = self.tokenizer(data_en, return_tensors="pt")
|
27 |
+
|
28 |
+
# translate from English to Malayalam
|
29 |
+
self.generated_tokens = self.model.generate(
|
30 |
+
**self.model_inputs,
|
31 |
+
forced_bos_token_id=self.tokenizer.lang_code_to_id["ml_IN"]
|
32 |
+
)
|
33 |
+
self. translate = self.tokenizer.batch_decode(self.generated_tokens, skip_special_tokens=True)
|
34 |
+
self.data_en = data_en
|
35 |
+
|
36 |
+
def get_translator(self):
|
37 |
+
output = self.translate(self.data_en)
|
38 |
+
return {'output': output}
|
39 |
+
|
40 |
+
|
41 |
+
class Translate(Resource):
|
42 |
+
def post(self):
|
43 |
+
try:
|
44 |
+
# Decode json object from the request
|
45 |
+
json_object = request.get_json()
|
46 |
+
data_en = json_object["text"]
|
47 |
+
obj = Classifier(data_en)
|
48 |
+
except Exception as e:
|
49 |
+
return {"Message": "Error in creating Translator object" + str(e)}
|
50 |
+
status = obj.get_translator()
|
51 |
+
return status
|
52 |
+
|
53 |
+
|
54 |
+
api.add_resource(Translate, '/api/translate')
|
55 |
+
|
56 |
+
if __name__ == '__main__':
|
57 |
+
app.run(host='0.0.0.0', port=7860)
|