EcoFriendlyWoodVerneer commited on
Commit
5e1f034
·
1 Parent(s): a107aaf

pls work?

Browse files
Files changed (1) hide show
  1. app.py +56 -15
app.py CHANGED
@@ -1,35 +1,76 @@
1
- import os
2
  from pathlib import Path
3
-
4
  import gradio as gr
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5
 
6
- MODEL_DIR = Path("t5_enzh")
7
  def model_ready():
8
- if not MODEL_DIR.exists():
9
- return False
10
- # accept either a final save or checkpoints
11
- has_ckpt = any(p.is_dir() and p.name.startswith("checkpoint-") for p in MODEL_DIR.iterdir())
12
- has_model = (MODEL_DIR / "config.json").exists() or (MODEL_DIR / "pytorch_model.bin").exists()
13
- return has_ckpt or has_model
 
 
 
 
 
 
14
 
15
  def translate_text(text: str):
16
  text = (text or "").strip()
17
  if not text:
18
  return ""
19
  if not model_ready():
20
- return "[Model not ready yet — training/upload not finished. Try again later.]"
21
- # TODO: load model + tokenizer here once you upload them
22
- return "[Model ready — inference not wired yet]"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
23
 
24
  with gr.Blocks() as demo:
25
- gr.Markdown("# EN → ZH Translator")
26
- status = "✅ Model ready" if model_ready() else "⏳ Model not ready"
27
  gr.Markdown(f"**Status:** {status}")
 
28
 
29
  inp = gr.Textbox(label="English", lines=4, placeholder="Type English here...")
30
  out = gr.Textbox(label="Chinese", lines=4)
31
  btn = gr.Button("Translate")
32
-
33
  btn.click(translate_text, inp, out)
34
 
35
  demo.launch()
 
 
1
  from pathlib import Path
 
2
  import gradio as gr
3
+ import torch
4
+ from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
5
+
6
+ ROOT = Path(__file__).parent
7
+
8
+ # Your repo has: t5_enzh/checkpoint-50000/...
9
+ MODEL_ROOT = ROOT / "t5_enzh"
10
+
11
+ def latest_checkpoint(root: Path) -> Path | None:
12
+ if not root.exists():
13
+ return None
14
+ ckpts = [p for p in root.iterdir() if p.is_dir() and p.name.startswith("checkpoint-")]
15
+ if not ckpts:
16
+ # maybe you uploaded final model directly into t5_enzh
17
+ if (root / "config.json").exists():
18
+ return root
19
+ return None
20
+ # pick biggest step number
21
+ ckpts.sort(key=lambda p: int(p.name.split("-")[-1]))
22
+ return ckpts[-1]
23
+
24
+ CKPT = latest_checkpoint(MODEL_ROOT)
25
+
26
+ _pipe = {"tok": None, "model": None}
27
 
 
28
  def model_ready():
29
+ return CKPT is not None
30
+
31
+ def get_model():
32
+ if _pipe["model"] is None:
33
+ tok = AutoTokenizer.from_pretrained(CKPT, use_fast=False)
34
+ model = AutoModelForSeq2SeqLM.from_pretrained(CKPT)
35
+ if torch.cuda.is_available():
36
+ model = model.to("cuda")
37
+ model.eval()
38
+ _pipe["tok"] = tok
39
+ _pipe["model"] = model
40
+ return _pipe["tok"], _pipe["model"]
41
 
42
  def translate_text(text: str):
43
  text = (text or "").strip()
44
  if not text:
45
  return ""
46
  if not model_ready():
47
+ return "[Model not ready yet — upload/checkpoint missing.]"
48
+
49
+ tok, model = get_model()
50
+ inputs = tok(text, return_tensors="pt", truncation=True, max_length=128)
51
+ if torch.cuda.is_available():
52
+ inputs = {k: v.to("cuda") for k, v in inputs.items()}
53
+
54
+ with torch.no_grad():
55
+ out_ids = model.generate(
56
+ **inputs,
57
+ max_new_tokens=128,
58
+ num_beams=4,
59
+ length_penalty=1.0,
60
+ )
61
+ return tok.decode(out_ids[0], skip_special_tokens=True)
62
+
63
+ title = "EN → ZH Translator"
64
+ status = "✅ Model ready" if model_ready() else "⏳ Model not ready"
65
 
66
  with gr.Blocks() as demo:
67
+ gr.Markdown(f"# {title}")
 
68
  gr.Markdown(f"**Status:** {status}")
69
+ gr.Markdown(f"**Loaded path:** `{str(CKPT) if CKPT else 'None'}`")
70
 
71
  inp = gr.Textbox(label="English", lines=4, placeholder="Type English here...")
72
  out = gr.Textbox(label="Chinese", lines=4)
73
  btn = gr.Button("Translate")
 
74
  btn.click(translate_text, inp, out)
75
 
76
  demo.launch()