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b01
 
 
 
 
 
 
 
9f682aa
3df9b01
 
 
 
 
 
 
9f682aa
3df9b01
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
from fastapi import FastAPI, Request
from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import JSONResponse
from pydantic import BaseModel
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
import torch
import asyncio
import httpx
import time
from concurrent.futures import ThreadPoolExecutor
import hashlib
from collections import defaultdict

app = FastAPI(title="Distributed M2M100 API")

# =====================================================
# CORS
# =====================================================
app.add_middleware(
    CORSMiddleware,
    allow_origins=["*"],
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)

# =====================================================
# MODEL
# =====================================================
MODEL_NAME = "facebook/m2m100_418M"
print("Loading model...")
tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
model = AutoModelForSeq2SeqLM.from_pretrained(MODEL_NAME)
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
model.to(device)
model.eval()
torch.set_num_threads(2)
torch.set_num_interop_threads(1)
print("Model loaded!")

# =====================================================
# CONFIG
# =====================================================
MAX_ACTIVE_REQUESTS = 3
MAX_QUEUE_SIZE = 3
MAX_CHARACTERS = 3000  # Character limit without spaces per user
USERNAME = "velost"
SPACE_PREFIX = "trans"
DISCOVERY_CHUNK_SIZE = 50
CACHE_TTL = 86400  # Cache time-to-live: 1 day in seconds (24 * 60 * 60)
MAX_CACHE_SIZE = 10000  # Maximum number of cached translations
MAX_RPS_LANGUAGES = 20000  # Maximum requests per second for /languages endpoint

# =====================================================
# GLOBALS
# =====================================================
active_requests = 0
request_queue = asyncio.Queue(maxsize=MAX_QUEUE_SIZE)
executor = ThreadPoolExecutor(max_workers=3)
lock = asyncio.Lock()

# 🔥 cooldown map (NEW)
cooldown = {}

# 🔥 Translation cache with TTL (NEW)
translation_cache = {}

# 🔥 RPS tracking for /languages endpoint (NEW)
languages_request_times = []
languages_lock = asyncio.Lock()

# =====================================================
# LANGUAGES
# =====================================================
SUPPORTED_LANGS = {
  "af": "Afrikaans",
  "am": "Amharic",
  "ar": "Arabic",
  "ast": "Asturian",
  "az": "Azerbaijani",
  "ba": "Bashkir",
  "be": "Belarusian",
  "bg": "Bulgarian",
  "bn": "Bengali",
  "br": "Breton",
  "bs": "Bosnian",
  "ca": "Catalan",
  "ceb": "Cebuano",
  "cs": "Czech",
  "cy": "Welsh",
  "da": "Danish",
  "de": "German",
  "el": "Greek",
  "en": "English",
  "es": "Spanish",
  "et": "Estonian",
  "fa": "Persian",
  "ff": "Fulah",
  "fi": "Finnish",
  "fr": "French",
  "fy": "Western Frisian",
  "ga": "Irish",
  "gd": "Scottish Gaelic",
  "gl": "Galician",
  "gu": "Gujarati",
  "ha": "Hausa",
  "he": "Hebrew",
  "hi": "Hindi",
  "hr": "Croatian",
  "ht": "Haitian Creole",
  "hu": "Hungarian",
  "hy": "Armenian",
  "id": "Indonesian",
  "ig": "Igbo",
  "ilo": "Ilocano",
  "is": "Icelandic",
  "it": "Italian",
  "ja": "Japanese",
  "jv": "Javanese",
  "ka": "Georgian",
  "kk": "Kazakh",
  "km": "Khmer",
  "kn": "Kannada",
  "ko": "Korean",
  "lb": "Luxembourgish",
  "lg": "Ganda",
  "ln": "Lingala",
  "lo": "Lao",
  "lt": "Lithuanian",
  "lv": "Latvian",
  "mg": "Malagasy",
  "mk": "Macedonian",
  "ml": "Malayalam",
  "mn": "Mongolian",
  "mr": "Marathi",
  "ms": "Malay",
  "my": "Myanmar",
  "ne": "Nepali",
  "nl": "Dutch",
  "no": "Norwegian",
  "ns": "Northern Sotho",
  "oc": "Occitan",
  "or": "Odia",
  "pa": "Punjabi",
  "pl": "Polish",
  "ps": "Pashto",
  "pt": "Portuguese",
  "ro": "Romanian",
  "ru": "Russian",
  "sd": "Sindhi",
  "si": "Sinhala",
  "sk": "Slovak",
  "sl": "Slovenian",
  "so": "Somali",
  "sq": "Albanian",
  "sr": "Serbian",
  "ss": "Swati",
  "su": "Sundanese",
  "sv": "Swedish",
  "sw": "Swahili",
  "ta": "Tamil",
  "th": "Thai",
  "tl": "Tagalog",
  "tn": "Tswana",
  "tr": "Turkish",
  "uk": "Ukrainian",
  "ur": "Urdu",
  "uz": "Uzbek",
  "vi": "Vietnamese",
  "wo": "Wolof",
  "xh": "Xhosa",
  "yi": "Yiddish",
  "yo": "Yoruba",
  "zh": "Chinese",
  "zu": "Zulu"
}

# =====================================================
# REQUEST MODEL
# =====================================================
class TranslateRequest(BaseModel):
    text: str
    source_lang: str
    target_lang: str

# =====================================================
# TRANSLATION
# =====================================================
def blocking_translate(text, source_lang, target_lang):
    tokenizer.src_lang = source_lang
    encoded = tokenizer(
        text, return_tensors="pt"
    ).to(device)
    generated_tokens = model.generate(
        **encoded,
        forced_bos_token_id=tokenizer.get_lang_id(target_lang),
        max_length=1024,
        num_beams=1
    )
    return tokenizer.batch_decode(
        generated_tokens, skip_special_tokens=True
    )[0]

# =====================================================
# CACHE HELPERS (NEW)
# =====================================================
def get_cache_key(text, source_lang, target_lang):
    """Generate a unique cache key for a translation request"""
    raw_key = f"{text}|{source_lang}|{target_lang}"
    return hashlib.md5(raw_key.encode()).hexdigest()

def get_from_cache(text, source_lang, target_lang):
    """Retrieve translation from cache if valid"""
    cache_key = get_cache_key(text, source_lang, target_lang)
    if cache_key in translation_cache:
        cached_data = translation_cache[cache_key]
        if time.time() - cached_data["timestamp"] < CACHE_TTL:
            return cached_data["translated_text"]
        else:
            # Remove expired cache entry
            del translation_cache[cache_key]
    return None

def add_to_cache(text, source_lang, target_lang, translated_text):
    """Add translation to cache with timestamp. Override oldest entry if cache is full."""
    cache_key = get_cache_key(text, source_lang, target_lang)
    
    # If key already exists, update it
    if cache_key in translation_cache:
        translation_cache[cache_key] = {
            "translated_text": translated_text,
            "timestamp": time.time()
        }
        return
    
    # If cache is full, find and remove the oldest entry
    if len(translation_cache) >= MAX_CACHE_SIZE:
        oldest_key = min(
            translation_cache.keys(),
            key=lambda k: translation_cache[k]["timestamp"]
        )
        del translation_cache[oldest_key]
    
    # Add new entry
    translation_cache[cache_key] = {
        "translated_text": translated_text,
        "timestamp": time.time()
    }

# =====================================================
# COOLDOWN HELPERS
# =====================================================
def is_blocked(url):
    return cooldown.get(url, 0) > time.time()

# =====================================================
# CHECK SINGLE SPACE
# =====================================================
async def check_space(client, i):
    space_name = SPACE_PREFIX if i == 0 else f"{SPACE_PREFIX}{i}"
    url = f"https://{USERNAME}-{space_name}.hf.space"
    
    if is_blocked(url):
        return {
            "exists": True,
            "space_name": space_name,
            "url": url,
            "status_data": {
                "current_space_status": "cooldown"
            }
        }
    
    try:
        response = await client.get(f"{url}/status")
        if response.status_code == 200:
            try:
                return {
                    "exists": True,
                    "space_name": space_name,
                    "url": url,
                    "status_data": response.json()
                }
            except:
                return {
                    "exists": True,
                    "space_name": space_name,
                    "url": url,
                    "status_data": None
                }
        elif response.status_code == 404:
            return {
                "exists": False,
                "space_name": space_name,
                "url": url
            }
        elif response.status_code == 429:
            cooldown[url] = time.time() + 1
            return {
                "exists": True,
                "space_name": space_name,
                "url": url,
                "status_data": {
                    "current_space_status": "rate_limited"
                }
            }
        else:
            return {
                "exists": True,
                "space_name": space_name,
                "url": url,
                "status_data": {
                    "current_space_status": "error"
                }
            }
    except:
        return {
            "exists": False,
            "space_name": space_name,
            "url": url
        }

# =====================================================
# DISCOVERY
# =====================================================
async def discover_spaces_parallel():
    existing_spaces = []
    empty_space_url = None
    timeout = httpx.Timeout(5.0)
    
    async with httpx.AsyncClient(timeout=timeout) as client:
        start = 0
        while True:
            tasks = [
                check_space(client, i)
                for i in range(start, start + DISCOVERY_CHUNK_SIZE)
            ]
            results = await asyncio.gather(*tasks)
            
            found_empty = False
            for result in results:
                if result["exists"]:
                    status = (result.get("status_data") or {}).get("current_space_status")
                    if status in ["rate_limited", "cooldown"]:
                        continue
                    existing_spaces.append({
                        "space_name": result["space_name"],
                        "url": result["url"],
                        "status": result.get("status_data")
                    })
                else:
                    empty_space_url = result["url"]
                    found_empty = True
                    break
            
            if found_empty:
                break
            
            start += DISCOVERY_CHUNK_SIZE
    
    return {
        "existing_spaces": existing_spaces,
        "empty_space_url": empty_space_url
    }

# =====================================================
# STATUS
# =====================================================
@app.get("/status")
async def status():
    queue_size = request_queue.qsize()
    full = (
        active_requests >= MAX_ACTIVE_REQUESTS
        and queue_size >= MAX_QUEUE_SIZE
    )
    discovered = await discover_spaces_parallel()
    return {
        "current_space_status": "full" if full else "available",
        "active_requests": active_requests,
        "queue_size": queue_size,
        "empty_space_url": discovered["empty_space_url"],
        "total_spaces": len(discovered["existing_spaces"]),
        "cached_translations": len(translation_cache)
    }

# =====================================================
# LANGUAGES WITH RPS LIMITING (UPDATED)
# =====================================================
@app.get("/languages")
async def languages(request: Request):
    async with languages_lock:
        current_time = time.time()
        
        # Remove requests older than 1 second
        global languages_request_times
        languages_request_times = [
            t for t in languages_request_times
            if current_time - t < 1.0
        ]
        
        # Check if we've exceeded the RPS limit
        if len(languages_request_times) >= MAX_RPS_LANGUAGES:
            # Discover empty space URL for redirection
            discovered = await discover_spaces_parallel()
            return JSONResponse(
                status_code=429,
                content={
                    "status": "space_full",
                    "message": f"Rate limit exceeded. Maximum {MAX_RPS_LANGUAGES} requests per second allowed.",
                    "empty_space_url": discovered["empty_space_url"]
                }
            )
        
        # Add current request timestamp
        languages_request_times.append(current_time)
    
    return SUPPORTED_LANGS

# =====================================================
# TRANSLATE
# =====================================================
@app.post("/translate")
async def translate(req: TranslateRequest):
    global active_requests
    
    # Character limit check (without spaces)
    char_count_without_spaces = len(req.text.replace(" ", ""))
    if char_count_without_spaces > MAX_CHARACTERS:
        return {
            "status": "character_limit_exceeded",
            "message": f"Text exceeds maximum character limit (without spaces). Maximum allowed: {MAX_CHARACTERS} characters, received: {char_count_without_spaces} characters",
            "max_characters": MAX_CHARACTERS,
            "received_characters": char_count_without_spaces
        }
    
    # Check cache first
    cached_result = get_from_cache(req.text, req.source_lang, req.target_lang)
    if cached_result is not None:
        return {
            "status": "success",
            "translated_text": cached_result,
            "from_cache": True
        }
    
    queue_size = request_queue.qsize()
    if active_requests >= MAX_ACTIVE_REQUESTS and queue_size >= MAX_QUEUE_SIZE:
        discovered = await discover_spaces_parallel()
        return {
            "status": "space_full",
            "empty_space_url": discovered["empty_space_url"]
        }
    
    await request_queue.put("req")
    async with lock:
        active_requests += 1
    try:
        loop = asyncio.get_running_loop()
        result = await loop.run_in_executor(
            executor,
            blocking_translate,
            req.text,
            req.source_lang,
            req.target_lang
        )
        
        # Store result in cache
        add_to_cache(req.text, req.source_lang, req.target_lang, result)
        
        return {
            "status": "success",
            "translated_text": result,
            "from_cache": False
        }
    finally:
        async with lock:
            active_requests -= 1
        await request_queue.get()
        request_queue.task_done()

# =====================================================
# HOME
# =====================================================
@app.get("/")
def home():
    return {"status": "running"}