Update app.py
Browse files
app.py
CHANGED
|
@@ -10,6 +10,7 @@ import uuid
|
|
| 10 |
import logging
|
| 11 |
|
| 12 |
import g4f
|
|
|
|
| 13 |
from g4f.client import Client
|
| 14 |
|
| 15 |
# =====================================================
|
|
@@ -17,6 +18,7 @@ from g4f.client import Client
|
|
| 17 |
# =====================================================
|
| 18 |
|
| 19 |
logging.basicConfig(level=logging.INFO)
|
|
|
|
| 20 |
logger = logging.getLogger(__name__)
|
| 21 |
|
| 22 |
# =====================================================
|
|
@@ -31,7 +33,7 @@ API_KEY = "sk-your-secret-key"
|
|
| 31 |
|
| 32 |
app = FastAPI(
|
| 33 |
title="Universal AI Gateway",
|
| 34 |
-
version="
|
| 35 |
)
|
| 36 |
|
| 37 |
# =====================================================
|
|
@@ -47,20 +49,22 @@ app.add_middleware(
|
|
| 47 |
)
|
| 48 |
|
| 49 |
# =====================================================
|
| 50 |
-
#
|
| 51 |
# =====================================================
|
| 52 |
|
| 53 |
class Message(BaseModel):
|
| 54 |
role: str
|
| 55 |
content: str
|
| 56 |
|
|
|
|
| 57 |
class ChatRequest(BaseModel):
|
| 58 |
model: str
|
| 59 |
messages: List[Message]
|
| 60 |
-
stream: bool = False
|
| 61 |
temperature: Optional[float] = 0.7
|
| 62 |
max_tokens: Optional[int] = 4096
|
| 63 |
|
|
|
|
| 64 |
# =====================================================
|
| 65 |
# AUTH
|
| 66 |
# =====================================================
|
|
@@ -69,9 +73,11 @@ def verify_api_key(req: Request):
|
|
| 69 |
|
| 70 |
auth = req.headers.get("Authorization")
|
| 71 |
|
| 72 |
-
# السماح بالاختبار
|
| 73 |
if not auth:
|
| 74 |
-
|
|
|
|
|
|
|
|
|
|
| 75 |
|
| 76 |
if not auth.startswith("Bearer "):
|
| 77 |
raise HTTPException(
|
|
@@ -79,7 +85,7 @@ def verify_api_key(req: Request):
|
|
| 79 |
detail="Invalid Authorization Format"
|
| 80 |
)
|
| 81 |
|
| 82 |
-
token = auth.replace("Bearer ", "")
|
| 83 |
|
| 84 |
if token != API_KEY:
|
| 85 |
raise HTTPException(
|
|
@@ -87,138 +93,99 @@ def verify_api_key(req: Request):
|
|
| 87 |
detail="Invalid API Key"
|
| 88 |
)
|
| 89 |
|
| 90 |
-
return True
|
| 91 |
|
| 92 |
# =====================================================
|
| 93 |
-
#
|
| 94 |
# =====================================================
|
| 95 |
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
providers_map = {}
|
| 99 |
-
|
| 100 |
-
# Duck.ai
|
| 101 |
-
if hasattr(g4f.Provider, "DuckDuckGo"):
|
| 102 |
-
providers_map["duck"] = g4f.Provider.DuckDuckGo
|
| 103 |
-
providers_map["duckduckgo"] = g4f.Provider.DuckDuckGo
|
| 104 |
-
|
| 105 |
-
# Blackbox
|
| 106 |
-
if hasattr(g4f.Provider, "Blackbox"):
|
| 107 |
-
providers_map["blackbox"] = g4f.Provider.Blackbox
|
| 108 |
-
|
| 109 |
-
# Free2GPT
|
| 110 |
-
if hasattr(g4f.Provider, "Free2GPT"):
|
| 111 |
-
providers_map["free2gpt"] = g4f.Provider.Free2GPT
|
| 112 |
-
|
| 113 |
-
# Copilot
|
| 114 |
-
if hasattr(g4f.Provider, "Copilot"):
|
| 115 |
-
providers_map["copilot"] = g4f.Provider.Copilot
|
| 116 |
-
|
| 117 |
-
# Liaobots
|
| 118 |
-
if hasattr(g4f.Provider, "Liaobots"):
|
| 119 |
-
providers_map["liaobots"] = g4f.Provider.Liaobots
|
| 120 |
-
|
| 121 |
-
# Gemini
|
| 122 |
-
if hasattr(g4f.Provider, "Gemini"):
|
| 123 |
-
providers_map["gemini"] = g4f.Provider.Gemini
|
| 124 |
-
|
| 125 |
-
# DeepInfra
|
| 126 |
-
if hasattr(g4f.Provider, "DeepInfra"):
|
| 127 |
-
providers_map["deepinfra"] = g4f.Provider.DeepInfra
|
| 128 |
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
|
|
|
|
|
|
| 132 |
|
| 133 |
-
return providers_map.get(provider_name.lower())
|
| 134 |
|
| 135 |
# =====================================================
|
| 136 |
-
#
|
| 137 |
# =====================================================
|
| 138 |
|
| 139 |
-
|
|
|
|
| 140 |
|
| 141 |
-
|
| 142 |
|
| 143 |
-
|
| 144 |
-
if "gpt" in model:
|
| 145 |
-
return "duck"
|
| 146 |
|
| 147 |
-
|
| 148 |
-
if "claude" in model:
|
| 149 |
-
return "liaobots"
|
| 150 |
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
return "gemini"
|
| 154 |
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
return "deepinfra"
|
| 158 |
|
| 159 |
-
|
| 160 |
-
if "llama" in model:
|
| 161 |
-
return "deepinfra"
|
| 162 |
|
| 163 |
-
|
| 164 |
-
if "mixtral" in model:
|
| 165 |
-
return "deepinfra"
|
| 166 |
|
| 167 |
-
|
| 168 |
-
return "duck"
|
| 169 |
|
| 170 |
-
|
| 171 |
-
# ROOT
|
| 172 |
-
# =====================================================
|
| 173 |
|
| 174 |
-
|
| 175 |
-
async def root():
|
| 176 |
-
|
| 177 |
-
return {
|
| 178 |
-
"status": "online",
|
| 179 |
-
"service": "Universal AI Gateway",
|
| 180 |
-
"version": "5.0.0",
|
| 181 |
-
"g4f": True,
|
| 182 |
-
"duck_ai": True
|
| 183 |
-
}
|
| 184 |
-
|
| 185 |
-
# =====================================================
|
| 186 |
-
# MODELS
|
| 187 |
-
# =====================================================
|
| 188 |
-
|
| 189 |
-
@app.get("/v1/models")
|
| 190 |
-
async def get_models():
|
| 191 |
|
| 192 |
-
|
| 193 |
-
|
| 194 |
-
|
| 195 |
-
|
| 196 |
-
|
| 197 |
-
|
| 198 |
-
|
| 199 |
-
|
| 200 |
-
|
| 201 |
-
|
| 202 |
-
|
| 203 |
-
|
| 204 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 205 |
|
| 206 |
-
|
| 207 |
|
| 208 |
-
|
| 209 |
|
| 210 |
-
models_data
|
| 211 |
-
|
| 212 |
-
|
| 213 |
-
|
| 214 |
-
|
| 215 |
-
|
|
|
|
|
|
|
| 216 |
|
| 217 |
return {
|
| 218 |
"object": "list",
|
| 219 |
"data": models_data
|
| 220 |
}
|
| 221 |
|
|
|
|
| 222 |
# =====================================================
|
| 223 |
# CHAT COMPLETIONS
|
| 224 |
# =====================================================
|
|
@@ -239,16 +206,10 @@ async def chat_completions(
|
|
| 239 |
for m in body.messages
|
| 240 |
]
|
| 241 |
|
| 242 |
-
|
| 243 |
-
|
| 244 |
-
provider = get_provider(provider_name)
|
| 245 |
-
|
| 246 |
-
logger.info(
|
| 247 |
-
f"MODEL={body.model} | PROVIDER={provider_name}"
|
| 248 |
-
)
|
| 249 |
|
| 250 |
# =================================================
|
| 251 |
-
# STREAM
|
| 252 |
# =================================================
|
| 253 |
|
| 254 |
if body.stream:
|
|
@@ -261,7 +222,6 @@ async def chat_completions(
|
|
| 261 |
|
| 262 |
response = client.chat.completions.create(
|
| 263 |
model=body.model,
|
| 264 |
-
provider=provider,
|
| 265 |
messages=messages,
|
| 266 |
stream=True
|
| 267 |
)
|
|
@@ -303,9 +263,9 @@ async def chat_completions(
|
|
| 303 |
|
| 304 |
await asyncio.sleep(0)
|
| 305 |
|
| 306 |
-
except Exception as
|
| 307 |
|
| 308 |
-
logger.error(f"Chunk
|
| 309 |
|
| 310 |
final_payload = {
|
| 311 |
"id": chunk_id,
|
|
@@ -327,7 +287,7 @@ async def chat_completions(
|
|
| 327 |
|
| 328 |
except Exception as e:
|
| 329 |
|
| 330 |
-
logger.error(f"Streaming
|
| 331 |
|
| 332 |
error_payload = {
|
| 333 |
"error": {
|
|
@@ -349,7 +309,7 @@ async def chat_completions(
|
|
| 349 |
)
|
| 350 |
|
| 351 |
# =================================================
|
| 352 |
-
# NORMAL
|
| 353 |
# =================================================
|
| 354 |
|
| 355 |
try:
|
|
@@ -359,7 +319,6 @@ async def chat_completions(
|
|
| 359 |
response = await asyncio.to_thread(
|
| 360 |
client.chat.completions.create,
|
| 361 |
model=body.model,
|
| 362 |
-
provider=provider,
|
| 363 |
messages=messages
|
| 364 |
)
|
| 365 |
|
|
@@ -394,25 +353,26 @@ async def chat_completions(
|
|
| 394 |
|
| 395 |
except Exception as e:
|
| 396 |
|
| 397 |
-
logger.error(f"Chat
|
| 398 |
|
| 399 |
raise HTTPException(
|
| 400 |
status_code=500,
|
| 401 |
detail=str(e)
|
| 402 |
)
|
| 403 |
|
|
|
|
| 404 |
# =====================================================
|
| 405 |
-
#
|
| 406 |
# =====================================================
|
| 407 |
|
| 408 |
-
@app.get("/
|
| 409 |
-
async def
|
| 410 |
|
| 411 |
return {
|
| 412 |
-
"
|
| 413 |
-
"time": int(time.time())
|
| 414 |
}
|
| 415 |
|
|
|
|
| 416 |
# =====================================================
|
| 417 |
# RUN
|
| 418 |
# =====================================================
|
|
@@ -421,6 +381,8 @@ if __name__ == "__main__":
|
|
| 421 |
|
| 422 |
import uvicorn
|
| 423 |
|
|
|
|
|
|
|
| 424 |
uvicorn.run(
|
| 425 |
app,
|
| 426 |
host="0.0.0.0",
|
|
|
|
| 10 |
import logging
|
| 11 |
|
| 12 |
import g4f
|
| 13 |
+
import g4f.models
|
| 14 |
from g4f.client import Client
|
| 15 |
|
| 16 |
# =====================================================
|
|
|
|
| 18 |
# =====================================================
|
| 19 |
|
| 20 |
logging.basicConfig(level=logging.INFO)
|
| 21 |
+
|
| 22 |
logger = logging.getLogger(__name__)
|
| 23 |
|
| 24 |
# =====================================================
|
|
|
|
| 33 |
|
| 34 |
app = FastAPI(
|
| 35 |
title="Universal AI Gateway",
|
| 36 |
+
version="4.0.0"
|
| 37 |
)
|
| 38 |
|
| 39 |
# =====================================================
|
|
|
|
| 49 |
)
|
| 50 |
|
| 51 |
# =====================================================
|
| 52 |
+
# MODELS
|
| 53 |
# =====================================================
|
| 54 |
|
| 55 |
class Message(BaseModel):
|
| 56 |
role: str
|
| 57 |
content: str
|
| 58 |
|
| 59 |
+
|
| 60 |
class ChatRequest(BaseModel):
|
| 61 |
model: str
|
| 62 |
messages: List[Message]
|
| 63 |
+
stream: Optional[bool] = False
|
| 64 |
temperature: Optional[float] = 0.7
|
| 65 |
max_tokens: Optional[int] = 4096
|
| 66 |
|
| 67 |
+
|
| 68 |
# =====================================================
|
| 69 |
# AUTH
|
| 70 |
# =====================================================
|
|
|
|
| 73 |
|
| 74 |
auth = req.headers.get("Authorization")
|
| 75 |
|
|
|
|
| 76 |
if not auth:
|
| 77 |
+
raise HTTPException(
|
| 78 |
+
status_code=401,
|
| 79 |
+
detail="Missing Authorization Header"
|
| 80 |
+
)
|
| 81 |
|
| 82 |
if not auth.startswith("Bearer "):
|
| 83 |
raise HTTPException(
|
|
|
|
| 85 |
detail="Invalid Authorization Format"
|
| 86 |
)
|
| 87 |
|
| 88 |
+
token = auth.replace("Bearer ", "")
|
| 89 |
|
| 90 |
if token != API_KEY:
|
| 91 |
raise HTTPException(
|
|
|
|
| 93 |
detail="Invalid API Key"
|
| 94 |
)
|
| 95 |
|
|
|
|
| 96 |
|
| 97 |
# =====================================================
|
| 98 |
+
# ROOT
|
| 99 |
# =====================================================
|
| 100 |
|
| 101 |
+
@app.get("/")
|
| 102 |
+
async def root():
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 103 |
|
| 104 |
+
return {
|
| 105 |
+
"status": "online",
|
| 106 |
+
"service": "Universal AI Gateway",
|
| 107 |
+
"version": "4.0.0"
|
| 108 |
+
}
|
| 109 |
|
|
|
|
| 110 |
|
| 111 |
# =====================================================
|
| 112 |
+
# AUTO MODELS IMPORT
|
| 113 |
# =====================================================
|
| 114 |
|
| 115 |
+
@app.get("/v1/models")
|
| 116 |
+
async def get_models():
|
| 117 |
|
| 118 |
+
models_data = []
|
| 119 |
|
| 120 |
+
try:
|
|
|
|
|
|
|
| 121 |
|
| 122 |
+
all_models = []
|
|
|
|
|
|
|
| 123 |
|
| 124 |
+
if hasattr(g4f.models, "_all_models"):
|
| 125 |
+
all_models = list(g4f.models._all_models)
|
|
|
|
| 126 |
|
| 127 |
+
elif hasattr(g4f.models, "__models__"):
|
| 128 |
+
all_models = list(g4f.models.__models__)
|
|
|
|
| 129 |
|
| 130 |
+
unique_models = set()
|
|
|
|
|
|
|
| 131 |
|
| 132 |
+
for model in all_models:
|
|
|
|
|
|
|
| 133 |
|
| 134 |
+
model_name = str(model)
|
|
|
|
| 135 |
|
| 136 |
+
if model_name not in unique_models:
|
|
|
|
|
|
|
| 137 |
|
| 138 |
+
unique_models.add(model_name)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 139 |
|
| 140 |
+
models_data.append({
|
| 141 |
+
"id": model_name,
|
| 142 |
+
"object": "model",
|
| 143 |
+
"created": int(time.time()),
|
| 144 |
+
"owned_by": "g4f"
|
| 145 |
+
})
|
| 146 |
+
|
| 147 |
+
if not models_data:
|
| 148 |
+
|
| 149 |
+
fallback_models = [
|
| 150 |
+
"gpt-4o-mini",
|
| 151 |
+
"gpt-4o",
|
| 152 |
+
"gpt-4",
|
| 153 |
+
"gpt-3.5-turbo",
|
| 154 |
+
"claude-3-haiku",
|
| 155 |
+
"claude-3-opus",
|
| 156 |
+
"gemini-pro",
|
| 157 |
+
"llama-3.1-70b",
|
| 158 |
+
"mixtral-8x7b"
|
| 159 |
+
]
|
| 160 |
+
|
| 161 |
+
for model_name in fallback_models:
|
| 162 |
+
|
| 163 |
+
models_data.append({
|
| 164 |
+
"id": model_name,
|
| 165 |
+
"object": "model",
|
| 166 |
+
"created": int(time.time()),
|
| 167 |
+
"owned_by": "g4f"
|
| 168 |
+
})
|
| 169 |
|
| 170 |
+
except Exception as e:
|
| 171 |
|
| 172 |
+
logger.error(f"Models error: {e}")
|
| 173 |
|
| 174 |
+
models_data = [
|
| 175 |
+
{
|
| 176 |
+
"id": "gpt-4o-mini",
|
| 177 |
+
"object": "model",
|
| 178 |
+
"created": int(time.time()),
|
| 179 |
+
"owned_by": "g4f"
|
| 180 |
+
}
|
| 181 |
+
]
|
| 182 |
|
| 183 |
return {
|
| 184 |
"object": "list",
|
| 185 |
"data": models_data
|
| 186 |
}
|
| 187 |
|
| 188 |
+
|
| 189 |
# =====================================================
|
| 190 |
# CHAT COMPLETIONS
|
| 191 |
# =====================================================
|
|
|
|
| 206 |
for m in body.messages
|
| 207 |
]
|
| 208 |
|
| 209 |
+
logger.info(f"Request model: {body.model}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 210 |
|
| 211 |
# =================================================
|
| 212 |
+
# STREAM MODE
|
| 213 |
# =================================================
|
| 214 |
|
| 215 |
if body.stream:
|
|
|
|
| 222 |
|
| 223 |
response = client.chat.completions.create(
|
| 224 |
model=body.model,
|
|
|
|
| 225 |
messages=messages,
|
| 226 |
stream=True
|
| 227 |
)
|
|
|
|
| 263 |
|
| 264 |
await asyncio.sleep(0)
|
| 265 |
|
| 266 |
+
except Exception as chunk_error:
|
| 267 |
|
| 268 |
+
logger.error(f"Chunk error: {chunk_error}")
|
| 269 |
|
| 270 |
final_payload = {
|
| 271 |
"id": chunk_id,
|
|
|
|
| 287 |
|
| 288 |
except Exception as e:
|
| 289 |
|
| 290 |
+
logger.error(f"Streaming error: {e}")
|
| 291 |
|
| 292 |
error_payload = {
|
| 293 |
"error": {
|
|
|
|
| 309 |
)
|
| 310 |
|
| 311 |
# =================================================
|
| 312 |
+
# NORMAL RESPONSE
|
| 313 |
# =================================================
|
| 314 |
|
| 315 |
try:
|
|
|
|
| 319 |
response = await asyncio.to_thread(
|
| 320 |
client.chat.completions.create,
|
| 321 |
model=body.model,
|
|
|
|
| 322 |
messages=messages
|
| 323 |
)
|
| 324 |
|
|
|
|
| 353 |
|
| 354 |
except Exception as e:
|
| 355 |
|
| 356 |
+
logger.error(f"Chat error: {e}")
|
| 357 |
|
| 358 |
raise HTTPException(
|
| 359 |
status_code=500,
|
| 360 |
detail=str(e)
|
| 361 |
)
|
| 362 |
|
| 363 |
+
|
| 364 |
# =====================================================
|
| 365 |
+
# TEST UI
|
| 366 |
# =====================================================
|
| 367 |
|
| 368 |
+
@app.get("/test")
|
| 369 |
+
async def test():
|
| 370 |
|
| 371 |
return {
|
| 372 |
+
"message": "Server is working"
|
|
|
|
| 373 |
}
|
| 374 |
|
| 375 |
+
|
| 376 |
# =====================================================
|
| 377 |
# RUN
|
| 378 |
# =====================================================
|
|
|
|
| 381 |
|
| 382 |
import uvicorn
|
| 383 |
|
| 384 |
+
print("Server running on port 7860")
|
| 385 |
+
|
| 386 |
uvicorn.run(
|
| 387 |
app,
|
| 388 |
host="0.0.0.0",
|