Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,10 +1,28 @@
|
|
| 1 |
-
from fastapi import FastAPI
|
| 2 |
from transformers import pipeline
|
|
|
|
| 3 |
|
| 4 |
app = FastAPI()
|
| 5 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
|
| 7 |
@app.post("/translate")
|
| 8 |
async def translate(text: str):
|
| 9 |
-
|
| 10 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI, HTTPException
|
| 2 |
from transformers import pipeline
|
| 3 |
+
import logging
|
| 4 |
|
| 5 |
app = FastAPI()
|
| 6 |
+
|
| 7 |
+
# Setup logging
|
| 8 |
+
logging.basicConfig(level=logging.INFO)
|
| 9 |
+
logger = logging.getLogger(__name__)
|
| 10 |
+
|
| 11 |
+
try:
|
| 12 |
+
logger.info("Loading translation model...")
|
| 13 |
+
translator = pipeline("translation", model="Helsinki-NLP/opus-mt-en-id")
|
| 14 |
+
logger.info("Model loaded successfully")
|
| 15 |
+
except Exception as e:
|
| 16 |
+
logger.error(f"Failed to load model: {str(e)}")
|
| 17 |
+
raise Exception(f"Model initialization failed: {str(e)}")
|
| 18 |
|
| 19 |
@app.post("/translate")
|
| 20 |
async def translate(text: str):
|
| 21 |
+
if not text:
|
| 22 |
+
raise HTTPException(status_code=400, detail="Text input is required")
|
| 23 |
+
try:
|
| 24 |
+
result = translator(text)
|
| 25 |
+
return {"translated_text": result[0]["translation_text"]}
|
| 26 |
+
except Exception as e:
|
| 27 |
+
logger.error(f"Translation failed: {str(e)}")
|
| 28 |
+
raise HTTPException(status_code=500, detail=f"Translation failed: {str(e)}")
|