| import malaya |
| from fastapi import FastAPI, HTTPException |
| from pydantic import BaseModel |
| import uvicorn |
| import os |
|
|
| app = FastAPI(title="Jawi Transliteration API") |
|
|
| |
| |
| try: |
| print("Loading Jawi model... this may take a moment.") |
| model = malaya.jawi.huggingface() |
| print("Model loaded successfully!") |
| except Exception as e: |
| print(f"Failed to load model: {e}") |
| model = None |
|
|
| class RequestData(BaseModel): |
| text: str |
|
|
| @app.get("/") |
| def health_check(): |
| return {"status": "ready" if model else "loading/error"} |
|
|
| @app.post("/convert") |
| async def convert_to_jawi(data: RequestData): |
| if not model: |
| raise HTTPException(status_code=503, detail="Model not loaded") |
| |
| if not data.text.strip(): |
| return {"jawi": ""} |
|
|
| try: |
| |
| result = model.generate([data.text], to_lang='jawi') |
| return { |
| "rumi": data.text, |
| "jawi": result[0] |
| } |
| except Exception as e: |
| return {"error": str(e)} |
|
|
| if __name__ == "__main__": |
| |
| port = int(os.environ.get("PORT", 7860)) |
| uvicorn.run(app, host="0.0.0.0", port=port) |