bahi-bh commited on
Commit
7e1c722
Β·
verified Β·
1 Parent(s): afec996

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +51 -110
app.py CHANGED
@@ -1,6 +1,3 @@
1
-
2
-
3
-
4
  import time
5
  import random
6
  import traceback
@@ -24,60 +21,12 @@ MODEL = "gpt-4o-mini"
24
  MAX_RETRIES = 10
25
  TIMEOUT = 60
26
 
27
- # =====================================================
28
- # FASTAPI
29
- # =====================================================
30
-
31
- app = FastAPI(
32
- title="Smart G4F Gateway",
33
- version="1.1"
34
- )
35
-
36
- # =====================================================
37
- # CORS
38
- # =====================================================
39
-
40
- app.add_middleware(
41
- CORSMiddleware,
42
- allow_origins=["*"],
43
- allow_credentials=True,
44
- allow_methods=["*"],
45
- allow_headers=["*"],
46
- )
47
-
48
- # =====================================================
49
- # AUTH
50
- # =====================================================
51
-
52
- def verify_api_key(request: Request):
53
-
54
- # Authorization: Bearer
55
- auth = request.headers.get("Authorization")
56
-
57
- if auth and auth.startswith("Bearer "):
58
-
59
- token = auth.replace("Bearer ", "").strip()
60
-
61
- if token == API_KEY:
62
- return True
63
-
64
- # x-api-key
65
- x_api_key = request.headers.get("x-api-key")
66
-
67
- if x_api_key and x_api_key.strip() == API_KEY:
68
- return True
69
-
70
- raise HTTPException(
71
- status_code=403,
72
- detail="Invalid API Key"
73
- )
74
 
75
  # =====================================================
76
- # SAFE PROVIDERS
77
  # =====================================================
78
 
79
- SAFE_PROVIDERS = []
80
-
81
  for provider in __providers__:
82
 
83
  try:
@@ -115,6 +64,27 @@ print(f"[+] SAFE PROVIDERS: {len(SAFE_PROVIDERS)}")
115
 
116
  client = Client()
117
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
118
  # =====================================================
119
  # REQUEST MODEL
120
  # =====================================================
@@ -123,6 +93,33 @@ class ChatRequest(BaseModel):
123
 
124
  prompt: str
125
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
126
  # =====================================================
127
  # SMART G4F
128
  # =====================================================
@@ -226,7 +223,7 @@ async def root():
226
  return {
227
  "status": "online",
228
  "providers": len(SAFE_PROVIDERS),
229
- "auth": True
230
  }
231
 
232
  # =====================================================
@@ -237,7 +234,6 @@ async def root():
237
 
238
  async def chat(request: Request, body: ChatRequest):
239
 
240
- # Ψ­Ω…Ψ§ΩŠΨ© بالمفΨͺΨ§Ψ­
241
  verify_api_key(request)
242
 
243
  try:
@@ -257,60 +253,6 @@ async def chat(request: Request, body: ChatRequest):
257
  detail=str(e)
258
  )
259
 
260
- # =====================================================
261
- # TEST
262
- # =====================================================
263
-
264
- @app.get("/test")
265
-
266
- async def test():
267
-
268
- return f"""
269
- <html>
270
- <body style="font-family:Arial;max-width:700px;margin:auto;padding:20px">
271
-
272
- <h2>Smart G4F Gateway</h2>
273
-
274
- <textarea id="msg" style="width:100%;height:120px"></textarea>
275
-
276
- <br><br>
277
-
278
- <button onclick="send()">Send</button>
279
-
280
- <pre id="out"></pre>
281
-
282
- <script>
283
-
284
- async function send() {{
285
-
286
- const text = document.getElementById("msg").value;
287
-
288
- const r = await fetch("/chat", {{
289
-
290
- method:"POST",
291
-
292
- headers:{{
293
- "Content-Type":"application/json",
294
- "x-api-key":"{API_KEY}"
295
- }},
296
-
297
- body:JSON.stringify({{
298
- prompt:text
299
- }})
300
- }});
301
-
302
- const data = await r.json();
303
-
304
- document.getElementById("out").innerText =
305
- JSON.stringify(data,null,2);
306
- }}
307
-
308
- </script>
309
-
310
- </body>
311
- </html>
312
- """
313
-
314
  # =====================================================
315
  # RUN
316
  # =====================================================
@@ -326,7 +268,6 @@ if __name__ == "__main__":
326
  β•‘ x-api-key supported β•‘
327
  β•‘ Bearer supported β•‘
328
  β•‘ /chat β•‘
329
- β•‘ /test β•‘
330
  β•šβ•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•
331
 
332
  """)
 
 
 
 
1
  import time
2
  import random
3
  import traceback
 
21
  MAX_RETRIES = 10
22
  TIMEOUT = 60
23
 
24
+ SAFE_PROVIDERS = []
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
25
 
26
  # =====================================================
27
+ # FILTER PROVIDERS
28
  # =====================================================
29
 
 
 
30
  for provider in __providers__:
31
 
32
  try:
 
64
 
65
  client = Client()
66
 
67
+ # =====================================================
68
+ # FASTAPI
69
+ # =====================================================
70
+
71
+ app = FastAPI(
72
+ title="Smart G4F Gateway",
73
+ version="1.0"
74
+ )
75
+
76
+ # =====================================================
77
+ # CORS
78
+ # =====================================================
79
+
80
+ app.add_middleware(
81
+ CORSMiddleware,
82
+ allow_origins=["*"],
83
+ allow_credentials=True,
84
+ allow_methods=["*"],
85
+ allow_headers=["*"],
86
+ )
87
+
88
  # =====================================================
89
  # REQUEST MODEL
90
  # =====================================================
 
93
 
94
  prompt: str
95
 
96
+ # =====================================================
97
+ # API KEY
98
+ # =====================================================
99
+
100
+ def verify_api_key(request: Request):
101
+
102
+ # x-api-key
103
+ x_api_key = request.headers.get("x-api-key")
104
+
105
+ if x_api_key == API_KEY:
106
+ return
107
+
108
+ # Authorization Bearer
109
+ auth = request.headers.get("Authorization")
110
+
111
+ if auth and auth.startswith("Bearer "):
112
+
113
+ token = auth.replace("Bearer ", "").strip()
114
+
115
+ if token == API_KEY:
116
+ return
117
+
118
+ raise HTTPException(
119
+ status_code=403,
120
+ detail="Invalid API Key"
121
+ )
122
+
123
  # =====================================================
124
  # SMART G4F
125
  # =====================================================
 
223
  return {
224
  "status": "online",
225
  "providers": len(SAFE_PROVIDERS),
226
+ "api_key": True
227
  }
228
 
229
  # =====================================================
 
234
 
235
  async def chat(request: Request, body: ChatRequest):
236
 
 
237
  verify_api_key(request)
238
 
239
  try:
 
253
  detail=str(e)
254
  )
255
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
256
  # =====================================================
257
  # RUN
258
  # =====================================================
 
268
  β•‘ x-api-key supported β•‘
269
  β•‘ Bearer supported β•‘
270
  β•‘ /chat β•‘
 
271
  β•šβ•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•
272
 
273
  """)