Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,7 +3,7 @@
|
|
| 3 |
# MODEL: facebook/nllb-200-distilled-600M
|
| 4 |
# + RAG (FAISS, backend only)
|
| 5 |
# + Post-Editing Indonesia (TinyLlama)
|
| 6 |
-
# METRIK: BLEU + ROUGE-L + METEOR
|
| 7 |
# ============================================================
|
| 8 |
|
| 9 |
import torch
|
|
@@ -23,6 +23,9 @@ from transformers import (
|
|
| 23 |
AutoModelForCausalLM
|
| 24 |
)
|
| 25 |
|
|
|
|
|
|
|
|
|
|
| 26 |
# ============================================================
|
| 27 |
# Setup
|
| 28 |
# ============================================================
|
|
@@ -57,6 +60,16 @@ pe_model.eval()
|
|
| 57 |
|
| 58 |
print("TinyLlama loaded")
|
| 59 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 60 |
# ============================================================
|
| 61 |
# Load RAG Corpus
|
| 62 |
# ============================================================
|
|
@@ -100,12 +113,9 @@ Perbaiki kalimat Bahasa Indonesia berikut TANPA:
|
|
| 100 |
- menambah informasi
|
| 101 |
- mengurangi informasi
|
| 102 |
- mengubah makna
|
| 103 |
-
|
| 104 |
Hanya rapikan tata bahasa dan hilangkan pengulangan.
|
| 105 |
-
|
| 106 |
Kalimat:
|
| 107 |
{text}
|
| 108 |
-
|
| 109 |
Hasil:
|
| 110 |
"""
|
| 111 |
|
|
@@ -123,6 +133,22 @@ Hasil:
|
|
| 123 |
decoded = pe_tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 124 |
return decoded.split("Hasil:")[-1].strip()
|
| 125 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 126 |
# ============================================================
|
| 127 |
# Translation Pipeline
|
| 128 |
# ============================================================
|
|
@@ -130,7 +156,7 @@ def translate_jawa(text):
|
|
| 130 |
if not text or not text.strip():
|
| 131 |
return "", ""
|
| 132 |
|
| 133 |
-
# ---------- RAG
|
| 134 |
_ = retrieve_context(text, k=3)
|
| 135 |
|
| 136 |
# ---------- Jawa → Indonesia ----------
|
|
@@ -184,18 +210,23 @@ def evaluate_translation(jawa, ref_id, ref_en):
|
|
| 184 |
pred_id, pred_en = translate_jawa(jawa)
|
| 185 |
|
| 186 |
bleu = sacrebleu.corpus_bleu([pred_id], [[ref_id]]).score
|
| 187 |
-
|
| 188 |
rouge = Rouge()
|
| 189 |
rouge_l = rouge.get_scores(pred_id, ref_id)[0]["rouge-l"]["f"]
|
| 190 |
-
|
| 191 |
meteor = meteor_score([ref_id.split()], pred_id.split())
|
| 192 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 193 |
return (
|
| 194 |
pred_id,
|
| 195 |
pred_en,
|
| 196 |
f"{bleu:.2f}",
|
| 197 |
f"{rouge_l:.4f}",
|
| 198 |
-
f"{meteor:.4f}"
|
|
|
|
| 199 |
)
|
| 200 |
|
| 201 |
# ============================================================
|
|
@@ -206,7 +237,7 @@ with gr.Blocks(title="Jawa Translation System") as demo:
|
|
| 206 |
## 🌾 Translasi Bahasa Jawa (NLLB + RAG + TinyLlama)
|
| 207 |
✔ RAG Parallel Corpus
|
| 208 |
✔ Post-Editing Neural
|
| 209 |
-
✔ Evaluasi BLEU · ROUGE-L · METEOR
|
| 210 |
""")
|
| 211 |
|
| 212 |
with gr.Tab("🔤 Translasi"):
|
|
@@ -230,11 +261,12 @@ with gr.Blocks(title="Jawa Translation System") as demo:
|
|
| 230 |
bleu = gr.Textbox(label="BLEU")
|
| 231 |
rouge_l = gr.Textbox(label="ROUGE-L")
|
| 232 |
meteor = gr.Textbox(label="METEOR")
|
|
|
|
| 233 |
|
| 234 |
gr.Button("Evaluasi").click(
|
| 235 |
evaluate_translation,
|
| 236 |
inputs=[eval_jawa, ref_id, ref_en],
|
| 237 |
-
outputs=[pred_id, pred_en, bleu, rouge_l, meteor]
|
| 238 |
)
|
| 239 |
|
| 240 |
# ============================================================
|
|
@@ -247,3 +279,6 @@ if __name__ == "__main__":
|
|
| 247 |
share=False
|
| 248 |
)
|
| 249 |
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
# MODEL: facebook/nllb-200-distilled-600M
|
| 4 |
# + RAG (FAISS, backend only)
|
| 5 |
# + Post-Editing Indonesia (TinyLlama)
|
| 6 |
+
# METRIK: BLEU + ROUGE-L + METEOR + COMET
|
| 7 |
# ============================================================
|
| 8 |
|
| 9 |
import torch
|
|
|
|
| 23 |
AutoModelForCausalLM
|
| 24 |
)
|
| 25 |
|
| 26 |
+
# ===================== COMET =====================
|
| 27 |
+
from comet import download_model, load_from_checkpoint
|
| 28 |
+
|
| 29 |
# ============================================================
|
| 30 |
# Setup
|
| 31 |
# ============================================================
|
|
|
|
| 60 |
|
| 61 |
print("TinyLlama loaded")
|
| 62 |
|
| 63 |
+
# ============================================================
|
| 64 |
+
# Load COMET
|
| 65 |
+
# ============================================================
|
| 66 |
+
print("Loading COMET...")
|
| 67 |
+
|
| 68 |
+
comet_model_path = download_model("Unbabel/wmt22-comet-da")
|
| 69 |
+
comet_model = load_from_checkpoint(comet_model_path)
|
| 70 |
+
|
| 71 |
+
print("COMET loaded")
|
| 72 |
+
|
| 73 |
# ============================================================
|
| 74 |
# Load RAG Corpus
|
| 75 |
# ============================================================
|
|
|
|
| 113 |
- menambah informasi
|
| 114 |
- mengurangi informasi
|
| 115 |
- mengubah makna
|
|
|
|
| 116 |
Hanya rapikan tata bahasa dan hilangkan pengulangan.
|
|
|
|
| 117 |
Kalimat:
|
| 118 |
{text}
|
|
|
|
| 119 |
Hasil:
|
| 120 |
"""
|
| 121 |
|
|
|
|
| 133 |
decoded = pe_tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 134 |
return decoded.split("Hasil:")[-1].strip()
|
| 135 |
|
| 136 |
+
# ============================================================
|
| 137 |
+
# COMET Evaluation
|
| 138 |
+
# ============================================================
|
| 139 |
+
def compute_comet(src, mt, ref):
|
| 140 |
+
try:
|
| 141 |
+
data = [{
|
| 142 |
+
"src": src,
|
| 143 |
+
"mt": mt,
|
| 144 |
+
"ref": ref
|
| 145 |
+
}]
|
| 146 |
+
score = comet_model.predict(data, batch_size=1, gpus=0)
|
| 147 |
+
return float(score["scores"][0])
|
| 148 |
+
except Exception as e:
|
| 149 |
+
print("COMET error:", e)
|
| 150 |
+
return 0.0
|
| 151 |
+
|
| 152 |
# ============================================================
|
| 153 |
# Translation Pipeline
|
| 154 |
# ============================================================
|
|
|
|
| 156 |
if not text or not text.strip():
|
| 157 |
return "", ""
|
| 158 |
|
| 159 |
+
# ---------- RAG ----------
|
| 160 |
_ = retrieve_context(text, k=3)
|
| 161 |
|
| 162 |
# ---------- Jawa → Indonesia ----------
|
|
|
|
| 210 |
pred_id, pred_en = translate_jawa(jawa)
|
| 211 |
|
| 212 |
bleu = sacrebleu.corpus_bleu([pred_id], [[ref_id]]).score
|
|
|
|
| 213 |
rouge = Rouge()
|
| 214 |
rouge_l = rouge.get_scores(pred_id, ref_id)[0]["rouge-l"]["f"]
|
|
|
|
| 215 |
meteor = meteor_score([ref_id.split()], pred_id.split())
|
| 216 |
|
| 217 |
+
comet_score = compute_comet(
|
| 218 |
+
src=jawa,
|
| 219 |
+
mt=pred_en,
|
| 220 |
+
ref=ref_en
|
| 221 |
+
)
|
| 222 |
+
|
| 223 |
return (
|
| 224 |
pred_id,
|
| 225 |
pred_en,
|
| 226 |
f"{bleu:.2f}",
|
| 227 |
f"{rouge_l:.4f}",
|
| 228 |
+
f"{meteor:.4f}",
|
| 229 |
+
f"{comet_score:.4f}"
|
| 230 |
)
|
| 231 |
|
| 232 |
# ============================================================
|
|
|
|
| 237 |
## 🌾 Translasi Bahasa Jawa (NLLB + RAG + TinyLlama)
|
| 238 |
✔ RAG Parallel Corpus
|
| 239 |
✔ Post-Editing Neural
|
| 240 |
+
✔ Evaluasi BLEU · ROUGE-L · METEOR · COMET
|
| 241 |
""")
|
| 242 |
|
| 243 |
with gr.Tab("🔤 Translasi"):
|
|
|
|
| 261 |
bleu = gr.Textbox(label="BLEU")
|
| 262 |
rouge_l = gr.Textbox(label="ROUGE-L")
|
| 263 |
meteor = gr.Textbox(label="METEOR")
|
| 264 |
+
comet_box = gr.Textbox(label="COMET")
|
| 265 |
|
| 266 |
gr.Button("Evaluasi").click(
|
| 267 |
evaluate_translation,
|
| 268 |
inputs=[eval_jawa, ref_id, ref_en],
|
| 269 |
+
outputs=[pred_id, pred_en, bleu, rouge_l, meteor, comet_box]
|
| 270 |
)
|
| 271 |
|
| 272 |
# ============================================================
|
|
|
|
| 279 |
share=False
|
| 280 |
)
|
| 281 |
|
| 282 |
+
|
| 283 |
+
|
| 284 |
+
|