Spaces:
Sleeping
Sleeping
Upload app.py
Browse files
app.py
CHANGED
|
@@ -1,48 +1,82 @@
|
|
| 1 |
-
from fastapi import FastAPI, HTTPException
|
| 2 |
-
from pydantic import BaseModel
|
| 3 |
-
from transformers import pipeline
|
| 4 |
-
import torch
|
| 5 |
-
import os
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI, HTTPException
|
| 2 |
+
from pydantic import BaseModel
|
| 3 |
+
from transformers import pipeline
|
| 4 |
+
import torch
|
| 5 |
+
import os
|
| 6 |
+
import re
|
| 7 |
+
|
| 8 |
+
|
| 9 |
+
def fix_ibani_spacing(text: str) -> str:
|
| 10 |
+
"""Remove unwanted spaces around special Ibani characters (á, ḅ)."""
|
| 11 |
+
if not text:
|
| 12 |
+
return text
|
| 13 |
+
|
| 14 |
+
# All Ibani letters (including special characters) that can appear in words
|
| 15 |
+
ibani_letters = (
|
| 16 |
+
r'a-zA-Z' # Basic ASCII letters
|
| 17 |
+
r'ạẹịọụ' # Vowels with dot below (lowercase)
|
| 18 |
+
r'ẠẸỊỌỤ' # Vowels with dot below (uppercase)
|
| 19 |
+
r'áéíóú' # Vowels with acute (lowercase)
|
| 20 |
+
r'ÁÉÍÓÚ' # Vowels with acute (uppercase)
|
| 21 |
+
r'àèìòù' # Vowels with grave (lowercase)
|
| 22 |
+
r'ÀÈÌÒÙ' # Vowels with grave (uppercase)
|
| 23 |
+
r'ḅ' # B with dot below (lowercase)
|
| 24 |
+
r'Ḅ' # B with dot below (uppercase)
|
| 25 |
+
r'ńṅ' # N with diacritics (lowercase)
|
| 26 |
+
r'ŃṄ' # N with diacritics (uppercase)
|
| 27 |
+
)
|
| 28 |
+
|
| 29 |
+
# Characters that cause spacing issues
|
| 30 |
+
special_chars = ['á', 'Á', 'ḅ', 'Ḅ']
|
| 31 |
+
|
| 32 |
+
for char in special_chars:
|
| 33 |
+
# Remove space before the character (when preceded by any Ibani letter)
|
| 34 |
+
text = re.sub(r'([' + ibani_letters + r'])\s+' + re.escape(char), r'\1' + char, text)
|
| 35 |
+
# Remove space after the character (when followed by any Ibani letter)
|
| 36 |
+
text = re.sub(re.escape(char) + r'\s+([' + ibani_letters + r'])', char + r'\1', text)
|
| 37 |
+
|
| 38 |
+
return text
|
| 39 |
+
|
| 40 |
+
# Version: 2026-01-15 - Updated model
|
| 41 |
+
app = FastAPI(title="Ibani Translator API")
|
| 42 |
+
|
| 43 |
+
# Model configuration
|
| 44 |
+
MODEL_ID = "williampepple1/ibani-translator"
|
| 45 |
+
|
| 46 |
+
print(f"Loading model {MODEL_ID}...")
|
| 47 |
+
try:
|
| 48 |
+
# Use pipeline for easy inference
|
| 49 |
+
# device=-1 forces CPU usage which is what HF Spaces free tier provides
|
| 50 |
+
# force_download=True ensures we get the latest model version
|
| 51 |
+
translator = pipeline(
|
| 52 |
+
"translation",
|
| 53 |
+
model=MODEL_ID,
|
| 54 |
+
device=-1,
|
| 55 |
+
model_kwargs={"force_download": True}
|
| 56 |
+
)
|
| 57 |
+
print("Model loaded successfully!")
|
| 58 |
+
except Exception as e:
|
| 59 |
+
print(f"Error loading model: {e}")
|
| 60 |
+
translator = None
|
| 61 |
+
|
| 62 |
+
class TranslationRequest(BaseModel):
|
| 63 |
+
text: str
|
| 64 |
+
|
| 65 |
+
@app.get("/")
|
| 66 |
+
def read_root():
|
| 67 |
+
return {"status": "healthy", "model": MODEL_ID}
|
| 68 |
+
|
| 69 |
+
@app.post("/translate")
|
| 70 |
+
async def translate(request: TranslationRequest):
|
| 71 |
+
if translator is None:
|
| 72 |
+
raise HTTPException(status_code=503, detail="Model not loaded")
|
| 73 |
+
|
| 74 |
+
try:
|
| 75 |
+
result = translator(request.text)
|
| 76 |
+
translated_text = fix_ibani_spacing(result[0]['translation_text'])
|
| 77 |
+
return {
|
| 78 |
+
"translated_text": translated_text,
|
| 79 |
+
"original_text": request.text
|
| 80 |
+
}
|
| 81 |
+
except Exception as e:
|
| 82 |
+
raise HTTPException(status_code=500, detail=str(e))
|