Spaces:
Sleeping
Sleeping
Upload app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,91 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI
|
| 2 |
+
from fastapi.middleware.cors import CORSMiddleware
|
| 3 |
+
from pydantic import BaseModel
|
| 4 |
+
import os
|
| 5 |
+
import requests
|
| 6 |
+
import argparse
|
| 7 |
+
import torch
|
| 8 |
+
|
| 9 |
+
# β
PyTorch 2.6 fix β weights_only=False allow karo
|
| 10 |
+
torch.serialization.add_safe_globals([argparse.Namespace])
|
| 11 |
+
|
| 12 |
+
app = FastAPI()
|
| 13 |
+
|
| 14 |
+
app.add_middleware(
|
| 15 |
+
CORSMiddleware,
|
| 16 |
+
allow_origins=["*"],
|
| 17 |
+
allow_methods=["*"],
|
| 18 |
+
allow_headers=["*"],
|
| 19 |
+
)
|
| 20 |
+
|
| 21 |
+
MODEL_URL = "https://huggingface.co/SLPG/English_to_Urdu_Unsupervised_MT/resolve/main/checkpoint_8_96000.pt"
|
| 22 |
+
DICT_EN_URL = "https://huggingface.co/SLPG/English_to_Urdu_Unsupervised_MT/resolve/main/dict.en.txt"
|
| 23 |
+
DICT_UR_URL = "https://huggingface.co/SLPG/English_to_Urdu_Unsupervised_MT/resolve/main/dict.ur.txt"
|
| 24 |
+
|
| 25 |
+
MODEL_PATH = "checkpoint_8_96000.pt"
|
| 26 |
+
DICT_EN_PATH = "dict.en.txt"
|
| 27 |
+
DICT_UR_PATH = "dict.ur.txt"
|
| 28 |
+
|
| 29 |
+
en_ur_model = None
|
| 30 |
+
|
| 31 |
+
def download_file(url: str, path: str):
|
| 32 |
+
if os.path.exists(path):
|
| 33 |
+
print(f"[β] Already exists: {path}")
|
| 34 |
+
return
|
| 35 |
+
print(f"[β] Downloading: {path}...")
|
| 36 |
+
with requests.get(url, stream=True) as r:
|
| 37 |
+
r.raise_for_status()
|
| 38 |
+
with open(path, "wb") as f:
|
| 39 |
+
for chunk in r.iter_content(chunk_size=8192):
|
| 40 |
+
f.write(chunk)
|
| 41 |
+
print(f"[β] Done: {path}")
|
| 42 |
+
|
| 43 |
+
def load_model():
|
| 44 |
+
global en_ur_model
|
| 45 |
+
if en_ur_model is not None:
|
| 46 |
+
return
|
| 47 |
+
|
| 48 |
+
download_file(MODEL_URL, MODEL_PATH)
|
| 49 |
+
download_file(DICT_EN_URL, DICT_EN_PATH)
|
| 50 |
+
download_file(DICT_UR_URL, DICT_UR_PATH)
|
| 51 |
+
|
| 52 |
+
from fairseq.models.transformer import TransformerModel
|
| 53 |
+
|
| 54 |
+
# β
PyTorch 2.6 fix β monkey patch
|
| 55 |
+
original_load = torch.load
|
| 56 |
+
def patched_load(*args, **kwargs):
|
| 57 |
+
kwargs['weights_only'] = False
|
| 58 |
+
return original_load(*args, **kwargs)
|
| 59 |
+
torch.load = patched_load
|
| 60 |
+
|
| 61 |
+
en_ur_model = TransformerModel.from_pretrained(
|
| 62 |
+
".",
|
| 63 |
+
checkpoint_file=MODEL_PATH,
|
| 64 |
+
data_name_or_path="."
|
| 65 |
+
)
|
| 66 |
+
en_ur_model.eval()
|
| 67 |
+
|
| 68 |
+
# Restore original
|
| 69 |
+
torch.load = original_load
|
| 70 |
+
print("[β] Model ready!")
|
| 71 |
+
|
| 72 |
+
@app.on_event("startup")
|
| 73 |
+
async def startup():
|
| 74 |
+
load_model()
|
| 75 |
+
|
| 76 |
+
class Req(BaseModel):
|
| 77 |
+
text: str
|
| 78 |
+
|
| 79 |
+
@app.get("/")
|
| 80 |
+
def root():
|
| 81 |
+
return {"status": "VoiceAura API β", "model_loaded": en_ur_model is not None}
|
| 82 |
+
|
| 83 |
+
@app.post("/translate")
|
| 84 |
+
def translate(req: Req):
|
| 85 |
+
if not req.text.strip():
|
| 86 |
+
return {"success": False, "translation": ""}
|
| 87 |
+
try:
|
| 88 |
+
result = en_ur_model.translate(req.text.strip())
|
| 89 |
+
return {"success": True, "translation": result}
|
| 90 |
+
except Exception as e:
|
| 91 |
+
return {"success": False, "translation": str(e)}
|