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
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import os
import json
import time
import logging
from typing import Any, Dict, List, Optional, Tuple
from fastapi import FastAPI, HTTPException, Request
from fastapi.responses import StreamingResponse, JSONResponse
from pydantic import BaseModel
import g4f

logging.basicConfig(level=logging.INFO)
logger = logging.getLogger("g4f-smart-router")

# =====================================================
# COOKIES
# =====================================================
def _load_cookies_raw() -> Dict[str, Any]:
    raw_env = (os.getenv("COOKIES_JSON") or "").strip()

    if raw_env:
        try:
            return json.loads(raw_env)
        except Exception as e:
            logger.warning(e)

    try:
        if os.path.exists("cookies.json"):
            with open("cookies.json", "r", encoding="utf-8") as f:
                return json.load(f)
    except Exception as e:
        logger.warning(e)

    return {}

def load_cookies() -> str:
    data = _load_cookies_raw()

    if not data:
        return "⚠️ No Cookies"

    try:
        from g4f.cookies import set_cookies
    except Exception:
        return "⚠️ Cookies Found"

    for domain, vals in data.items():
        try:
            dom = domain if "." in domain else f".{domain}.com"

            if isinstance(vals, list):
                vals = {
                    x["name"]: x["value"]
                    for x in vals
                    if isinstance(x, dict)
                }

            if isinstance(vals, dict):
                set_cookies(dom, vals)

        except Exception as e:
            logger.warning(e)

    return "✅ Cookies Loaded"

COOKIE_STATUS = load_cookies()

# =====================================================
# PROVIDERS
# =====================================================
def get_provider(name: str):
    try:
        return getattr(g4f.Provider, name)
    except:
        return None

REAL_PROVIDERS: Dict[str, Any] = {
    "Perplexity": get_provider("Perplexity") or get_provider("PerplexityAi"),
    "Copilot": get_provider("Copilot"),
    "Qwen": get_provider("Qwen"),
    "Blackbox": get_provider("Blackbox"),
    "DeepSeek": get_provider("DeepSeek"),
    "HuggingFace": get_provider("HuggingFace"),
    "Cloudflare": get_provider("Cloudflare"),
    "You": get_provider("You"),
    "Bing": get_provider("Bing"),
}

REAL_PROVIDERS = {k: v for k, v in REAL_PROVIDERS.items() if v}

# =====================================================
# MODELS
# =====================================================
PROVIDER_MODELS_FALLBACK: Dict[str, List[str]] = {
    "Perplexity": ["sonar", "sonar-pro", "gpt-4o", "llama-3"],
    "Copilot": ["gpt-4o", "gpt-4", "turbo"],
    "Qwen": ["qwen-max", "qwen-plus", "qwen-turbo", "qwen"],
    "Blackbox": ["gpt-4o", "claude-3", "gemini-pro", "llama-3"],
    "DeepSeek": ["deepseek-chat", "deepseek-coder"],
    "HuggingFace": ["meta-llama", "mistral", "qwen", "gemma"],
    "Cloudflare": ["llama-3", "mistral", "qwen"],
    "You": ["gpt-4o", "claude", "llama-3"],
    "Bing": ["gpt-4o", "gpt-4"],
}

# =====================================================
# MODEL DISCOVERY
# =====================================================
def _normalize_model_list(x: Any) -> List[str]:
    out: List[str] = []

    if x is None:
        return out

    if isinstance(x, dict):
        out = [str(k) for k in x.keys()]
    elif isinstance(x, (list, tuple, set)):
        out = [str(i) for i in x]
    else:
        out = [str(x)]

    out = [m.strip() for m in out if m]

    seen = set()
    uniq = []

    for m in out:
        if m not in seen:
            uniq.append(m)
            seen.add(m)

    return uniq

def discover_provider_models(provider_obj: Any, provider_name: str) -> List[str]:

    candidates = []

    for attr in (
        "models",
        "model",
        "default_model",
        "available_models",
        "supported_models"
    ):
        try:
            if hasattr(provider_obj, attr):
                candidates += _normalize_model_list(
                    getattr(provider_obj, attr)
                )
        except:
            pass

    for fn in ("get_models", "models_list", "list_models"):
        try:
            if hasattr(provider_obj, fn):
                candidates += _normalize_model_list(
                    getattr(provider_obj, fn)()
                )
        except:
            pass

    if not candidates:
        candidates = PROVIDER_MODELS_FALLBACK.get(
            provider_name,
            ["gpt-4o"]
        )

    seen = set()
    uniq = []

    for m in candidates:
        if m not in seen:
            uniq.append(m)
            seen.add(m)

    return uniq

# =====================================================
# STREAM CLEANER
# =====================================================
def clean_stream(chunk):
    try:
        if isinstance(chunk, dict):
            if 'choices' in chunk and chunk['choices']:
                delta = chunk['choices'][0].get('delta', {})
                if 'content' in delta:
                    return delta['content']
                if 'text' in delta:
                    return delta['text']
            if 'content' in chunk:
                return chunk['content']
            if 'text' in chunk:
                return chunk['text']
            return ""

        if isinstance(chunk, str):
            if chunk.strip().startswith("{") and chunk.strip().endswith("}"):
                try:
                    data = json.loads(chunk)
                    if 'choices' in data and data['choices']:
                        delta = data['choices'][0].get('delta', {})
                        if 'content' in delta:
                            return delta['content']
                    if 'content' in data:
                        return data['content']
                    if 'text' in data:
                        return data['text']
                except:
                    pass

            chunk = chunk.replace('\\n', '\n').replace('\\r', '\r').replace('\\t', '    ')
            chunk = chunk.replace('\\"', '"').replace("\\'", "'")
            return chunk

        return str(chunk)
    except Exception as e:
        logger.warning(f"خطأ في clean_stream: {e}")
        return ""

# =====================================================
# CACHE
# =====================================================
CACHE = {}
CACHE_TS = {}

def _cache_get(key):
    if key not in CACHE:
        return None
    return CACHE[key]

def _cache_set(key, val):
    CACHE[key] = val
    CACHE_TS[key] = time.time()

# =====================================================
# CHAT LOGIC
# =====================================================
_PROVIDER_MODEL_CACHE = {}

def ask(message: str, history, provider_name: str, model_name: str):
    history = history or []
    message = (message or "").strip()

    if not message:
        yield ""
        return

    key = f"{provider_name}|{model_name}|{message}"

    cached = _cache_get(key)

    if cached is not None:
        yield cached
        return

    msgs = []

    try:
        if history:
            if isinstance(history[0], dict):
                for item in history[-40:]:
                    role = item.get("role")
                    content = item.get("content")
                    if role and content:
                        msgs.append({
                            "role": str(role),
                            "content": str(content)
                        })
            else:
                for item in history[-20:]:
                    if isinstance(item, (list, tuple)) and len(item) == 2:
                        u = item[0]
                        a = item[1]
                        if u:
                            msgs.append({
                                "role": "user",
                                "content": str(u)
                            })
                        if a:
                            msgs.append({
                                "role": "assistant",
                                "content": str(a)
                            })
    except Exception as e:
        logger.warning(e)

    msgs.append({
        "role": "user",
        "content": message
    })

    fallback_providers = [
        provider_name,
        "Perplexity",
        "Copilot",
        "Blackbox",
        "DeepSeek",
        "Bing",
        "You",
        "Qwen"
    ]

    used = []

    for pname in fallback_providers:
        if pname in used:
            continue

        used.append(pname)

        pobj = REAL_PROVIDERS.get(pname)

        if not pobj:
            continue

        if pname not in _PROVIDER_MODEL_CACHE:
            _PROVIDER_MODEL_CACHE[pname] = discover_provider_models(
                pobj,
                pname
            )

        model_candidates = [model_name] + [
            x for x in _PROVIDER_MODEL_CACHE[pname]
            if x != model_name
        ]

        for m in model_candidates[:12]:
            try:
                stream = g4f.ChatCompletion.create(
                    model=m,
                    provider=pobj,
                    messages=msgs,
                    stream=True
                )

                text = ""

                for chunk in stream:
                    c = clean_stream(chunk)

                    if c is None or c == "":
                        continue

                    text += c
                    yield text

                if text.strip():
                    _cache_set(key, text)
                    return

            except Exception as e:
                logger.warning(e)
                continue

    yield "❌ Failed with all providers."

# =====================================================
# FASTAPI
# =====================================================

app = FastAPI(title="G4F API", description="G4F Smart Router API")

API_KEY = os.getenv("API_KEY", "mysecretkey123")

class ChatRequest(BaseModel):
    message: str
    provider: str = "Perplexity"
    model: str = "sonar"
    history: List[Any] = []

# =====================================================
# تعديل دالة التحقق لدعم X-API-Key مع الاحتفاظ بـ Bearer
# =====================================================
def verify_api_key(request: Request):
    # الطريقة الأولى: Authorization: Bearer XXX
    auth = request.headers.get("Authorization")
    if auth and auth.startswith("Bearer "):
        key = auth.replace("Bearer ", "")
        if key == API_KEY:
            return True
    
    # الطريقة الثانية: X-API-Key: XXX
    x_api_key = request.headers.get("X-API-Key")
    if x_api_key and x_api_key == API_KEY:
        return True
    
    # لا يوجد مفتاح صالح
    raise HTTPException(
        status_code=401, 
        detail="Invalid or missing API key. Use 'Authorization: Bearer KEY' or 'X-API-Key: KEY'"
    )

# =====================================================
# نقاط النهاية (بدون تغيير)
# =====================================================

@app.get("/")
async def root():
    return {
        "message": "G4F API is running",
        "endpoints": {
            "GET /": "هذه الصفحة",
            "GET /health": "التحقق من صحة الخادم",
            "GET /providers": "قائمة المزودين والنماذج (يتطلب مفتاح)",
            "POST /chat": "إرسال رسالة والحصول على رد (يتطلب مفتاح)",
            "POST /chat/stream": "إرسال رسالة والحصول على رد متدفق (يتطلب مفتاح)"
        },
        "authentication": "Bearer YOUR_API_KEY or X-API-Key: YOUR_API_KEY",
        "status": "✅ Server is working"
    }

@app.get("/health")
async def health():
    return {"status": "ok", "cookies": COOKIE_STATUS, "providers": list(REAL_PROVIDERS.keys())}

@app.post("/chat")
async def chat(request: Request, chat_req: ChatRequest):
    verify_api_key(request)
    
    full_response = ""
    for chunk in ask(chat_req.message, chat_req.history, chat_req.provider, chat_req.model):
        full_response = chunk
    
    return JSONResponse(content={"response": full_response})

@app.post("/chat/stream")
async def chat_stream(request: Request, chat_req: ChatRequest):
    verify_api_key(request)
    
    async def generate():
        for chunk in ask(chat_req.message, chat_req.history, chat_req.provider, chat_req.model):
            yield f"data: {json.dumps({'chunk': chunk})}\n\n"
        yield "data: [DONE]\n\n"
    
    return StreamingResponse(generate(), media_type="text/event-stream")

@app.get("/providers")
async def get_providers(request: Request):
    verify_api_key(request)
    
    providers_info = {}
    for pname, pobj in REAL_PROVIDERS.items():
        if pname not in _PROVIDER_MODEL_CACHE:
            _PROVIDER_MODEL_CACHE[pname] = discover_provider_models(pobj, pname)
        providers_info[pname] = _PROVIDER_MODEL_CACHE[pname]
    
    return JSONResponse(content={"providers": providers_info})

if __name__ == "__main__":
    import uvicorn
    port = int(os.getenv("PORT", "7860"))
    uvicorn.run(app, host="0.0.0.0", port=port)