AbrahamKlb commited on
Commit
315eac1
1 Parent(s): b9dcc74

Upload 3 files

Browse files
Files changed (3) hide show
  1. Dockerfile +29 -0
  2. app.py +33 -0
  3. requirements.txt +8 -0
Dockerfile ADDED
@@ -0,0 +1,29 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Use an official Python runtime as a parent image
2
+ FROM python:3.8-slim-buster
3
+
4
+ # Set the working directory in the container to /app
5
+ WORKDIR /app
6
+
7
+ # Clone the mmtafrica repository
8
+ RUN git clone https://github.com/edaiofficial/mmtafrica.git
9
+
10
+ # Add the current directory contents into the container at /app
11
+ ADD . /app
12
+
13
+ # Install virtualenv and activate it
14
+ RUN pip install --no-cache-dir virtualenv
15
+
16
+ # Install any needed packages specified in requirements.txt
17
+ RUN pip install --no-cache-dir -r requirements.txt
18
+
19
+ # Install mmtafrica from the cloned repository
20
+ RUN pip install --no-cache-dir /app/mmtafrica
21
+
22
+ # Install additional packages
23
+ RUN pip install --no-cache-dir numpy pandas scikit-learn matplotlib
24
+
25
+ # Make port 80 available to the world outside this container
26
+ EXPOSE 80
27
+
28
+ # Run app.py when the container launches
29
+ CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "80"]
app.py ADDED
@@ -0,0 +1,33 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from fastapi import FastAPI
2
+ from typing import Optional
3
+ import torch
4
+ from mmtafrica.mmtafrica import load_params, translate
5
+ from huggingface_hub import hf_hub_download
6
+
7
+ app = FastAPI()
8
+
9
+ language_map = {'English':'en','Swahili':'sw','Fon':'fon','Igbo':'ig',
10
+ 'Kinyarwanda':'rw','Xhosa':'xh','Yoruba':'yo','French':'fr'}
11
+
12
+ # Load parameters and model from checkpoint
13
+ checkpoint = hf_hub_download(repo_id="chrisjay/mmtafrica", filename="mmt_translation.pt")
14
+ device = 'gpu' if torch.cuda.is_available() else 'cpu'
15
+ params = load_params({'checkpoint':checkpoint,'device':device})
16
+
17
+ @app.post("/translate")
18
+ async def translate(data: dict):
19
+ source_language = data['source_language']
20
+ target_language = data['target_language']
21
+ source_sentence = data['source_sentence']
22
+
23
+ source_language_ = language_map[source_language]
24
+ target_language_ = language_map[target_language]
25
+
26
+ try:
27
+ pred = translate(params,source_sentence,source_lang=source_language_,target_lang=target_language_)
28
+ if pred=='':
29
+ return {"translation": "Could not find translation"}
30
+ else:
31
+ return {"translation": pred}
32
+ except Exception as error:
33
+ return {"error": f"Issue with translation: \n {error}"}
requirements.txt ADDED
@@ -0,0 +1,8 @@
 
 
 
 
 
 
 
 
 
1
+ torch
2
+ huggingface_hub
3
+ fastapi
4
+ uvicorn
5
+ numpy
6
+ pandas
7
+ scikit-learn
8
+ matplotlib