Fatitommy commited on
Commit
3459a45
Β·
verified Β·
1 Parent(s): 9ef3d3f

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +125 -0
app.py ADDED
@@ -0,0 +1,125 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ VoiceAura Translation API
3
+ Models:
4
+ 1. SLPG/English_to_Urdu_Unsupervised_MT (en β†’ ur)
5
+ 2. SLPG/Punjabi_Shahmukhi_to_Gurmukhi (pa-s β†’ pa-g)
6
+ 3. SLPG/Punjabi_Gurmukhi_to_Shahmukhi (pa-g β†’ pa-s)
7
+ """
8
+
9
+ from fastapi import FastAPI
10
+ from fastapi.middleware.cors import CORSMiddleware
11
+ from pydantic import BaseModel
12
+ import os, requests, argparse, torch
13
+
14
+ # βœ… PyTorch 2.6 fix
15
+ torch.serialization.add_safe_globals([argparse.Namespace])
16
+
17
+ app = FastAPI()
18
+ app.add_middleware(
19
+ CORSMiddleware,
20
+ allow_origins=["*"],
21
+ allow_methods=["*"],
22
+ allow_headers=["*"],
23
+ )
24
+
25
+ # ── Model URLs ───────────────────────────────────────────
26
+ MODELS_CONFIG = {
27
+ "en-ur": {
28
+ "files": {
29
+ "checkpoint_8_96000.pt": "https://huggingface.co/SLPG/English_to_Urdu_Unsupervised_MT/resolve/main/checkpoint_8_96000.pt",
30
+ "dict.en.txt": "https://huggingface.co/SLPG/English_to_Urdu_Unsupervised_MT/resolve/main/dict.en.txt",
31
+ "dict.ur.txt": "https://huggingface.co/SLPG/English_to_Urdu_Unsupervised_MT/resolve/main/dict.ur.txt",
32
+ },
33
+ "dir": "models/en_ur",
34
+ "checkpoint": "checkpoint_8_96000.pt",
35
+ "instance": None,
36
+ },
37
+ "pa-s-pa-g": {
38
+ "files": {
39
+ "checkpoint_5_78000.pt": "https://huggingface.co/SLPG/Punjabi_Shahmukhi_to_Gurmukhi_Transliteration/resolve/main/checkpoint_5_78000.pt",
40
+ "dict.pa.txt": "https://huggingface.co/SLPG/Punjabi_Shahmukhi_to_Gurmukhi_Transliteration/resolve/main/dict.pa.txt",
41
+ "dict.pk.txt": "https://huggingface.co/SLPG/Punjabi_Shahmukhi_to_Gurmukhi_Transliteration/resolve/main/dict.pk.txt",
42
+ },
43
+ "dir": "models/pa_s_pa_g",
44
+ "checkpoint": "checkpoint_5_78000.pt",
45
+ "instance": None,
46
+ },
47
+ }
48
+
49
+ # ── Helpers ──────────────────────────────────────────────
50
+ def download_file(url: str, path: str):
51
+ if os.path.exists(path):
52
+ print(f"[βœ“] Exists: {path}")
53
+ return
54
+ print(f"[↓] Downloading: {path} ...")
55
+ os.makedirs(os.path.dirname(path), exist_ok=True)
56
+ with requests.get(url, stream=True) as r:
57
+ r.raise_for_status()
58
+ with open(path, "wb") as f:
59
+ for chunk in r.iter_content(chunk_size=8192):
60
+ f.write(chunk)
61
+ print(f"[βœ“] Done: {path}")
62
+
63
+ def patched_torch_load(*args, **kwargs):
64
+ kwargs["weights_only"] = False
65
+ return _original_torch_load(*args, **kwargs)
66
+
67
+ _original_torch_load = torch.load
68
+
69
+ def load_model(pair: str):
70
+ cfg = MODELS_CONFIG[pair]
71
+ if cfg["instance"] is not None:
72
+ return cfg["instance"]
73
+
74
+ # Download files
75
+ for fname, url in cfg["files"].items():
76
+ download_file(url, os.path.join(cfg["dir"], fname))
77
+
78
+ # Patch torch.load for fairseq
79
+ torch.load = patched_torch_load
80
+ from fairseq.models.transformer import TransformerModel
81
+ model = TransformerModel.from_pretrained(
82
+ cfg["dir"],
83
+ checkpoint_file=cfg["checkpoint"],
84
+ data_name_or_path=cfg["dir"],
85
+ )
86
+ torch.load = _original_torch_load
87
+ model.eval()
88
+ cfg["instance"] = model
89
+ print(f"[βœ“] Model ready: {pair}")
90
+ return model
91
+
92
+ # ── Startup β€” load all models ────────────────────────────
93
+ @app.on_event("startup")
94
+ async def startup():
95
+ for pair in MODELS_CONFIG:
96
+ load_model(pair)
97
+
98
+ # ── Endpoints ────────────────────────────────────────────
99
+ class Req(BaseModel):
100
+ text: str
101
+ from_lang: str = "en"
102
+ to_lang: str = "ur"
103
+
104
+ @app.get("/")
105
+ def root():
106
+ loaded = {k: MODELS_CONFIG[k]["instance"] is not None for k in MODELS_CONFIG}
107
+ return {"status": "VoiceAura API βœ“", "models": loaded}
108
+
109
+ @app.post("/translate")
110
+ def translate(req: Req):
111
+ if not req.text.strip():
112
+ return {"success": False, "translation": ""}
113
+
114
+ pair = f"{req.from_lang}-{req.to_lang}"
115
+
116
+ if pair not in MODELS_CONFIG:
117
+ return {"success": False, "translation": f"⚠️ Pair '{pair}' not supported."}
118
+
119
+ try:
120
+ model = load_model(pair)
121
+ result = model.translate(req.text.strip())
122
+ return {"success": True, "translation": result, "pair": pair}
123
+ except Exception as e:
124
+ print(f"Error [{pair}]: {e}")
125
+ return {"success": False, "translation": str(e)}