devusman commited on
Commit
05e7f60
·
1 Parent(s): d9f9373
Files changed (3) hide show
  1. Dockerfile +15 -6
  2. app.py +46 -0
  3. requirements.txt +5 -0
Dockerfile CHANGED
@@ -1,8 +1,17 @@
1
- # Use the official LibreTranslate image
2
- FROM libretranslate/libretranslate:latest
3
 
4
- # Expose the default LibreTranslate port
5
- EXPOSE 5000
6
 
7
- # Set the command to run the LibreTranslate server
8
- CMD ["./run.sh", "--host", "0.0.0.0"]
 
 
 
 
 
 
 
 
 
 
1
+ # Use an official Python runtime as a parent image
2
+ FROM python:3.9-slim
3
 
4
+ # Set the working directory in the container
5
+ WORKDIR /code
6
 
7
+ # Copy the dependencies file to the working directory
8
+ COPY requirements.txt .
9
+
10
+ # Install any needed packages specified in requirements.txt
11
+ RUN pip install --no-cache-dir --upgrade -r requirements.txt
12
+
13
+ # Copy the rest of the application code to the working directory
14
+ COPY . .
15
+
16
+ # Command to run the API
17
+ CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "7860"]
app.py ADDED
@@ -0,0 +1,46 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from fastapi import FastAPI, HTTPException
2
+ from pydantic import BaseModel
3
+ import ctranslate2
4
+ import sentencepiece as spm
5
+ import os
6
+
7
+ app = FastAPI()
8
+
9
+ # Load the CTranslate2 model and the SentencePiece tokenizer
10
+ try:
11
+ model_path = "en_ar_ct2_model/"
12
+ sp_model_path = os.path.join(model_path, "source.spm")
13
+
14
+ translator = ctranslate2.Translator(model_path, device="cpu") # Use "cuda" if on a GPU Space
15
+ sp = spm.SentencePieceProcessor()
16
+ sp.load(sp_model_path)
17
+ except Exception as e:
18
+ # This helps in debugging if the model files are not found
19
+ raise RuntimeError(f"Error loading model: {e}")
20
+
21
+
22
+ class TranslationRequest(BaseModel):
23
+ text: list[str] # Expect a list of strings for batching
24
+
25
+ class TranslationResponse(BaseModel):
26
+ translations: list[str]
27
+
28
+ @app.get("/")
29
+ def read_root():
30
+ return {"message": "English to Arabic Translation API is running."}
31
+
32
+ @app.post("/translate", response_model=TranslationResponse)
33
+ def translate_text(request: TranslationRequest):
34
+ if not request.text:
35
+ raise HTTPException(status_code=400, detail="Input text list cannot be empty.")
36
+
37
+ source_sentences = request.text
38
+ source_tokenized = [sp.encode(sentence, out_type=str) for sentence in source_sentences]
39
+
40
+ # Translate the batch of sentences
41
+ translations_tokenized = translator.translate_batch(source_tokenized)
42
+
43
+ # Decode the translated sentences
44
+ translations = [sp.decode(translation.hypotheses[0]) for translation in translations_tokenized]
45
+
46
+ return {"translations": translations}
requirements.txt ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
 
1
+ fastapi
2
+ uvicorn
3
+ ctranslate2
4
+ sentencepiece
5
+ transformers