Spaces:
Runtime error
Runtime error
Download app.py from azaharizaman/huggingface-php: direct link, hf CLI and curl.
- Browser
- Download file 977 Bytes
-
https://huggingface.co/spaces/azaharizaman/huggingface-php/resolve/main/app.py
- Command line
-
hf download hf://spaces/azaharizaman/huggingface-php/app.py
-
curl -L -o app.py https://huggingface.co/spaces/azaharizaman/huggingface-php/resolve/main/app.py
977 Bytes
| from fastapi import FastAPI, HTTPException | |
| from pydantic import BaseModel | |
| from transformers import pipeline | |
| app = FastAPI(title="English to French Translator") | |
| # Load model once at startuprfff | |
| translator = pipeline("translation_en_to_fr", model="Helsinki-NLP/opus-mt-en-fr") | |
| class TranslateRequest(BaseModel): | |
| text: str | |
| class TranslateResponse(BaseModel): | |
| translated_text: str | |
| def root(): | |
| return {"message": "Welcome to the English–French Translation API!"} | |
| def translate(req: TranslateRequest): | |
| if not req.text.strip(): | |
| raise HTTPException(status_code=400, detail="Text input cannot be empty.") | |
| try: | |
| result = translator(req.text) | |
| translated = result[0]["translation_text"] | |
| return TranslateResponse(translated_text=translated) | |
| except Exception as e: | |
| raise HTTPException(status_code=500, detail=f"Translation failed: {str(e)}") | |