williampepple1 commited on
Commit
2798693
·
verified ·
1 Parent(s): b6f6301

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +82 -48
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
- # Version: 2026-01-15 - Updated model
8
- app = FastAPI(title="Ibani Translator API")
9
-
10
- # Model configuration
11
- MODEL_ID = "williampepple1/ibani-translator"
12
-
13
- print(f"Loading model {MODEL_ID}...")
14
- try:
15
- # Use pipeline for easy inference
16
- # device=-1 forces CPU usage which is what HF Spaces free tier provides
17
- # force_download=True ensures we get the latest model version
18
- translator = pipeline(
19
- "translation",
20
- model=MODEL_ID,
21
- device=-1,
22
- model_kwargs={"force_download": True}
23
- )
24
- print("Model loaded successfully!")
25
- except Exception as e:
26
- print(f"Error loading model: {e}")
27
- translator = None
28
-
29
- class TranslationRequest(BaseModel):
30
- text: str
31
-
32
- @app.get("/")
33
- def read_root():
34
- return {"status": "healthy", "model": MODEL_ID}
35
-
36
- @app.post("/translate")
37
- async def translate(request: TranslationRequest):
38
- if translator is None:
39
- raise HTTPException(status_code=503, detail="Model not loaded")
40
-
41
- try:
42
- result = translator(request.text)
43
- return {
44
- "translated_text": result[0]['translation_text'],
45
- "original_text": request.text
46
- }
47
- except Exception as e:
48
- raise HTTPException(status_code=500, detail=str(e))
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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))