Spaces:
Sleeping
Sleeping
File size: 918 Bytes
f8d2fd0 306a5c6 f8d2fd0 0990428 f8d2fd0 306a5c6 0990428 f8d2fd0 229fe6a f8d2fd0 fddbb6b f8d2fd0 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | from fastapi import FastAPI, Request
from fastapi.responses import JSONResponse
from fastapi.staticfiles import StaticFiles
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
import torch
app = FastAPI()
# Mount static files (HTML + CSS)
app.mount("/", StaticFiles(directory=".", html=True), name="static")
# Load model
model_name = "itsmeussa/AdabTranslate-Darija"
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModelForSeq2SeqLM.from_pretrained(model_name)
@app.post("/translate")
async def translate(request: Request):
data = await request.json()
text = data.get("text", "")
if not text.strip():
return {"translation": "⚠️ Empty input."}
inputs = tokenizer(text, return_tensors="pt")
outputs = model.generate(**inputs, max_length=256)
translation = tokenizer.decode(outputs[0], skip_special_tokens=True)
return {"translation": translation}
|