Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,70 +1,57 @@
|
|
|
|
|
| 1 |
import os
|
| 2 |
import gradio as gr
|
| 3 |
-
from fastapi import FastAPI
|
| 4 |
-
from pydantic import BaseModel
|
| 5 |
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM, pipeline
|
| 6 |
|
| 7 |
-
#
|
| 8 |
-
MODEL_ID = os.getenv("MODEL_ID", "hasmar03/mt5_id2md")
|
| 9 |
MAX_LEN = int(os.getenv("MAX_LEN", "128"))
|
| 10 |
|
| 11 |
-
#
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
pipe
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
|
| 21 |
-
# ====== LOGIKA TERJEMAH ======
|
| 22 |
def _build_prompt(text: str, direction: str):
|
| 23 |
-
#
|
| 24 |
-
if direction == "id2md":
|
| 25 |
return f"translate Indonesian to Mandar: {text}"
|
| 26 |
-
elif direction == "md2id":
|
| 27 |
return f"translate Mandar to Indonesian: {text}"
|
| 28 |
-
|
| 29 |
-
return text
|
| 30 |
|
| 31 |
def translate_fn(text: str, arah: str):
|
| 32 |
-
|
| 33 |
-
|
|
|
|
| 34 |
return out
|
| 35 |
|
| 36 |
-
# ====== UI GRADIO (untuk testing manual di browser) ======
|
| 37 |
with gr.Blocks(title="Mandar β Indonesia Translator") as demo:
|
| 38 |
gr.Markdown("### Mandar β Indonesia Translator")
|
| 39 |
arah = gr.Radio(
|
| 40 |
["Indonesia β Mandar", "Mandar β Indonesia"],
|
| 41 |
value="Indonesia β Mandar",
|
| 42 |
-
label="Arah"
|
| 43 |
)
|
| 44 |
-
src = gr.Textbox(label="Teks sumber", lines=3)
|
| 45 |
btn = gr.Button("Terjemahkan")
|
| 46 |
out = gr.Textbox(label="Hasil", lines=3)
|
| 47 |
-
|
|
|
|
| 48 |
|
| 49 |
-
#
|
| 50 |
-
|
| 51 |
-
text: str
|
| 52 |
-
direction: str = "id2md" # "id2md" atau "md2id"
|
| 53 |
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
fastapi_app = FastAPI()
|
| 58 |
-
|
| 59 |
-
@fastapi_app.get("/health")
|
| 60 |
-
def health():
|
| 61 |
-
return {"ok": True}
|
| 62 |
-
|
| 63 |
-
@fastapi_app.post("/translate", response_model=TranslateResp)
|
| 64 |
-
def translate_api(body: TranslateReq):
|
| 65 |
-
prompt = _build_prompt(body.text, body.direction)
|
| 66 |
-
out = pipe(prompt)[0]["generated_text"]
|
| 67 |
-
return {"translation": out}
|
| 68 |
-
|
| 69 |
-
# Mount: root (/) tetap UI Gradio, endpoint /translate adalah FastAPI
|
| 70 |
-
app = gr.mount_gradio_app(fastapi_app, demo, path="/")
|
|
|
|
| 1 |
+
# app.py β Gradio Blocks + REST API bawaan (api_name), lazy-load model
|
| 2 |
import os
|
| 3 |
import gradio as gr
|
|
|
|
|
|
|
| 4 |
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM, pipeline
|
| 5 |
|
| 6 |
+
# ===== Konfigurasi =====
|
| 7 |
+
MODEL_ID = os.getenv("MODEL_ID", "hasmar03/mt5_id2md")
|
| 8 |
MAX_LEN = int(os.getenv("MAX_LEN", "128"))
|
| 9 |
|
| 10 |
+
# ===== Lazy loader =====
|
| 11 |
+
pipe = None
|
| 12 |
+
def get_pipe():
|
| 13 |
+
global pipe
|
| 14 |
+
if pipe is None:
|
| 15 |
+
tok = AutoTokenizer.from_pretrained(MODEL_ID)
|
| 16 |
+
mdl = AutoModelForSeq2SeqLM.from_pretrained(MODEL_ID)
|
| 17 |
+
pipe = pipeline(
|
| 18 |
+
"text2text-generation",
|
| 19 |
+
model=mdl,
|
| 20 |
+
tokenizer=tok,
|
| 21 |
+
max_length=MAX_LEN,
|
| 22 |
+
)
|
| 23 |
+
return pipe
|
| 24 |
|
|
|
|
| 25 |
def _build_prompt(text: str, direction: str):
|
| 26 |
+
# Sesuaikan dengan skema training Anda
|
| 27 |
+
if direction == "id2md" or direction == "Indonesia β Mandar":
|
| 28 |
return f"translate Indonesian to Mandar: {text}"
|
| 29 |
+
elif direction == "md2id" or direction == "Mandar β Indonesia":
|
| 30 |
return f"translate Mandar to Indonesian: {text}"
|
| 31 |
+
return text
|
|
|
|
| 32 |
|
| 33 |
def translate_fn(text: str, arah: str):
|
| 34 |
+
p = get_pipe()
|
| 35 |
+
prompt = _build_prompt(text, arah)
|
| 36 |
+
out = p(prompt)[0]["generated_text"]
|
| 37 |
return out
|
| 38 |
|
|
|
|
| 39 |
with gr.Blocks(title="Mandar β Indonesia Translator") as demo:
|
| 40 |
gr.Markdown("### Mandar β Indonesia Translator")
|
| 41 |
arah = gr.Radio(
|
| 42 |
["Indonesia β Mandar", "Mandar β Indonesia"],
|
| 43 |
value="Indonesia β Mandar",
|
| 44 |
+
label="Arah",
|
| 45 |
)
|
| 46 |
+
src = gr.Textbox(label="Teks sumber", lines=3, placeholder="Ketik teksβ¦")
|
| 47 |
btn = gr.Button("Terjemahkan")
|
| 48 |
out = gr.Textbox(label="Hasil", lines=3)
|
| 49 |
+
# api_name membuat REST endpoint: /api/predict/translate
|
| 50 |
+
btn.click(translate_fn, inputs=[src, arah], outputs=out, api_name="translate")
|
| 51 |
|
| 52 |
+
# Antrian (aman untuk Space)
|
| 53 |
+
demo.queue()
|
|
|
|
|
|
|
| 54 |
|
| 55 |
+
# Opsional: saat run lokal
|
| 56 |
+
if __name__ == "__main__":
|
| 57 |
+
demo.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|