File size: 23,886 Bytes
2ccd595
 
a5bd7b0
0048c63
2ac1c69
0048c63
 
 
2ac1c69
ef7feb8
214fdd3
0e9b3d2
319f13d
 
 
 
0e9b3d2
0048c63
 
 
 
 
d4e84c3
0048c63
 
 
 
 
a490a13
48286af
2ccd595
af35ade
c91e60a
 
2ccd595
ef7feb8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a532730
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ef7feb8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c91e60a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
af35ade
c91e60a
 
 
 
ef7feb8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2ccd595
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c4505e9
 
e6c8296
48286af
2ac1c69
 
48286af
2ac1c69
48286af
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2ac1c69
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3193e6f
0e9b3d2
 
 
 
 
 
 
 
 
3193e6f
0e9b3d2
 
 
 
 
 
 
 
 
3193e6f
 
0e9b3d2
e4d5e20
0e9b3d2
3193e6f
0e9b3d2
 
 
 
0c28fb5
 
 
 
 
 
 
 
 
 
c91e60a
2ccd595
f3d208d
2ccd595
 
c91e60a
 
2a185fb
ef7feb8
 
 
 
 
 
 
c4505e9
2ccd595
c91e60a
2ccd595
 
 
 
 
 
 
 
 
 
49bc6b1
2ccd595
3193e6f
0048c63
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
48286af
3223e7e
48286af
 
 
 
 
 
 
 
2ac1c69
ef7feb8
 
48286af
 
 
ef7feb8
 
 
 
 
 
a532730
 
ef7feb8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bc1a853
 
48286af
ef7feb8
48286af
 
 
 
a532730
 
 
 
4c17342
48286af
a532730
 
 
 
 
ef7feb8
 
 
 
 
 
 
 
 
 
 
a532730
64109a6
ef7feb8
 
 
 
 
 
 
 
 
 
 
 
 
6a330f5
2ac1c69
48286af
8219688
214fdd3
 
 
 
2ac1c69
48286af
 
 
 
2ac1c69
 
 
48286af
 
 
2ac1c69
 
 
 
 
6a330f5
 
 
2ac1c69
 
 
6a330f5
 
 
 
 
 
 
 
 
48286af
 
 
 
 
 
c91e60a
1a9495c
c91e60a
0094ac7
c91e60a
 
f3d208d
2a185fb
f0e91d0
c91e60a
 
70ec4e5
a490a13
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e6c8296
a490a13
 
 
70ec4e5
 
 
 
0e9b3d2
0f2ea77
0e9b3d2
0f2ea77
 
 
 
 
 
0e9b3d2
c91e60a
0094ac7
c91e60a
5c798e3
 
5d017c1
5c798e3
 
 
a490a13
5c798e3
 
0cdccf3
 
0e9b3d2
 
0cdccf3
 
0e9b3d2
 
5c798e3
0e9b3d2
 
5c798e3
 
0e9b3d2
5c798e3
 
 
 
 
 
 
 
 
0e9b3d2
5c798e3
 
 
 
 
 
 
 
0e9b3d2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5c798e3
0e9b3d2
 
 
 
5c798e3
 
70ec4e5
 
 
 
 
 
5c798e3
 
 
0e9b3d2
 
 
 
 
319f13d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6aea2b7
 
 
33046d3
6aea2b7
 
 
 
 
 
592f567
6aea2b7
 
 
 
 
592f567
 
 
04c47fc
 
158f766
 
04c47fc
 
592f567
 
 
 
 
657b7f6
592f567
 
 
 
 
158f766
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6aea2b7
158f766
6aea2b7
 
158f766
6aea2b7
158f766
 
 
 
0e9b3d2
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
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
import os
import time
from fastapi import FastAPI, Request, HTTPException
from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import Response, JSONResponse, StreamingResponse
import httpx
from bs4 import BeautifulSoup
from typing import List, Dict
import asyncio
import re
from random import randint
from urllib.parse import quote
import uuid

WAN_SPACE = "https://huggingface.co/spaces/Wan-AI/Wan2.1"
WAN_API_BASE = f"{WAN_SPACE}/gradio_api"

app = FastAPI()

app.add_middleware(
    CORSMiddleware,
    allow_origins=["*"],
    allow_methods=["GET", "POST", "HEAD"],
    allow_headers=["*"],
)

OLLAMA_LIBRARY_URL = "https://ollama.com/library"

RATE_LIMIT = 60
WINDOW_SECONDS = 60 * 60 * 24
ip_store = {}  # { ip: { "count": int, "reset": timestamp } }
AUDIO_RATE_LIMIT = 10
AUDIO_WINDOW_SECONDS = 60 * 60 * 24
audio_ip_store = {}

REASONING_KEYWORDS = [
    # explicit reasoning requests
    "prove", "demonstrate", "derive", "justify", "verify",
    "show that", "walk through", "step by step", "reason through",
    "chain of reasoning", "rigorous", "formal proof",

    # analysis/comparison
    "analyze", "analysis of", "compare and contrast",
    "evaluate", "critically assess", "explain why",
    "explain how", "what causes", "implications of",

    # problem solving
    "solve", "solution to", "how would you approach",
    "strategy for", "optimize", "algorithm for",

    # technical domains
    "theorem", "lemma", "corollary",
    "complexity analysis", "big o", "time complexity",
    "mathematical", "statistical", "probabilistic",
    "model the", "simulate",
]

CODE_KEYWORDS = [
    "await", "async", "print(", "console.log(",
    "code", ".ts", ".js", ".py", ".repy", ".rb",
    "gnu", "gcc", "clang", "clang++", "program",
    "coding"
]

CREATIVE_KEYWORDS = [
    # cinematic cues
    "cinematic", "film still", "movie scene",
    "epic", "dramatic lighting", "moody lighting",
    "volumetric lighting", "depth of field",
    "anamorphic lens", "8k", "4k",

    # art styles
    "concept art", "digital painting",
    "fantasy art", "sci-fi", "mythical",
    "cyberpunk", "steampunk",
    "baroque", "surreal", "abstract",
    "oil painting", "watercolor",

    # rendering engines
    "octane render", "unreal engine",
    "ray tracing", "global illumination",

    # emotional narrative framing
    "emotional portrait", "story scene",
    "hero shot", "dramatic pose",
]

STRUCTURED_KEYWORDS = [
    "return as json",
    "output json",
    "json schema",
    "format as json",
    "structured output",
    "extract entities",
    "extract fields",
    "parse this",
    "convert to table",
    "create a table",
    "categorize into",
    "classify",
    "label the following",
    "taxonomy",
    "generate schema",
]

MATH_PATTERNS = [
    r"\b∫\b", r"\b∑\b", r"\b∂\b",
    r"\bmatrix\b",
    r"\blimit\b",
    r"\bintegral\b",
    r"\bderivative\b",
    r"\bdifferential equation\b",
    r"\blinear algebra\b",
    r"\boptimi[sz]e\b",
    r"\bgradient\b",
    r"\bbackprop\b",
    r"\bproof\b",
    r"\btheorem\b",
]

LIGHTWEIGHT_KEYWORDS = [
    "hello", "hi", "hey",
    "thanks", "thank you",
    "define", "definition of",
    "what is", "who is",
    "quick question",
    "short answer",
    "brief explanation",
    "summarize",
    "paraphrase",
    "rewrite this",
]

def is_long_context(messages: list) -> bool:
    total_chars = sum(len(m.get("content", "")) for m in messages)
    return total_chars > 4000


def contains_code(prompt: str) -> bool:
    if "```" in prompt:
        return True
    for kw in CODE_KEYWORDS:
        if kw in prompt:
            return True
    return False

def is_code_heavy(prompt: str, code_present: bool, long_context: bool) -> bool:
    """
    Determines whether the coding task is substantial enough
    to require a code-optimized or larger model.
    """

    if not code_present:
        return False

    heavy_patterns = [
        r"\brefactor\b",
        r"\boptimi[sz]e\b",
        r"\bdebug\b",
        r"\bfix this\b",
        r"\barchitecture\b",
        r"\bdesign pattern\b",
        r"\bscalable\b",
        r"\bmicroservice\b",
        r"\bmultiple files\b",
        r"\bentire project\b",
        r"\bcodebase\b",
        r"\bperformance\b",
    ]

    for pattern in heavy_patterns:
        if re.search(pattern, prompt):
            return True

    if prompt.count("```") >= 2:
        return True

    if long_context:
        return True

    return False

def is_math_heavy(prompt: str) -> bool:
    for pattern in MATH_PATTERNS:
        if re.search(pattern, prompt):
            return True
    return False


def is_structured_task(prompt: str) -> bool:
    for kw in STRUCTURED_KEYWORDS:
        if kw in prompt:
            return True
    return False


def multiple_questions(prompt: str) -> bool:
    return prompt.count("?") >= 3

def extract_user_text(messages: list) -> str:
    return " ".join(
        m.get("content", "")
        for m in messages
        if m.get("role") == "user"
    ).lower()

def check_audio_rate_limit(ip: str):
    now = time.time()

    if ip not in audio_ip_store:
        audio_ip_store[ip] = {
            "count": 0,
            "reset": now + AUDIO_WINDOW_SECONDS
        }

    entry = audio_ip_store[ip]

    if now > entry["reset"]:
        entry["count"] = 0
        entry["reset"] = now + AUDIO_WINDOW_SECONDS

    if entry["count"] >= AUDIO_RATE_LIMIT:
        raise HTTPException(
            status_code=429,
            detail="Daily audio limit reached: 10 per IP"
        )

    entry["count"] += 1


def is_complex_reasoning(prompt: str) -> bool:
    if len(prompt) > 800:
        return True

    for kw in REASONING_KEYWORDS:
        if kw in prompt:
            return True

    if re.search(r"\b(if|therefore|assume|let x|given that)\b", prompt):
        return True

    return False


def is_lightweight(prompt: str) -> bool:
    if len(prompt) < 100:
        for kw in LIGHTWEIGHT_KEYWORDS:
            if kw in prompt:
                return True
    return False


def is_cinematic_image_prompt(prompt: str) -> bool:
    for kw in CREATIVE_KEYWORDS:
        if kw in prompt.lower():
            return True
    return False

def check_rate_limit(ip: str):
    now = time.time()

    if ip not in ip_store:
        ip_store[ip] = {"count": 0, "reset": now + WINDOW_SECONDS}

    entry = ip_store[ip]

    if now > entry["reset"]:
        entry["count"] = 0
        entry["reset"] = now + WINDOW_SECONDS

    if entry["count"] >= RATE_LIMIT:
        raise HTTPException(
            status_code=429,
            detail="Daily limit reached: 25 images per IP"
        )

    entry["count"] += 1

PKEY  = os.getenv("POLLINATIONS_KEY", "")
PKEY2 = os.getenv("POLLINATIONS2_KEY", "")
PKEY3 = os.getenv("POLLINATIONS3_KEY", "")

CHAT_RATE_LIMIT = 50
CHAT_WINDOW_SECONDS = 60 * 60

chat_ip_store = {}

GROQ_TOOL_MODELS = [
    "openai/gpt-oss-120b",
    "openai/gpt-oss-20b",
    "meta-llama/llama-4-scout-17b-16e-instruct",
    "qwen/qwen3-32b",
    "moonshotai/kimi-k2-instruct",
]

GROQ_NORMAL_MODELS = [
    "llama-3.1-8b-instant",
    "llama-3.3-70b-versatile",
    "meta-llama/llama-4-maverick-17b-128e-instruct",
    "meta-llama/llama-guard-4-12b",
    "openai/gpt-oss-safeguard-20b",
    "qwen/qwen3-32b",
]

CEREBRAS_MODELS = [
    "gpt-oss-120b",
    "llama3.1-8b",
    "qwen-3-235b-a22b-instruct-2507",
    "zai-glm-4.7",
]

def check_chat_rate_limit(ip: str):
    now = time.time()

    if ip not in chat_ip_store:
        chat_ip_store[ip] = {
            "count": 0,
            "reset": now + CHAT_WINDOW_SECONDS
        }

    entry = chat_ip_store[ip]

    if now > entry["reset"]:
        entry["count"] = 0
        entry["reset"] = now + CHAT_WINDOW_SECONDS

    if entry["count"] >= CHAT_RATE_LIMIT:
        raise HTTPException(
            status_code=429,
            detail="Chat rate limit exceeded"
        )

    entry["count"] += 1
    return entry["count"]

@app.head("/status/sfx")
async def head_sfx():
    return Response(
        status_code=200,
        headers={
            "Content-Type": "audio/mpeg",
            "Accept-Ranges": "bytes",
        }
    )

@app.head("/status/image")
async def head_image():
    return Response(
        status_code=200,
        headers={
            "Content-Type": "image/jpeg",
            "Accept-Ranges": "bytes",
        }
    )

@app.head("/status/video")
async def head_video():
    return Response(
        status_code=207,
        headers={
            "Content-Type": "video/mp4",
            "Accept-Ranges": "bytes",
        }
    )

@app.head("/status/text")
async def head_video():
    return Response(
        status_code=200,
        headers={
            "Content-Type": "video/mp4",
            "Accept-Ranges": "bytes",
        }
    )

@app.post("/gen/image")
@app.get("/genimg/{prompt}")
async def generate_image(request: Request, prompt: str = None):
    client_ip = request.client.host
    check_rate_limit(client_ip)
    timeout = httpx.Timeout(300.0, read=300.0)
    if prompt is None:
        prompt = (await request.json()).get("prompt")
    if is_cinematic_image_prompt(prompt):
        chosen_model = "flux"
    else:
        chosen_model = "zimage"

    print(f"[IMAGE GEN] Routing to model: {chosen_model}")

    url = f"https://gen.pollinations.ai/image/{prompt}?model={chosen_model}&key={PKEY2}"

    async with httpx.AsyncClient(timeout = timeout) as client:
        response = await client.get(url)

    if response.status_code != 200:
        raise HTTPException(
            status_code=500,
            detail=f"Pollinations error: {response.status_code}"
        )

    return Response(
        content=response.content,
        media_type="image/jpeg"
    )
@app.head("/models")
@app.get("/models")
async def get_models() -> List[Dict]:
    async with httpx.AsyncClient() as client:
        response = await client.get(OLLAMA_LIBRARY_URL)
        html = response.text

    soup = BeautifulSoup(html, "html.parser")
    items = soup.select("li[x-test-model]")

    models = []
    for item in items:
        name = item.select_one("[x-test-model-title] span")
        description = item.select_one("p.max-w-lg")
        sizes = [el.get_text(strip=True) for el in item.select("[x-test-size]")]
        pulls = item.select_one("[x-test-pull-count]")
        tags = [t.get_text(strip=True) for t in item.select('span[class*="text-blue-600"]')]
        updated = item.select_one("[x-test-updated]")
        link = item.select_one("a")

        models.append({
            "name": name.get_text(strip=True) if name else "",
            "description": description.get_text(strip=True) if description else "No description",
            "sizes": sizes,
            "pulls": pulls.get_text(strip=True) if pulls else "Unknown",
            "tags": tags,
            "updated": updated.get_text(strip=True) if updated else "Unknown",
            "link": link.get("href") if link else None,
        })

    return models

@app.post("/gen/chat/completions")
async def generate_text(request: Request):
    body = await request.json()

    messages = body.get("messages", [])
    if not isinstance(messages, list) or len(messages) == 0:
        raise HTTPException(400, "messages[] is required")

    ip = request.client.host
    msg_count = check_chat_rate_limit(ip)
    prompt_text = extract_user_text(messages)
    
    uses_tools = (
        "tools" in body and isinstance(body["tools"], list) and len(body["tools"]) > 0
    ) or ("tool_choice" in body and body["tool_choice"] not in [None, "none"])
    
    long_context = is_long_context(messages)
    code_present = contains_code(prompt_text)
    math_heavy = is_math_heavy(prompt_text)
    structured_task = is_structured_task(prompt_text)
    multi_q = multiple_questions(prompt_text)
    code_heavy = is_code_heavy(prompt_text, code_present, long_context)

    score = 0
    
    if long_context:
        score += 3
    
    if math_heavy:
        score += 3
    
    if structured_task:
        score += 2
    
    if code_present:
        score += 2
    
    if multi_q:
        score += 1
    
    for kw in REASONING_KEYWORDS:
        if kw in prompt_text:
            score += 1
    
    chosen_model = "llama-3.1-8b-instant"
    provider = "groq"
    if score > 10:
        score = 10
    if uses_tools:
        if score >= 4:
            chosen_model = "openai/gpt-oss-120b"
        else:
            chosen_model = "openai/gpt-oss-20b"
        provider = "groq"

    elif code_present:
    
        if code_heavy and score >= 6:
            chosen_model = "gpt-oss-120b"
            provider = "cerebras"
    
        elif score >= 4:
            chosen_model = "llama-3.3-70b-versatile"
            provider = "groq"

    elif score >= 6:
        chosen_model = "gpt-oss-120b"
        provider = "cerebras"
    
    elif score >= 4:
        chosen_model = "llama-3.3-70b-versatile"
        provider = "groq"
    
    elif score >= 3 and structured_task:
        chosen_model = "qwen-3-235b-a22b-instruct-2507"
        provider = "cerebras"
        
    body["model"] = chosen_model
    print(f"""
    [ADVANCED ROUTER]
      Score: {score}
      Uses tools: {uses_tools}
      Long context: {long_context}
      Code present: {code_present}
      Math heavy: {math_heavy}
      Structured: {structured_task}
      Multi-question: {multi_q}
      → Selected: {chosen_model} ({provider})
    """)


    stream = body.get("stream", False)

    if provider == "groq":
        num = randint(1, 2)
        if num == 1:
            API_KEY = os.getenv("GROQ_KEY", "")
        elif num == 2:
            API_KEY = os.getenv("GROQ2_KEY", "")
        if not API_KEY:
            raise HTTPException(500, "Missing GROQ_KEY")

        url = "https://api.groq.com/openai/v1/chat/completions"

    elif provider == "cerebras":
        API_KEY = os.getenv("CER_KEY", "")
        if not API_KEY:
            raise HTTPException(500, "Missing CER_KEY")

        url = "https://api.cerebras.ai/v1/chat/completions"
    else:
        raise HTTPException(500, "Unknown provider routing error")

    headers = {"Authorization": f"Bearer {API_KEY}"}

    if stream:
        async def event_generator():
            async with httpx.AsyncClient(timeout=None) as client:
                async with client.stream("POST", url, json=body, headers=headers) as r:
                    async for chunk in r.aiter_raw():
                        yield chunk
    
        return StreamingResponse(
            event_generator(),
            media_type="text/event-stream",
        )
    else:
        async with httpx.AsyncClient(timeout=None) as client:
            r = await client.post(url, json=body, headers=headers)
        
        return JSONResponse(
            status_code=r.status_code,
            content=r.json()
        )

    raise HTTPException(500, "Unknown provider routing error")

@app.get("/gen/sfx/{prompt}")
@app.post("/gen/sfx")
async def gensfx(request: Request, prompt: str = None):
    client_ip = request.client.host
    check_audio_rate_limit(client_ip)
    if prompt is None:
        prompt = (await request.json()).get("prompt")
    url = f"https://gen.pollinations.ai/audio/{prompt}?model=elevenmusic&key={PKEY}"
    async with httpx.AsyncClient(timeout=None) as client:
        response = await client.get(url)
    body_text = ""
    try:
        body_text = response.text
    except Exception:
        pass
    if response.status_code != 200:
        return JSONResponse(
            status_code=response.status_code,
            content={
                "success": False,
                "error": "Upstream music/sfx generation failed",
                "status_code": response.status_code,
                "message": body_text[:1000]
            }
        )
    return Response(
        response.content,
        media_type="audio/mpeg"
    )

@app.get("/gen/tts/{prompt}")
@app.post("/gen/tts")
async def gensfx(request: Request, prompt: str = None):
    client_ip = request.client.host
    check_rate_limit(client_ip)
    if prompt is None:
        prompt = (await request.json()).get("prompt")
    url = f"https://gen.pollinations.ai/audio/{prompt}?key={PKEY3}"
    async with httpx.AsyncClient(timeout=None) as client:
        response = await client.get(url)
    body_text = ""
    try:
        body_text = response.text
    except Exception:
        pass
    if response.status_code != 200:
        return JSONResponse(
            status_code=response.status_code,
            content={
                "success": False,
                "error": "Upstream audio generation failed",
                "status_code": response.status_code,
                "message": body_text[:1000]
            }
        )
    return Response(
        response.content,
        media_type="audio/mpeg"
    )

@app.get("/gen/video/{prompt}")
@app.post("/gen/video")
async def genvideo(request: Request, prompt: str = None):
    client_ip = request.client.host
    check_rate_limit(client_ip)

    if prompt is None:
        return RedirectResponse(url="/gen/video/airforce", status_code=status.HTTP_307_TEMPORARY_REDIRECT)

        body = await request.json()
        prompt = body.get("prompt")
    else:
        return RedirectResponse(url=f"/gen/video/airforce/{prompt}", status_code=status.HTTP_307_TEMPORARY_REDIRECT)
    if not prompt:
        raise HTTPException(400, "Prompt is required")

    encoded_prompt = quote(prompt)
    url = f"https://gen.pollinations.ai/image/{encoded_prompt}?model=grok-video&key={PKEY}"

    async def fetch_with_retry(url, max_retries=6):
        async with httpx.AsyncClient(timeout=300.0) as client:
            for attempt in range(max_retries):
                response = await client.get(url)

                if response.status_code == 200:
                    return response

                body_text = ""
                try:
                    body_text = response.text
                except Exception:
                    pass

                if response.status_code == 429 and "api.airforce" in body_text:
                    wait = 0.5 * (2 ** attempt)
                    print(f"[VIDEO RETRY] 429 from api.airforce, retrying in {wait:.2f}s...")
                    await asyncio.sleep(wait)
                    continue

                content_type = response.headers.get("content-type", "")

                if "application/json" in content_type:
                    return JSONResponse(
                        status_code=response.status_code,
                        content=response.json()
                    )
                else:
                    return JSONResponse(
                        status_code=response.status_code,
                        content={
                            "success": False,
                            "error": "Upstream video generation failed",
                            "status_code": response.status_code,
                            "message": body_text[:1000]
                        }
                    )

            return JSONResponse(
                status_code=503,
                content={
                    "success": False,
                    "error": "Pollinations video generation overloaded (api.airforce). Try again later."
                }
            )

    result = await fetch_with_retry(url)
    
    if isinstance(result, JSONResponse):
        return result
    
    response = result

    return Response(
        content=response.content,
        media_type="video/mp4",
        headers={
            "Content-Length": str(len(response.content)),
            "Accept-Ranges": "bytes"
        }
    )

@app.get("/gen/video/exp/{prompt}")
@app.post("/gen/video/exp")
async def genvideo_wan(request: Request, prompt: str = None):
    client_ip = request.client.host
    check_rate_limit(client_ip)

    if prompt is None:
        body = await request.json()
        prompt = body.get("prompt")

    if not prompt:
        raise HTTPException(400, "Prompt is required")

    session_hash = uuid.uuid4().hex

    join_payload = {
        "fn_index": 0,
        "session_hash": session_hash,
        "data": [prompt],
    }

    async with httpx.AsyncClient(timeout=60.0) as client:
        # 1️⃣ Join queue
        join = await client.post(
            f"{WAN_API_BASE}/queue/join",
            json=join_payload,
        )

        if join.status_code != 200:
            raise HTTPException(
                join.status_code,
                f"Failed to join WAN queue: {join.text}"
            )

        for _ in range(120):
            await asyncio.sleep(1)

            poll = await client.get(
                f"{WAN_API_BASE}/queue/data",
                params={"session_hash": session_hash},
            )

            if poll.status_code != 200:
                continue

            data = poll.json()

            if data.get("status") == "complete":
                try:
                    video_url = data["data"][0]
                except Exception:
                    raise HTTPException(
                        500,
                        "WAN completed but returned unexpected format"
                    )

                # 3️⃣ Fetch video
                video = await client.get(video_url)

                if video.status_code != 200:
                    raise HTTPException(
                        502,
                        "Failed to fetch generated video"
                    )

                return Response(
                    content=video.content,
                    media_type="video/mp4",
                    headers={
                        "Content-Length": str(len(video.content)),
                        "Accept-Ranges": "bytes",
                    }
                )

            if data.get("status") == "error":
                raise HTTPException(
                    500,
                    f"WAN generation error: {data}"
                )

    raise HTTPException(
        504,
        "WAN video generation timed out"
    )

AIRFORCE_KEY = os.getenv("AIRFORCE")
AIRFORCE_VIDEO_MODEL = "wan-2.6"
AIRFORCE_API_URL = "https://api.airforce/v1/images/generations"

MAX_VIDEO_RETRIES = 6

@app.get("/gen/video/airforce/{prompt}")
@app.post("/gen/video/airforce")
async def genvideo_airforce(request: Request, prompt: str = None):
    if prompt is None:
        body = await request.json()
        prompt = body.get("prompt")
        duration = body.get("duration", 5)

    body = {
        "model": "grok-imagine-video",
        "prompt": prompt,
        "n": 1,
        "size": "1024x1024",
        "response_format": "b64_json",
        "sse": False,
        "mode": "normal",
        "aspectRatio": "3:2"
    }

    async with httpx.AsyncClient(timeout=600) as client:
        resp = await client.post(
            "https://api.airforce/v1/images/generations",
            headers={"Authorization": f"Bearer {AIRFORCE_KEY}", "Content-Type": "application/json"},
            json=body
        )

    if resp.status_code != 200:
        return JSONResponse(status_code=resp.status_code, content=resp.json())
    if not resp.content:
        raise HTTPException(
            status_code=502,
            detail="api.airforce returned empty response"
        )

    try:
        result = resp.json()
        b64_video = result["data"][0]["b64_json"]
    except Exception:
        raise HTTPException(
            502,
            f"Invalid api.airforce response: {resp.text[:500]}"
        )

    if not b64_video:
        raise HTTPException(502, "Airforce returned empty b64_json")

    video_bytes = base64.b64decode(b64_video)

    return Response(
        content=video_bytes,
        media_type="video/mp4",
        headers={
            "Content-Length": str(len(video_bytes)),
            "Accept-Ranges": "bytes",
        },
    )