Spaces:
Sleeping
Sleeping
| from fastapi import FastAPI | |
| from fastapi.middleware.cors import CORSMiddleware | |
| from pydantic import BaseModel | |
| from transformers import pipeline | |
| app = FastAPI() | |
| # Allow CORS for your website | |
| app.add_middleware( | |
| CORSMiddleware, | |
| allow_origins=["*"], # Update with your actual domain in production | |
| allow_credentials=True, | |
| allow_methods=["*"], | |
| allow_headers=["*"], | |
| ) | |
| # Load translation models | |
| # Using Helsinki-NLP models - you can replace with your own fine-tuned models | |
| ga_en_translator = pipeline("translation", model="Helsinki-NLP/opus-mt-ga-en") | |
| en_ga_translator = pipeline("translation", model="Helsinki-NLP/opus-mt-en-ga") | |
| class TranslationRequest(BaseModel): | |
| text: str | |
| direction: str # "ga-en" or "en-ga" | |
| class TranslationResponse(BaseModel): | |
| translation: str | |
| def read_root(): | |
| return {"status": "Irish-English Translation API is running"} | |
| def translate(request: TranslationRequest): | |
| if request.direction == "ga-en": | |
| result = ga_en_translator(request.text, max_length=512) | |
| else: | |
| result = en_ga_translator(request.text, max_length=512) | |
| translation = result[0]["translation_text"] | |
| return TranslationResponse(translation=translation) | |