Update app.py
Browse files
app.py
CHANGED
|
@@ -1,9 +1,8 @@
|
|
| 1 |
import time
|
| 2 |
import random
|
| 3 |
import traceback
|
| 4 |
-
import uuid
|
| 5 |
|
| 6 |
-
from fastapi import FastAPI, Header
|
| 7 |
from pydantic import BaseModel
|
| 8 |
import uvicorn
|
| 9 |
|
|
@@ -15,7 +14,6 @@ MODEL = "gpt-4o-mini"
|
|
| 15 |
MAX_RETRIES = 10
|
| 16 |
TIMEOUT = 60
|
| 17 |
|
| 18 |
-
# ضع المفتاح هنا
|
| 19 |
OPENAI_API_KEY = "sk-your-key"
|
| 20 |
|
| 21 |
SAFE_PROVIDERS = []
|
|
@@ -97,7 +95,7 @@ class SmartG4F:
|
|
| 97 |
|
| 98 |
return providers
|
| 99 |
|
| 100 |
-
def ask(self,
|
| 101 |
|
| 102 |
errors = []
|
| 103 |
|
|
@@ -121,7 +119,12 @@ class SmartG4F:
|
|
| 121 |
response = client.chat.completions.create(
|
| 122 |
model=MODEL,
|
| 123 |
provider=provider,
|
| 124 |
-
messages=
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 125 |
timeout=TIMEOUT,
|
| 126 |
)
|
| 127 |
|
|
@@ -181,18 +184,8 @@ app = FastAPI()
|
|
| 181 |
ai = SmartG4F()
|
| 182 |
|
| 183 |
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
# =========================
|
| 187 |
-
|
| 188 |
-
class Message(BaseModel):
|
| 189 |
-
role: str
|
| 190 |
-
content: str
|
| 191 |
-
|
| 192 |
-
|
| 193 |
-
class ChatRequest(BaseModel):
|
| 194 |
-
model: str = MODEL
|
| 195 |
-
messages: list[Message]
|
| 196 |
|
| 197 |
|
| 198 |
@app.get("/")
|
|
@@ -204,67 +197,19 @@ async def home():
|
|
| 204 |
}
|
| 205 |
|
| 206 |
|
| 207 |
-
|
| 208 |
-
|
| 209 |
-
|
| 210 |
-
|
| 211 |
-
@app.post("/v1/chat/completions")
|
| 212 |
-
async def chat_completions(
|
| 213 |
-
request: ChatRequest,
|
| 214 |
authorization: str = Header(None)
|
| 215 |
):
|
| 216 |
|
| 217 |
-
|
| 218 |
-
|
| 219 |
-
|
| 220 |
-
|
| 221 |
-
raise HTTPException(
|
| 222 |
-
status_code=401,
|
| 223 |
-
detail="Missing Authorization header"
|
| 224 |
-
)
|
| 225 |
-
|
| 226 |
-
token = authorization.replace("Bearer ", "")
|
| 227 |
-
|
| 228 |
-
if token != OPENAI_API_KEY:
|
| 229 |
-
raise HTTPException(
|
| 230 |
-
status_code=401,
|
| 231 |
-
detail="Invalid API key"
|
| 232 |
-
)
|
| 233 |
-
|
| 234 |
-
result = ai.ask(
|
| 235 |
-
[m.dict() for m in request.messages]
|
| 236 |
-
)
|
| 237 |
-
|
| 238 |
-
if not result["success"]:
|
| 239 |
-
|
| 240 |
-
raise HTTPException(
|
| 241 |
-
status_code=500,
|
| 242 |
-
detail=result
|
| 243 |
-
)
|
| 244 |
-
|
| 245 |
-
response_text = result["response"]
|
| 246 |
-
|
| 247 |
-
return {
|
| 248 |
-
"id": f"chatcmpl-{uuid.uuid4().hex}",
|
| 249 |
-
"object": "chat.completion",
|
| 250 |
-
"created": int(time.time()),
|
| 251 |
-
"model": request.model,
|
| 252 |
-
"choices": [
|
| 253 |
-
{
|
| 254 |
-
"index": 0,
|
| 255 |
-
"message": {
|
| 256 |
-
"role": "assistant",
|
| 257 |
-
"content": response_text
|
| 258 |
-
},
|
| 259 |
-
"finish_reason": "stop"
|
| 260 |
-
}
|
| 261 |
-
],
|
| 262 |
-
"usage": {
|
| 263 |
-
"prompt_tokens": 0,
|
| 264 |
-
"completion_tokens": 0,
|
| 265 |
-
"total_tokens": 0
|
| 266 |
}
|
| 267 |
-
|
|
|
|
| 268 |
|
| 269 |
|
| 270 |
if __name__ == "__main__":
|
|
|
|
| 1 |
import time
|
| 2 |
import random
|
| 3 |
import traceback
|
|
|
|
| 4 |
|
| 5 |
+
from fastapi import FastAPI, Header
|
| 6 |
from pydantic import BaseModel
|
| 7 |
import uvicorn
|
| 8 |
|
|
|
|
| 14 |
MAX_RETRIES = 10
|
| 15 |
TIMEOUT = 60
|
| 16 |
|
|
|
|
| 17 |
OPENAI_API_KEY = "sk-your-key"
|
| 18 |
|
| 19 |
SAFE_PROVIDERS = []
|
|
|
|
| 95 |
|
| 96 |
return providers
|
| 97 |
|
| 98 |
+
def ask(self, prompt):
|
| 99 |
|
| 100 |
errors = []
|
| 101 |
|
|
|
|
| 119 |
response = client.chat.completions.create(
|
| 120 |
model=MODEL,
|
| 121 |
provider=provider,
|
| 122 |
+
messages=[
|
| 123 |
+
{
|
| 124 |
+
"role": "user",
|
| 125 |
+
"content": prompt
|
| 126 |
+
}
|
| 127 |
+
],
|
| 128 |
timeout=TIMEOUT,
|
| 129 |
)
|
| 130 |
|
|
|
|
| 184 |
ai = SmartG4F()
|
| 185 |
|
| 186 |
|
| 187 |
+
class Query(BaseModel):
|
| 188 |
+
prompt: str
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 189 |
|
| 190 |
|
| 191 |
@app.get("/")
|
|
|
|
| 197 |
}
|
| 198 |
|
| 199 |
|
| 200 |
+
@app.post("/ask")
|
| 201 |
+
async def ask(
|
| 202 |
+
query: Query,
|
|
|
|
|
|
|
|
|
|
|
|
|
| 203 |
authorization: str = Header(None)
|
| 204 |
):
|
| 205 |
|
| 206 |
+
if authorization != f"Bearer {OPENAI_API_KEY}":
|
| 207 |
+
return {
|
| 208 |
+
"success": False,
|
| 209 |
+
"error": "Invalid API Key"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 210 |
}
|
| 211 |
+
|
| 212 |
+
return ai.ask(query.prompt)
|
| 213 |
|
| 214 |
|
| 215 |
if __name__ == "__main__":
|