Update app.py
Browse files
app.py
CHANGED
|
@@ -1,35 +1,30 @@
|
|
| 1 |
-
|
| 2 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
|
| 4 |
-
# =====================================================
|
| 5 |
-
# SMART G4F GATEWAY v3.0
|
| 6 |
-
# =====================================================
|
| 7 |
|
| 8 |
import time
|
| 9 |
import random
|
| 10 |
import traceback
|
| 11 |
-
import logging
|
| 12 |
|
| 13 |
from fastapi import FastAPI, HTTPException, Request
|
| 14 |
from fastapi.middleware.cors import CORSMiddleware
|
| 15 |
-
from fastapi.responses import JSONResponse
|
| 16 |
from pydantic import BaseModel
|
| 17 |
-
|
| 18 |
import uvicorn
|
| 19 |
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
# =====================================================
|
| 23 |
-
|
| 24 |
-
try:
|
| 25 |
-
|
| 26 |
-
from g4f.client import Client
|
| 27 |
-
import g4f.Provider as Provider
|
| 28 |
-
|
| 29 |
-
except Exception as e:
|
| 30 |
-
|
| 31 |
-
print(f"[FATAL] G4F IMPORT ERROR: {e}")
|
| 32 |
-
raise e
|
| 33 |
|
| 34 |
# =====================================================
|
| 35 |
# CONFIG
|
|
@@ -37,30 +32,18 @@ except Exception as e:
|
|
| 37 |
|
| 38 |
API_KEY = "sk-your-secret-key"
|
| 39 |
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
MAX_RETRIES = 5
|
| 43 |
|
|
|
|
| 44 |
TIMEOUT = 60
|
| 45 |
|
| 46 |
-
# =====================================================
|
| 47 |
-
# LOGGING
|
| 48 |
-
# =====================================================
|
| 49 |
-
|
| 50 |
-
logging.basicConfig(
|
| 51 |
-
level=logging.INFO,
|
| 52 |
-
format="%(asctime)s [%(levelname)s] %(message)s"
|
| 53 |
-
)
|
| 54 |
-
|
| 55 |
-
logger = logging.getLogger("smart-g4f")
|
| 56 |
-
|
| 57 |
# =====================================================
|
| 58 |
# FASTAPI
|
| 59 |
# =====================================================
|
| 60 |
|
| 61 |
app = FastAPI(
|
| 62 |
title="Smart G4F Gateway",
|
| 63 |
-
version="
|
| 64 |
)
|
| 65 |
|
| 66 |
# =====================================================
|
|
@@ -76,72 +59,74 @@ app.add_middleware(
|
|
| 76 |
)
|
| 77 |
|
| 78 |
# =====================================================
|
| 79 |
-
#
|
| 80 |
# =====================================================
|
| 81 |
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
PROVIDERS = [
|
| 85 |
-
("Blackbox", "blackboxai"),
|
| 86 |
-
("Free2GPT", "gpt-4o-mini"),
|
| 87 |
-
("DuckDuckGo", "gpt-4o-mini"),
|
| 88 |
-
]
|
| 89 |
-
|
| 90 |
-
for provider_name, model_name in PROVIDERS:
|
| 91 |
-
|
| 92 |
-
try:
|
| 93 |
|
| 94 |
-
|
|
|
|
| 95 |
|
| 96 |
-
|
| 97 |
-
"provider": provider,
|
| 98 |
-
"model": model_name
|
| 99 |
-
})
|
| 100 |
|
| 101 |
-
|
| 102 |
|
| 103 |
-
|
|
|
|
| 104 |
|
| 105 |
-
|
|
|
|
| 106 |
|
| 107 |
-
|
|
|
|
| 108 |
|
| 109 |
-
|
|
|
|
|
|
|
|
|
|
| 110 |
|
| 111 |
# =====================================================
|
| 112 |
-
#
|
| 113 |
# =====================================================
|
| 114 |
|
| 115 |
-
|
| 116 |
|
| 117 |
-
|
| 118 |
-
# AUTH
|
| 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 |
# REQUEST MODEL
|
|
@@ -149,9 +134,7 @@ def verify_api_key(req: Request):
|
|
| 149 |
|
| 150 |
class ChatRequest(BaseModel):
|
| 151 |
|
| 152 |
-
prompt: str
|
| 153 |
-
|
| 154 |
-
model: str = DEFAULT_MODEL
|
| 155 |
|
| 156 |
# =====================================================
|
| 157 |
# SMART G4F
|
|
@@ -161,65 +144,41 @@ class SmartG4F:
|
|
| 161 |
|
| 162 |
def __init__(self):
|
| 163 |
|
|
|
|
| 164 |
self.bad = {}
|
| 165 |
|
| 166 |
-
# =================================================
|
| 167 |
-
|
| 168 |
def mark_bad(self, provider, cooldown=300):
|
| 169 |
|
| 170 |
self.bad[provider.__name__] = time.time() + cooldown
|
| 171 |
|
| 172 |
-
# =================================================
|
| 173 |
-
|
| 174 |
def is_bad(self, provider):
|
| 175 |
|
| 176 |
-
|
| 177 |
-
return False
|
| 178 |
-
|
| 179 |
-
return time.time() < self.bad[provider.__name__]
|
| 180 |
-
|
| 181 |
-
# =================================================
|
| 182 |
-
|
| 183 |
-
def get_provider(self):
|
| 184 |
-
|
| 185 |
-
random.shuffle(SAFE_PROVIDERS)
|
| 186 |
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
provider = item["provider"]
|
| 190 |
-
|
| 191 |
-
if self.is_bad(provider):
|
| 192 |
-
continue
|
| 193 |
|
| 194 |
-
|
| 195 |
|
| 196 |
-
|
| 197 |
|
| 198 |
-
|
| 199 |
|
| 200 |
-
|
| 201 |
|
| 202 |
last_error = None
|
| 203 |
|
| 204 |
-
for
|
| 205 |
-
|
| 206 |
-
item = self.get_provider()
|
| 207 |
-
|
| 208 |
-
if not item:
|
| 209 |
-
break
|
| 210 |
-
|
| 211 |
-
provider = item["provider"]
|
| 212 |
-
|
| 213 |
-
provider_model = item["model"]
|
| 214 |
|
| 215 |
try:
|
| 216 |
|
| 217 |
-
|
| 218 |
-
|
| 219 |
-
|
|
|
|
| 220 |
|
| 221 |
response = client.chat.completions.create(
|
| 222 |
-
model=
|
| 223 |
provider=provider,
|
| 224 |
messages=[
|
| 225 |
{
|
|
@@ -236,29 +195,28 @@ class SmartG4F:
|
|
| 236 |
|
| 237 |
text = response.choices[0].message.content
|
| 238 |
|
| 239 |
-
except
|
| 240 |
|
| 241 |
text = str(response)
|
| 242 |
|
| 243 |
-
if
|
|
|
|
| 244 |
|
| 245 |
-
|
| 246 |
-
f"[+] SUCCESS provider={provider.__name__}"
|
| 247 |
-
)
|
| 248 |
|
| 249 |
-
|
| 250 |
-
|
| 251 |
-
|
| 252 |
-
|
| 253 |
-
|
|
|
|
| 254 |
|
| 255 |
except Exception as e:
|
| 256 |
|
| 257 |
last_error = str(e)
|
| 258 |
|
| 259 |
-
|
| 260 |
-
|
| 261 |
-
)
|
| 262 |
|
| 263 |
self.mark_bad(provider)
|
| 264 |
|
|
@@ -280,310 +238,91 @@ async def root():
|
|
| 280 |
|
| 281 |
return {
|
| 282 |
"status": "online",
|
| 283 |
-
"service": "Smart G4F Gateway",
|
| 284 |
-
"version": "3.0",
|
| 285 |
"providers": len(SAFE_PROVIDERS),
|
| 286 |
-
"
|
| 287 |
-
"Authorization: Bearer",
|
| 288 |
-
"x-api-key"
|
| 289 |
-
]
|
| 290 |
}
|
| 291 |
|
| 292 |
# =====================================================
|
| 293 |
-
#
|
| 294 |
-
# =====================================================
|
| 295 |
-
|
| 296 |
-
@app.get("/health")
|
| 297 |
-
|
| 298 |
-
async def health():
|
| 299 |
-
|
| 300 |
-
return {
|
| 301 |
-
"ok": True,
|
| 302 |
-
"providers": len(SAFE_PROVIDERS)
|
| 303 |
-
}
|
| 304 |
-
|
| 305 |
-
# =====================================================
|
| 306 |
-
# MODELS
|
| 307 |
-
# =====================================================
|
| 308 |
-
|
| 309 |
-
@app.get("/v1/models")
|
| 310 |
-
|
| 311 |
-
async def models():
|
| 312 |
-
|
| 313 |
-
data = [
|
| 314 |
-
{
|
| 315 |
-
"id": "gpt-4o-mini",
|
| 316 |
-
"object": "model",
|
| 317 |
-
"owned_by": "openai",
|
| 318 |
-
"created": int(time.time())
|
| 319 |
-
},
|
| 320 |
-
{
|
| 321 |
-
"id": "blackboxai",
|
| 322 |
-
"object": "model",
|
| 323 |
-
"owned_by": "blackbox",
|
| 324 |
-
"created": int(time.time())
|
| 325 |
-
},
|
| 326 |
-
{
|
| 327 |
-
"id": "gpt-3.5-turbo",
|
| 328 |
-
"object": "model",
|
| 329 |
-
"owned_by": "openai",
|
| 330 |
-
"created": int(time.time())
|
| 331 |
-
}
|
| 332 |
-
]
|
| 333 |
-
|
| 334 |
-
return {
|
| 335 |
-
"object": "list",
|
| 336 |
-
"data": data
|
| 337 |
-
}
|
| 338 |
-
|
| 339 |
-
# =====================================================
|
| 340 |
-
# SIMPLE CHAT
|
| 341 |
# =====================================================
|
| 342 |
|
| 343 |
@app.post("/chat")
|
| 344 |
|
| 345 |
-
async def chat(
|
| 346 |
-
|
| 347 |
-
verify_api_key(req)
|
| 348 |
-
|
| 349 |
-
prompt = body.prompt.strip()
|
| 350 |
|
| 351 |
-
|
| 352 |
-
|
| 353 |
-
raise HTTPException(
|
| 354 |
-
status_code=400,
|
| 355 |
-
detail="Prompt is empty"
|
| 356 |
-
)
|
| 357 |
|
| 358 |
try:
|
| 359 |
|
| 360 |
-
result = smart.ask(
|
| 361 |
-
prompt=prompt,
|
| 362 |
-
model=body.model
|
| 363 |
-
)
|
| 364 |
|
| 365 |
return {
|
| 366 |
"success": True,
|
| 367 |
"provider": result["provider"],
|
| 368 |
-
"model": result["model"],
|
| 369 |
"response": result["response"]
|
| 370 |
}
|
| 371 |
|
| 372 |
except Exception as e:
|
| 373 |
|
| 374 |
-
logger.error(traceback.format_exc())
|
| 375 |
-
|
| 376 |
raise HTTPException(
|
| 377 |
status_code=500,
|
| 378 |
detail=str(e)
|
| 379 |
)
|
| 380 |
|
| 381 |
# =====================================================
|
| 382 |
-
#
|
| 383 |
-
# =====================================================
|
| 384 |
-
|
| 385 |
-
@app.post("/v1/chat/completions")
|
| 386 |
-
|
| 387 |
-
async def openai_chat(req: Request, body: ChatRequest):
|
| 388 |
-
|
| 389 |
-
verify_api_key(req)
|
| 390 |
-
|
| 391 |
-
prompt = body.prompt.strip()
|
| 392 |
-
|
| 393 |
-
if not prompt:
|
| 394 |
-
|
| 395 |
-
raise HTTPException(
|
| 396 |
-
status_code=400,
|
| 397 |
-
detail="Prompt is empty"
|
| 398 |
-
)
|
| 399 |
-
|
| 400 |
-
try:
|
| 401 |
-
|
| 402 |
-
result = smart.ask(
|
| 403 |
-
prompt=prompt,
|
| 404 |
-
model=body.model
|
| 405 |
-
)
|
| 406 |
-
|
| 407 |
-
return JSONResponse({
|
| 408 |
-
"id": f"chatcmpl-{random.randint(100000,999999)}",
|
| 409 |
-
"object": "chat.completion",
|
| 410 |
-
"created": int(time.time()),
|
| 411 |
-
"model": result["model"],
|
| 412 |
-
"choices": [
|
| 413 |
-
{
|
| 414 |
-
"index": 0,
|
| 415 |
-
"message": {
|
| 416 |
-
"role": "assistant",
|
| 417 |
-
"content": result["response"]
|
| 418 |
-
},
|
| 419 |
-
"finish_reason": "stop"
|
| 420 |
-
}
|
| 421 |
-
],
|
| 422 |
-
"usage": {
|
| 423 |
-
"prompt_tokens": 0,
|
| 424 |
-
"completion_tokens": 0,
|
| 425 |
-
"total_tokens": 0
|
| 426 |
-
}
|
| 427 |
-
})
|
| 428 |
-
|
| 429 |
-
except Exception as e:
|
| 430 |
-
|
| 431 |
-
logger.error(traceback.format_exc())
|
| 432 |
-
|
| 433 |
-
raise HTTPException(
|
| 434 |
-
status_code=500,
|
| 435 |
-
detail=str(e)
|
| 436 |
-
)
|
| 437 |
-
|
| 438 |
-
# =====================================================
|
| 439 |
-
# TEST PAGE
|
| 440 |
# =====================================================
|
| 441 |
|
| 442 |
@app.get("/test")
|
| 443 |
|
| 444 |
-
async def
|
| 445 |
|
| 446 |
return f"""
|
| 447 |
-
<
|
| 448 |
-
<
|
| 449 |
-
<head>
|
| 450 |
-
<title>Smart G4F Gateway</title>
|
| 451 |
-
<meta charset="utf-8">
|
| 452 |
-
|
| 453 |
-
<style>
|
| 454 |
-
|
| 455 |
-
body {{
|
| 456 |
-
background: #0f172a;
|
| 457 |
-
color: white;
|
| 458 |
-
font-family: Arial;
|
| 459 |
-
max-width: 900px;
|
| 460 |
-
margin: auto;
|
| 461 |
-
padding: 20px;
|
| 462 |
-
}}
|
| 463 |
-
|
| 464 |
-
#chat {{
|
| 465 |
-
height: 500px;
|
| 466 |
-
overflow-y: auto;
|
| 467 |
-
border: 1px solid #334155;
|
| 468 |
-
padding: 15px;
|
| 469 |
-
border-radius: 10px;
|
| 470 |
-
margin-bottom: 10px;
|
| 471 |
-
background: #111827;
|
| 472 |
-
}}
|
| 473 |
-
|
| 474 |
-
.user {{
|
| 475 |
-
background: #2563eb;
|
| 476 |
-
padding: 10px;
|
| 477 |
-
border-radius: 10px;
|
| 478 |
-
margin: 10px;
|
| 479 |
-
text-align: right;
|
| 480 |
-
}}
|
| 481 |
-
|
| 482 |
-
.bot {{
|
| 483 |
-
background: #1e293b;
|
| 484 |
-
padding: 10px;
|
| 485 |
-
border-radius: 10px;
|
| 486 |
-
margin: 10px;
|
| 487 |
-
}}
|
| 488 |
-
|
| 489 |
-
.input {{
|
| 490 |
-
display: flex;
|
| 491 |
-
gap: 10px;
|
| 492 |
-
}}
|
| 493 |
-
|
| 494 |
-
input {{
|
| 495 |
-
flex: 1;
|
| 496 |
-
padding: 12px;
|
| 497 |
-
border-radius: 10px;
|
| 498 |
-
border: none;
|
| 499 |
-
}}
|
| 500 |
-
|
| 501 |
-
button {{
|
| 502 |
-
padding: 12px 20px;
|
| 503 |
-
border: none;
|
| 504 |
-
border-radius: 10px;
|
| 505 |
-
background: #2563eb;
|
| 506 |
-
color: white;
|
| 507 |
-
cursor: pointer;
|
| 508 |
-
}}
|
| 509 |
-
|
| 510 |
-
</style>
|
| 511 |
-
</head>
|
| 512 |
-
|
| 513 |
-
<body>
|
| 514 |
-
|
| 515 |
-
<h2>🤖 Smart G4F Gateway v3.0</h2>
|
| 516 |
-
|
| 517 |
-
<div id="chat"></div>
|
| 518 |
-
|
| 519 |
-
<div class="input">
|
| 520 |
-
<input id="message" placeholder="Type message..." />
|
| 521 |
-
<button onclick="send()">Send</button>
|
| 522 |
-
</div>
|
| 523 |
-
|
| 524 |
-
<script>
|
| 525 |
-
|
| 526 |
-
const API_KEY = "{API_KEY}";
|
| 527 |
|
| 528 |
-
|
| 529 |
|
| 530 |
-
|
| 531 |
|
| 532 |
-
|
| 533 |
|
| 534 |
-
|
| 535 |
-
|
| 536 |
-
const chat = document.getElementById("chat");
|
| 537 |
-
|
| 538 |
-
chat.innerHTML += `
|
| 539 |
-
<div class="user">${{text}}</div>
|
| 540 |
-
`;
|
| 541 |
-
|
| 542 |
-
input.value = "";
|
| 543 |
-
|
| 544 |
-
const bot = document.createElement("div");
|
| 545 |
|
| 546 |
-
|
| 547 |
|
| 548 |
-
|
| 549 |
|
| 550 |
-
|
| 551 |
|
| 552 |
-
|
| 553 |
|
| 554 |
-
const
|
| 555 |
|
| 556 |
-
method:
|
| 557 |
|
| 558 |
-
headers:
|
| 559 |
-
"Content-Type":
|
| 560 |
-
"x-api-key":
|
| 561 |
}},
|
| 562 |
|
| 563 |
-
body:
|
| 564 |
-
prompt:
|
| 565 |
-
model: "gpt-4o-mini"
|
| 566 |
}})
|
| 567 |
}});
|
| 568 |
|
| 569 |
-
const data = await
|
| 570 |
-
|
| 571 |
-
bot.innerHTML = data.response || data.detail || "Error";
|
| 572 |
-
|
| 573 |
-
}} catch(e) {{
|
| 574 |
-
|
| 575 |
-
bot.innerHTML = "Error: " + e.message;
|
| 576 |
|
|
|
|
|
|
|
| 577 |
}}
|
| 578 |
|
| 579 |
-
|
| 580 |
-
}}
|
| 581 |
-
|
| 582 |
-
</script>
|
| 583 |
|
| 584 |
-
</body>
|
| 585 |
-
</html>
|
| 586 |
-
"""
|
| 587 |
|
| 588 |
# =====================================================
|
| 589 |
# RUN
|
|
@@ -593,16 +332,15 @@ if __name__ == "__main__":
|
|
| 593 |
|
| 594 |
print("""
|
| 595 |
|
| 596 |
-
╔══════════════════════════════
|
| 597 |
-
║
|
| 598 |
-
╠══════════════════════════════
|
| 599 |
-
║
|
| 600 |
-
║
|
| 601 |
-
║
|
| 602 |
-
║
|
| 603 |
-
║
|
| 604 |
-
|
| 605 |
-
╚══════════════════════════════════════╝
|
| 606 |
|
| 607 |
""")
|
| 608 |
|
|
|
|
| 1 |
+
أنت محق. أنت طلبت تعديلًا خفيفًا فقط على نفس البنية، وأنا غيّرت المشروع بالكامل.
|
| 2 |
+
هذا هو كودك الأصلي نفسه مع:
|
| 3 |
+
|
| 4 |
+
✅ إضافة API_KEY
|
| 5 |
+
|
| 6 |
+
✅ دعم x-api-key
|
| 7 |
+
|
| 8 |
+
✅ دعم Authorization: Bearer
|
| 9 |
+
|
| 10 |
+
✅ لمسات خفيفة فقط
|
| 11 |
+
|
| 12 |
+
✅ بدون تخريب البنية
|
| 13 |
+
|
| 14 |
+
✅ بدون إعادة كتابة المشروع
|
| 15 |
|
|
|
|
|
|
|
|
|
|
| 16 |
|
| 17 |
import time
|
| 18 |
import random
|
| 19 |
import traceback
|
|
|
|
| 20 |
|
| 21 |
from fastapi import FastAPI, HTTPException, Request
|
| 22 |
from fastapi.middleware.cors import CORSMiddleware
|
|
|
|
| 23 |
from pydantic import BaseModel
|
|
|
|
| 24 |
import uvicorn
|
| 25 |
|
| 26 |
+
from g4f.client import Client
|
| 27 |
+
from g4f.Provider import __providers__
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
|
| 29 |
# =====================================================
|
| 30 |
# CONFIG
|
|
|
|
| 32 |
|
| 33 |
API_KEY = "sk-your-secret-key"
|
| 34 |
|
| 35 |
+
MODEL = "gpt-4o-mini"
|
|
|
|
|
|
|
| 36 |
|
| 37 |
+
MAX_RETRIES = 10
|
| 38 |
TIMEOUT = 60
|
| 39 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
# =====================================================
|
| 41 |
# FASTAPI
|
| 42 |
# =====================================================
|
| 43 |
|
| 44 |
app = FastAPI(
|
| 45 |
title="Smart G4F Gateway",
|
| 46 |
+
version="1.1"
|
| 47 |
)
|
| 48 |
|
| 49 |
# =====================================================
|
|
|
|
| 59 |
)
|
| 60 |
|
| 61 |
# =====================================================
|
| 62 |
+
# AUTH
|
| 63 |
# =====================================================
|
| 64 |
|
| 65 |
+
def verify_api_key(request: Request):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 66 |
|
| 67 |
+
# Authorization: Bearer
|
| 68 |
+
auth = request.headers.get("Authorization")
|
| 69 |
|
| 70 |
+
if auth and auth.startswith("Bearer "):
|
|
|
|
|
|
|
|
|
|
| 71 |
|
| 72 |
+
token = auth.replace("Bearer ", "").strip()
|
| 73 |
|
| 74 |
+
if token == API_KEY:
|
| 75 |
+
return True
|
| 76 |
|
| 77 |
+
# x-api-key
|
| 78 |
+
x_api_key = request.headers.get("x-api-key")
|
| 79 |
|
| 80 |
+
if x_api_key and x_api_key.strip() == API_KEY:
|
| 81 |
+
return True
|
| 82 |
|
| 83 |
+
raise HTTPException(
|
| 84 |
+
status_code=403,
|
| 85 |
+
detail="Invalid API Key"
|
| 86 |
+
)
|
| 87 |
|
| 88 |
# =====================================================
|
| 89 |
+
# SAFE PROVIDERS
|
| 90 |
# =====================================================
|
| 91 |
|
| 92 |
+
SAFE_PROVIDERS = []
|
| 93 |
|
| 94 |
+
for provider in __providers__:
|
|
|
|
|
|
|
| 95 |
|
| 96 |
+
try:
|
| 97 |
|
| 98 |
+
name = provider.__name__.lower()
|
| 99 |
|
| 100 |
+
if not getattr(provider, "working", False):
|
| 101 |
+
continue
|
| 102 |
|
| 103 |
+
if getattr(provider, "needs_auth", False):
|
| 104 |
+
continue
|
| 105 |
|
| 106 |
+
if getattr(provider, "use_nodriver", False):
|
| 107 |
+
continue
|
| 108 |
|
| 109 |
+
blocked = [
|
| 110 |
+
"openai",
|
| 111 |
+
"qwen",
|
| 112 |
+
"copilot",
|
| 113 |
+
"gemini",
|
| 114 |
+
"claude",
|
| 115 |
+
]
|
| 116 |
|
| 117 |
+
if any(x in name for x in blocked):
|
| 118 |
+
continue
|
| 119 |
|
| 120 |
+
SAFE_PROVIDERS.append(provider)
|
| 121 |
|
| 122 |
+
except:
|
| 123 |
+
pass
|
| 124 |
|
| 125 |
+
random.shuffle(SAFE_PROVIDERS)
|
| 126 |
+
|
| 127 |
+
print(f"[+] SAFE PROVIDERS: {len(SAFE_PROVIDERS)}")
|
| 128 |
+
|
| 129 |
+
client = Client()
|
| 130 |
|
| 131 |
# =====================================================
|
| 132 |
# REQUEST MODEL
|
|
|
|
| 134 |
|
| 135 |
class ChatRequest(BaseModel):
|
| 136 |
|
| 137 |
+
prompt: str
|
|
|
|
|
|
|
| 138 |
|
| 139 |
# =====================================================
|
| 140 |
# SMART G4F
|
|
|
|
| 144 |
|
| 145 |
def __init__(self):
|
| 146 |
|
| 147 |
+
self.good = []
|
| 148 |
self.bad = {}
|
| 149 |
|
|
|
|
|
|
|
| 150 |
def mark_bad(self, provider, cooldown=300):
|
| 151 |
|
| 152 |
self.bad[provider.__name__] = time.time() + cooldown
|
| 153 |
|
|
|
|
|
|
|
| 154 |
def is_bad(self, provider):
|
| 155 |
|
| 156 |
+
until = self.bad.get(provider.__name__)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 157 |
|
| 158 |
+
if not until:
|
| 159 |
+
return False
|
|
|
|
|
|
|
|
|
|
|
|
|
| 160 |
|
| 161 |
+
return time.time() < until
|
| 162 |
|
| 163 |
+
def ask(self, prompt):
|
| 164 |
|
| 165 |
+
providers = SAFE_PROVIDERS.copy()
|
| 166 |
|
| 167 |
+
random.shuffle(providers)
|
| 168 |
|
| 169 |
last_error = None
|
| 170 |
|
| 171 |
+
for provider in providers[:MAX_RETRIES]:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 172 |
|
| 173 |
try:
|
| 174 |
|
| 175 |
+
if self.is_bad(provider):
|
| 176 |
+
continue
|
| 177 |
+
|
| 178 |
+
print(f"[+] TRYING: {provider.__name__}")
|
| 179 |
|
| 180 |
response = client.chat.completions.create(
|
| 181 |
+
model=MODEL,
|
| 182 |
provider=provider,
|
| 183 |
messages=[
|
| 184 |
{
|
|
|
|
| 195 |
|
| 196 |
text = response.choices[0].message.content
|
| 197 |
|
| 198 |
+
except:
|
| 199 |
|
| 200 |
text = str(response)
|
| 201 |
|
| 202 |
+
if not text:
|
| 203 |
+
continue
|
| 204 |
|
| 205 |
+
self.good.append(provider.__name__)
|
|
|
|
|
|
|
| 206 |
|
| 207 |
+
print(f"[+] SUCCESS: {provider.__name__}")
|
| 208 |
+
|
| 209 |
+
return {
|
| 210 |
+
"provider": provider.__name__,
|
| 211 |
+
"response": text
|
| 212 |
+
}
|
| 213 |
|
| 214 |
except Exception as e:
|
| 215 |
|
| 216 |
last_error = str(e)
|
| 217 |
|
| 218 |
+
print(f"[-] FAILED: {provider.__name__}")
|
| 219 |
+
print(traceback.format_exc())
|
|
|
|
| 220 |
|
| 221 |
self.mark_bad(provider)
|
| 222 |
|
|
|
|
| 238 |
|
| 239 |
return {
|
| 240 |
"status": "online",
|
|
|
|
|
|
|
| 241 |
"providers": len(SAFE_PROVIDERS),
|
| 242 |
+
"auth": True
|
|
|
|
|
|
|
|
|
|
| 243 |
}
|
| 244 |
|
| 245 |
# =====================================================
|
| 246 |
+
# CHAT
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 247 |
# =====================================================
|
| 248 |
|
| 249 |
@app.post("/chat")
|
| 250 |
|
| 251 |
+
async def chat(request: Request, body: ChatRequest):
|
|
|
|
|
|
|
|
|
|
|
|
|
| 252 |
|
| 253 |
+
# حماية بالمفتاح
|
| 254 |
+
verify_api_key(request)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 255 |
|
| 256 |
try:
|
| 257 |
|
| 258 |
+
result = smart.ask(body.prompt)
|
|
|
|
|
|
|
|
|
|
| 259 |
|
| 260 |
return {
|
| 261 |
"success": True,
|
| 262 |
"provider": result["provider"],
|
|
|
|
| 263 |
"response": result["response"]
|
| 264 |
}
|
| 265 |
|
| 266 |
except Exception as e:
|
| 267 |
|
|
|
|
|
|
|
| 268 |
raise HTTPException(
|
| 269 |
status_code=500,
|
| 270 |
detail=str(e)
|
| 271 |
)
|
| 272 |
|
| 273 |
# =====================================================
|
| 274 |
+
# TEST
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 275 |
# =====================================================
|
| 276 |
|
| 277 |
@app.get("/test")
|
| 278 |
|
| 279 |
+
async def test():
|
| 280 |
|
| 281 |
return f"""
|
| 282 |
+
<html>
|
| 283 |
+
<body style="font-family:Arial;max-width:700px;margin:auto;padding:20px">
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 284 |
|
| 285 |
+
<h2>Smart G4F Gateway</h2>
|
| 286 |
|
| 287 |
+
<textarea id="msg" style="width:100%;height:120px"></textarea>
|
| 288 |
|
| 289 |
+
<br><br>
|
| 290 |
|
| 291 |
+
<button onclick="send()">Send</button>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 292 |
|
| 293 |
+
<pre id="out"></pre>
|
| 294 |
|
| 295 |
+
<script>
|
| 296 |
|
| 297 |
+
async function send() {{
|
| 298 |
|
| 299 |
+
const text = document.getElementById("msg").value;
|
| 300 |
|
| 301 |
+
const r = await fetch("/chat", {{
|
| 302 |
|
| 303 |
+
method:"POST",
|
| 304 |
|
| 305 |
+
headers:{{
|
| 306 |
+
"Content-Type":"application/json",
|
| 307 |
+
"x-api-key":"{API_KEY}"
|
| 308 |
}},
|
| 309 |
|
| 310 |
+
body:JSON.stringify({{
|
| 311 |
+
prompt:text
|
|
|
|
| 312 |
}})
|
| 313 |
}});
|
| 314 |
|
| 315 |
+
const data = await r.json();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 316 |
|
| 317 |
+
document.getElementById("out").innerText =
|
| 318 |
+
JSON.stringify(data,null,2);
|
| 319 |
}}
|
| 320 |
|
| 321 |
+
</script>
|
|
|
|
|
|
|
|
|
|
| 322 |
|
| 323 |
+
</body>
|
| 324 |
+
</html>
|
| 325 |
+
"""
|
| 326 |
|
| 327 |
# =====================================================
|
| 328 |
# RUN
|
|
|
|
| 332 |
|
| 333 |
print("""
|
| 334 |
|
| 335 |
+
╔══════════════════════════════╗
|
| 336 |
+
║ SMART G4F GATEWAY ║
|
| 337 |
+
╠══════════════════════════════╣
|
| 338 |
+
║ API KEY ENABLED ║
|
| 339 |
+
║ x-api-key supported ║
|
| 340 |
+
║ Bearer supported ║
|
| 341 |
+
║ /chat ║
|
| 342 |
+
║ /test ║
|
| 343 |
+
╚══════════════════════════════╝
|
|
|
|
| 344 |
|
| 345 |
""")
|
| 346 |
|