Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,10 +3,8 @@ from pydantic import BaseModel
|
|
| 3 |
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
|
| 4 |
import torch
|
| 5 |
|
| 6 |
-
# Nama repo model di Hugging Face (ubah kalau beda)
|
| 7 |
MODEL_ID = "rahmanansah/t5-id-bugis"
|
| 8 |
|
| 9 |
-
# Load model & tokenizer sekali saat startup
|
| 10 |
device = 0 if torch.cuda.is_available() else -1
|
| 11 |
tokenizer = AutoTokenizer.from_pretrained(MODEL_ID)
|
| 12 |
model = AutoModelForSeq2SeqLM.from_pretrained(MODEL_ID)
|
|
@@ -21,13 +19,15 @@ def translate(input: InputText):
|
|
| 21 |
if not input.text.strip():
|
| 22 |
return {"result": ""}
|
| 23 |
|
| 24 |
-
# Tambahkan prefix agar konsisten
|
| 25 |
text = input.text.strip()
|
| 26 |
if not text.lower().startswith("translate"):
|
| 27 |
text = f"translate id2bg: {text}"
|
| 28 |
|
| 29 |
-
# Tokenisasi dan inferensi
|
| 30 |
inputs = tokenizer(text, return_tensors="pt").to(model.device)
|
| 31 |
outputs = model.generate(**inputs, max_length=64)
|
| 32 |
decoded = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 33 |
return {"result": decoded}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
|
| 4 |
import torch
|
| 5 |
|
|
|
|
| 6 |
MODEL_ID = "rahmanansah/t5-id-bugis"
|
| 7 |
|
|
|
|
| 8 |
device = 0 if torch.cuda.is_available() else -1
|
| 9 |
tokenizer = AutoTokenizer.from_pretrained(MODEL_ID)
|
| 10 |
model = AutoModelForSeq2SeqLM.from_pretrained(MODEL_ID)
|
|
|
|
| 19 |
if not input.text.strip():
|
| 20 |
return {"result": ""}
|
| 21 |
|
|
|
|
| 22 |
text = input.text.strip()
|
| 23 |
if not text.lower().startswith("translate"):
|
| 24 |
text = f"translate id2bg: {text}"
|
| 25 |
|
|
|
|
| 26 |
inputs = tokenizer(text, return_tensors="pt").to(model.device)
|
| 27 |
outputs = model.generate(**inputs, max_length=64)
|
| 28 |
decoded = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 29 |
return {"result": decoded}
|
| 30 |
+
|
| 31 |
+
if __name__ == "__main__":
|
| 32 |
+
import uvicorn
|
| 33 |
+
uvicorn.run("app:app", host="0.0.0.0", port=7860)
|