harshil-code commited on
Commit
f4f553b
·
0 Parent(s):

Initial commit

Browse files
Files changed (6) hide show
  1. .gitignore +8 -0
  2. Dockerfile +17 -0
  3. README.MD +1 -0
  4. main.py +12 -0
  5. model_loader.py +26 -0
  6. requirements.txt +36 -0
.gitignore ADDED
@@ -0,0 +1,8 @@
 
 
 
 
 
 
 
 
 
1
+ # Python virtual environment
2
+ venv/
3
+ .env/
4
+ __pycache__/
5
+ *.pyc
6
+
7
+ # Hugging Face / Docker
8
+ .cache/
Dockerfile ADDED
@@ -0,0 +1,17 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ FROM python:3.10-slim
2
+
3
+ WORKDIR /app
4
+
5
+ # System dependencies
6
+ RUN apt-get update && apt-get install -y \
7
+ git \
8
+ && rm -rf /var/lib/apt/lists/*
9
+
10
+ COPY requirements.txt .
11
+ RUN pip install --no-cache-dir -r requirements.txt
12
+
13
+ COPY . .
14
+
15
+ EXPOSE 7860
16
+
17
+ CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "7860"]
README.MD ADDED
@@ -0,0 +1 @@
 
 
1
+ Hi Python, translation api
main.py ADDED
@@ -0,0 +1,12 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from fastapi import FastAPI
2
+ from pydantic import BaseModel
3
+ from model_loader import translate
4
+
5
+ app = FastAPI(title="NLLB Translation API")
6
+
7
+ class TranslationRequest(BaseModel):
8
+ text: str
9
+
10
+ @app.post("/translate")
11
+ def translate_text(req: TranslationRequest):
12
+ return {"translated_text": translate(req.text)}
model_loader.py ADDED
@@ -0,0 +1,26 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
3
+
4
+ MODEL_NAME = "facebook/nllb-200-distilled-600M"
5
+
6
+ print("Loading tokenizer and model...")
7
+
8
+ tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
9
+ model = AutoModelForSeq2SeqLM.from_pretrained(MODEL_NAME)
10
+
11
+ device = "cuda" if torch.cuda.is_available() else "cpu"
12
+ model.to(device)
13
+ model.eval()
14
+
15
+ def translate(text: str):
16
+ inputs = tokenizer(text, return_tensors="pt").to(device)
17
+
18
+ lang_id = tokenizer.convert_tokens_to_ids("arb_Arab")
19
+
20
+ outputs = model.generate(
21
+ inputs["input_ids"],
22
+ forced_bos_token_id=lang_id,
23
+ max_length=300
24
+ )
25
+
26
+ return tokenizer.decode(outputs[0], skip_special_tokens=True)
requirements.txt ADDED
@@ -0,0 +1,36 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ annotated-doc==0.0.4
2
+ annotated-types==0.7.0
3
+ anyio==4.12.0
4
+ certifi==2025.11.12
5
+ charset-normalizer==3.4.4
6
+ click==8.3.1
7
+ fastapi==0.124.4
8
+ filelock==3.20.0
9
+ fsspec==2025.12.0
10
+ h11==0.16.0
11
+ hf-xet==1.2.0
12
+ huggingface-hub==0.36.0
13
+ idna==3.11
14
+ Jinja2==3.1.6
15
+ MarkupSafe==3.0.3
16
+ mpmath==1.3.0
17
+ networkx==3.6.1
18
+ numpy==2.3.5
19
+ packaging==25.0
20
+ pydantic==2.12.5
21
+ pydantic_core==2.41.5
22
+ PyYAML==6.0.3
23
+ regex==2025.11.3
24
+ requests==2.32.5
25
+ safetensors==0.7.0
26
+ sentencepiece==0.2.1
27
+ starlette==0.50.0
28
+ sympy==1.14.0
29
+ tokenizers==0.19.1
30
+ torch==2.2.2
31
+ tqdm==4.67.1
32
+ transformers==4.40.0
33
+ typing-inspection==0.4.2
34
+ typing_extensions==4.15.0
35
+ urllib3==2.6.2
36
+ uvicorn==0.38.0