Spaces:
Running
Running
Upload 3 files
Browse files- Dockerfile.txt +17 -0
- main.py +31 -0
- requirements.txt +7 -0
Dockerfile.txt
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Use the official Python image from Docker Hub
|
| 2 |
+
FROM python:3.10-slim
|
| 3 |
+
|
| 4 |
+
# Set the working directory
|
| 5 |
+
WORKDIR /app
|
| 6 |
+
|
| 7 |
+
# Copy the requirements file into the container
|
| 8 |
+
COPY requirements.txt .
|
| 9 |
+
|
| 10 |
+
# Install dependencies
|
| 11 |
+
RUN pip install --no-cache-dir -r requirements.txt
|
| 12 |
+
|
| 13 |
+
# Copy the current directory contents into the container at /app
|
| 14 |
+
COPY . .
|
| 15 |
+
|
| 16 |
+
# Command to run the FastAPI app
|
| 17 |
+
CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "7860"]
|
main.py
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI
|
| 2 |
+
from pydantic import BaseModel
|
| 3 |
+
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
|
| 4 |
+
import torch
|
| 5 |
+
|
| 6 |
+
app = FastAPI()
|
| 7 |
+
|
| 8 |
+
# Load model once on startup
|
| 9 |
+
MODEL_NAME = "facebook/nllb-200-1.3B" # 3B version
|
| 10 |
+
tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
|
| 11 |
+
model = AutoModelForSeq2SeqLM.from_pretrained(MODEL_NAME, torch_dtype=torch.float16).to("cuda" if torch.cuda.is_available() else "cpu")
|
| 12 |
+
|
| 13 |
+
class TranslationRequest(BaseModel):
|
| 14 |
+
text: str
|
| 15 |
+
src_lang: str
|
| 16 |
+
tgt_lang: str
|
| 17 |
+
|
| 18 |
+
@app.post("/translate")
|
| 19 |
+
def translate(req: TranslationRequest):
|
| 20 |
+
inputs = tokenizer(
|
| 21 |
+
req.text,
|
| 22 |
+
return_tensors="pt",
|
| 23 |
+
).to(model.device)
|
| 24 |
+
|
| 25 |
+
outputs = model.generate(
|
| 26 |
+
**inputs,
|
| 27 |
+
forced_bos_token_id=tokenizer.lang_code_to_id[req.tgt_lang],
|
| 28 |
+
max_length=512
|
| 29 |
+
)
|
| 30 |
+
translation = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 31 |
+
return {"translation": translation}
|
requirements.txt
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
transformers
|
| 2 |
+
torch
|
| 3 |
+
sentencepiece
|
| 4 |
+
tiktoken
|
| 5 |
+
fastapi
|
| 6 |
+
uvicorn
|
| 7 |
+
protobuf
|