Mitchell Kilpatrick SE2022 commited on
Commit ·
6eb24ef
1
Parent(s): e3bb193
Fix to app and requirements
Browse files- app.py +13 -1
- requirements.txt +2 -2
app.py
CHANGED
|
@@ -1,27 +1,39 @@
|
|
|
|
|
| 1 |
from fastapi import FastAPI
|
| 2 |
from pydantic import BaseModel
|
| 3 |
from transformers import pipeline
|
| 4 |
|
| 5 |
-
|
| 6 |
|
| 7 |
app = FastAPI()
|
| 8 |
|
|
|
|
|
|
|
| 9 |
translator = pipeline(
|
| 10 |
"translation",
|
| 11 |
model="Helsinki-NLP/opus-mt-synthetic-en-gd"
|
| 12 |
)
|
| 13 |
|
|
|
|
|
|
|
| 14 |
class TranslateRequest(BaseModel):
|
| 15 |
text: str
|
| 16 |
|
| 17 |
@app.get("/")
|
| 18 |
def health():
|
|
|
|
| 19 |
return {"status": "ok"}
|
| 20 |
|
| 21 |
@app.post("/translate")
|
| 22 |
def translate(req: TranslateRequest):
|
|
|
|
|
|
|
| 23 |
result = translator(req.text, max_length=140)
|
|
|
|
|
|
|
|
|
|
| 24 |
return {
|
| 25 |
"translatedText": result[0]["translation_text"],
|
| 26 |
"phonetics": ""
|
| 27 |
}
|
|
|
|
|
|
| 1 |
+
import logging
|
| 2 |
from fastapi import FastAPI
|
| 3 |
from pydantic import BaseModel
|
| 4 |
from transformers import pipeline
|
| 5 |
|
| 6 |
+
logging.basicConfig(level=logging.INFO)
|
| 7 |
|
| 8 |
app = FastAPI()
|
| 9 |
|
| 10 |
+
logging.info("Loading translation model...")
|
| 11 |
+
|
| 12 |
translator = pipeline(
|
| 13 |
"translation",
|
| 14 |
model="Helsinki-NLP/opus-mt-synthetic-en-gd"
|
| 15 |
)
|
| 16 |
|
| 17 |
+
logging.info("Model loaded successfully")
|
| 18 |
+
|
| 19 |
class TranslateRequest(BaseModel):
|
| 20 |
text: str
|
| 21 |
|
| 22 |
@app.get("/")
|
| 23 |
def health():
|
| 24 |
+
logging.info("Health check called")
|
| 25 |
return {"status": "ok"}
|
| 26 |
|
| 27 |
@app.post("/translate")
|
| 28 |
def translate(req: TranslateRequest):
|
| 29 |
+
logging.info(f"Received text: {req.text}")
|
| 30 |
+
|
| 31 |
result = translator(req.text, max_length=140)
|
| 32 |
+
|
| 33 |
+
logging.info(f"Raw model output: {result}")
|
| 34 |
+
|
| 35 |
return {
|
| 36 |
"translatedText": result[0]["translation_text"],
|
| 37 |
"phonetics": ""
|
| 38 |
}
|
| 39 |
+
|
requirements.txt
CHANGED
|
@@ -1,5 +1,5 @@
|
|
| 1 |
fastapi
|
| 2 |
-
uvicorn
|
| 3 |
transformers
|
| 4 |
torch
|
| 5 |
-
sentencepiece
|
|
|
|
| 1 |
fastapi
|
| 2 |
+
uvicorn[standard]
|
| 3 |
transformers
|
| 4 |
torch
|
| 5 |
+
sentencepiece
|