Spaces:
Sleeping
Sleeping
Commit
·
b234500
0
Parent(s):
Initial deploy of Ibani Inference API
Browse files- Dockerfile +15 -0
- app.py +45 -0
- requirements.txt +7 -0
Dockerfile
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
FROM python:3.9-slim
|
| 2 |
+
|
| 3 |
+
WORKDIR /app
|
| 4 |
+
|
| 5 |
+
COPY requirements.txt .
|
| 6 |
+
RUN pip install --no-cache-dir -r requirements.txt
|
| 7 |
+
|
| 8 |
+
COPY . .
|
| 9 |
+
|
| 10 |
+
# Expose the port FastAPI runs on
|
| 11 |
+
EXPOSE 7860
|
| 12 |
+
|
| 13 |
+
# Command to run the FastAPI app
|
| 14 |
+
# Note: HF Spaces uses port 7860 by default
|
| 15 |
+
CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "7860"]
|
app.py
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI, HTTPException
|
| 2 |
+
from pydantic import BaseModel
|
| 3 |
+
from transformers import pipeline
|
| 4 |
+
import torch
|
| 5 |
+
import os
|
| 6 |
+
|
| 7 |
+
app = FastAPI(title="Ibani Translator API")
|
| 8 |
+
|
| 9 |
+
# Model configuration
|
| 10 |
+
MODEL_ID = "williampepple1/ibani-translator"
|
| 11 |
+
|
| 12 |
+
print(f"Loading model {MODEL_ID}...")
|
| 13 |
+
try:
|
| 14 |
+
# Use pipeline for easy inference
|
| 15 |
+
# device=-1 forces CPU usage which is what HF Spaces free tier provides
|
| 16 |
+
translator = pipeline(
|
| 17 |
+
"translation",
|
| 18 |
+
model=MODEL_ID,
|
| 19 |
+
device=-1
|
| 20 |
+
)
|
| 21 |
+
print("Model loaded successfully!")
|
| 22 |
+
except Exception as e:
|
| 23 |
+
print(f"Error loading model: {e}")
|
| 24 |
+
translator = None
|
| 25 |
+
|
| 26 |
+
class TranslationRequest(BaseModel):
|
| 27 |
+
text: str
|
| 28 |
+
|
| 29 |
+
@app.get("/")
|
| 30 |
+
def read_root():
|
| 31 |
+
return {"status": "healthy", "model": MODEL_ID}
|
| 32 |
+
|
| 33 |
+
@app.post("/translate")
|
| 34 |
+
async def translate(request: TranslationRequest):
|
| 35 |
+
if translator is None:
|
| 36 |
+
raise HTTPException(status_code=503, detail="Model not loaded")
|
| 37 |
+
|
| 38 |
+
try:
|
| 39 |
+
result = translator(request.text)
|
| 40 |
+
return {
|
| 41 |
+
"translated_text": result[0]['translation_text'],
|
| 42 |
+
"original_text": request.text
|
| 43 |
+
}
|
| 44 |
+
except Exception as e:
|
| 45 |
+
raise HTTPException(status_code=500, detail=str(e))
|
requirements.txt
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
fastapi
|
| 2 |
+
uvicorn
|
| 3 |
+
transformers
|
| 4 |
+
torch
|
| 5 |
+
sentencepiece
|
| 6 |
+
sacremoses
|
| 7 |
+
pydantic
|