File size: 9,251 Bytes
68f4b63
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
import torch
from fastapi import FastAPI, HTTPException, Request
from fastapi.responses import HTMLResponse
from fastapi.staticfiles import StaticFiles
from pydantic import BaseModel
from transformers import AutoModelForSeq2SeqLM, AutoTokenizer
from IndicTransToolkit.processor import IndicProcessor
import os
import traceback

app = FastAPI()

# Configuration
DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
print(f"Running on device: {DEVICE}")

# Load models
MODELS = {}
HF_TOKEN = os.environ.get("HF_TOKEN")

def load_model(name, repo_id):
    print(f"Loading {name} from {repo_id}...")
    try:
        tokenizer = AutoTokenizer.from_pretrained(
            repo_id, 
            trust_remote_code=True,
            token=HF_TOKEN
        )
        # Removed flash_attention_2 to fix 'NoneType' shape error on T4
        model = AutoModelForSeq2SeqLM.from_pretrained(
            repo_id,
            trust_remote_code=True,
            torch_dtype=torch.float16 if DEVICE == "cuda" else torch.float32,
            token=HF_TOKEN
        ).to(DEVICE)
        model.eval()
        return {"tokenizer": tokenizer, "model": model}
    except Exception as e:
        print(f"Failed to load {name}: {e}")
        raise e

# Load on startup
@app.on_event("startup")
async def startup_event():
    global MODELS, ip
    
    if not HF_TOKEN:
        print("WARNING: HF_TOKEN environment variable is not set. Gated models may fail to load.")

    # 1. English to Indic
    MODELS["en-indic"] = load_model("en-indic", "ai4bharat/indictrans2-en-indic-dist-200M")
    
    # 2. Indic to English
    MODELS["indic-en"] = load_model("indic-en", "ai4bharat/indictrans2-indic-en-dist-200M")
    
    # Processor
    ip = IndicProcessor(inference=True)
    print("All models loaded successfully.")

class TranslationRequest(BaseModel):
    text: str
    source_lang: str
    target_lang: str

@app.post("/translate")
async def translate(request: TranslationRequest):
    try:
        src = request.source_lang
        tgt = request.target_lang
        text = request.text

        if not text:
            return {"translated_text": ""}

        if src.startswith("eng"):
            model_key = "en-indic"
        elif tgt.startswith("eng"):
            model_key = "indic-en"
        else:
            raise HTTPException(status_code=400, detail="Direct Indic-to-Indic translation not supported.")

        if model_key not in MODELS:
             raise HTTPException(status_code=500, detail=f"Model {model_key} failed to load on startup.")

        print(f"Translating {model_key}: {src} -> {tgt} (len: {len(text)})")
        
        bundle = MODELS[model_key]
        tokenizer = bundle["tokenizer"]
        model = bundle["model"]

        # Preprocess
        batch = ip.preprocess_batch([text], src_lang=src, tgt_lang=tgt)
        
        # Tokenize
        inputs = tokenizer(
            batch,
            truncation=True,
            padding="longest",
            return_tensors="pt",
            return_attention_mask=True,
        ).to(DEVICE)

        # Generate
        with torch.no_grad():
            generated_tokens = model.generate(
                **inputs,
                use_cache=False, 
                min_length=0,
                max_length=2048,
                num_beams=5,
                num_return_sequences=1,
            )

        # Decode
        decoded_tokens = tokenizer.batch_decode(
            generated_tokens,
            skip_special_tokens=True,
            clean_up_tokenization_spaces=True,
        )

        # Postprocess
        translations = ip.postprocess_batch(decoded_tokens, lang=tgt)
        
        return {"translated_text": translations[0]}

    except Exception as e:
        traceback.print_exc()
        print(f"Error: {e}")
        raise HTTPException(status_code=500, detail=str(e))

@app.get("/", response_class=HTMLResponse)
def read_root():
    return """
    <!DOCTYPE html>
    <html>
    <head>
        <title>Noa AI Translator</title>
        <script src="https://cdn.tailwindcss.com"></script>
    </head>
    <body class="bg-gray-50 min-h-screen p-8">
        <div class="max-w-2xl mx-auto bg-white rounded-xl shadow-md p-6">
            <h1 class="text-2xl font-bold mb-6 text-gray-800">Noa AI Translator</h1>
            
            <div class="space-y-4">
                <div class="grid grid-cols-2 gap-4">
                    <div>
                        <label class="block text-sm font-medium text-gray-700 mb-1">Source Language</label>
                        <select id="sourceLang" class="w-full border rounded-md p-2">
                            <option value="eng_Latn">English</option>
                            <option value="hin_Deva">Hindi</option>
                            <option value="tam_Taml">Tamil</option>
                            <option value="tel_Telu">Telugu</option>
                            <option value="kan_Knda">Kannada</option>
                            <option value="mal_Mlym">Malayalam</option>
                            <option value="mar_Deva">Marathi</option>
                            <option value="guj_Gujr">Gujarati</option>
                            <option value="ben_Beng">Bengali</option>
                            <option value="asm_Beng">Assamese</option>
                            <option value="pan_Guru">Punjabi</option>
                        </select>
                    </div>
                    <div>
                        <label class="block text-sm font-medium text-gray-700 mb-1">Target Language</label>
                        <select id="targetLang" class="w-full border rounded-md p-2">
                            <option value="hin_Deva">Hindi</option>
                            <option value="eng_Latn">English</option>
                            <option value="tam_Taml">Tamil</option>
                            <option value="tel_Telu">Telugu</option>
                            <option value="kan_Knda">Kannada</option>
                            <option value="mal_Mlym">Malayalam</option>
                            <option value="mar_Deva">Marathi</option>
                            <option value="guj_Gujr">Gujarati</option>
                            <option value="ben_Beng">Bengali</option>
                            <option value="asm_Beng">Assamese</option>
                            <option value="pan_Guru">Punjabi</option>
                        </select>
                    </div>
                </div>

                <div>
                    <label class="block text-sm font-medium text-gray-700 mb-1">Input Text</label>
                    <textarea id="inputText" rows="6" class="w-full border rounded-md p-2" placeholder="Enter text here..."></textarea>
                </div>

                <button onclick="translateText()" id="translateBtn" class="w-full bg-blue-600 text-white py-2 px-4 rounded-md hover:bg-blue-700 transition-colors font-medium">
                    Translate
                </button>

                <div>
                    <label class="block text-sm font-medium text-gray-700 mb-1">Translation</label>
                    <div id="outputText" class="w-full border rounded-md p-4 min-h-[150px] bg-gray-50 whitespace-pre-wrap"></div>
                </div>
            </div>
        </div>

        <script>
            async function translateText() {
                const btn = document.getElementById('translateBtn');
                const output = document.getElementById('outputText');
                const text = document.getElementById('inputText').value;
                const sourceLang = document.getElementById('sourceLang').value;
                const targetLang = document.getElementById('targetLang').value;

                if (!text) return;

                btn.disabled = true;
                btn.textContent = 'Translating...';
                output.textContent = '';

                try {
                    const response = await fetch('/translate', {
                        method: 'POST',
                        headers: {
                            'Content-Type': 'application/json',
                        },
                        body: JSON.stringify({
                            text: text,
                            source_lang: sourceLang,
                            target_lang: targetLang
                        })
                    });

                    const data = await response.json();
                    
                    if (response.ok) {
                        output.textContent = data.translated_text;
                        output.classList.remove('text-red-500');
                    } else {
                        output.textContent = 'Error: ' + (data.detail || 'Translation failed');
                        output.classList.add('text-red-500');
                    }
                } catch (e) {
                    output.textContent = 'Error: ' + e.message;
                    output.classList.add('text-red-500');
                } finally {
                    btn.disabled = false;
                    btn.textContent = 'Translate';
                }
            }
        </script>
    </body>
    </html>
    """