bahi-bh commited on
Commit
2bc930e
·
verified ·
1 Parent(s): 15b14be

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +22 -16
app.py CHANGED
@@ -83,19 +83,24 @@ def verify_api_key(req: Request):
83
 
84
  token = None
85
 
86
- if auth:
 
 
87
 
88
- if not auth.startswith("Bearer "):
89
- raise HTTPException(
90
- status_code=401,
91
- detail="Invalid Authorization Format"
92
- )
93
 
94
- token = auth.replace("Bearer ", "").strip()
95
 
96
- elif x_api_key:
 
97
 
98
- token = x_api_key.strip()
 
 
 
 
 
99
 
100
  if token != API_KEY:
101
  raise HTTPException(
@@ -473,14 +478,12 @@ async def anthropic_messages(
473
  if (
474
  hasattr(chunk, "choices")
475
  and chunk.choices
 
 
476
  and chunk.choices[0].delta
477
- and chunk.choices[0]
478
- .delta.content
479
  ):
480
- content = (
481
- chunk.choices[0]
482
- .delta.content
483
- )
484
 
485
  if not content:
486
  continue
@@ -495,7 +498,10 @@ async def anthropic_messages(
495
 
496
  yield (
497
  "data: "
498
- + json.dumps(payload)
 
 
 
499
  + "\n\n"
500
  )
501
 
 
83
 
84
  token = None
85
 
86
+ # دعم x-api-key
87
+ if x_api_key:
88
+ token = x_api_key.strip()
89
 
90
+ # دعم Authorization Bearer
91
+ elif auth:
 
 
 
92
 
93
+ parts = auth.split(" ", 1)
94
 
95
+ if len(parts) == 2:
96
+ token = parts[1].strip()
97
 
98
+ # لو لم يتم استخراج توكن صالح
99
+ if not token:
100
+ raise HTTPException(
101
+ status_code=401,
102
+ detail="Invalid API Key"
103
+ )
104
 
105
  if token != API_KEY:
106
  raise HTTPException(
 
478
  if (
479
  hasattr(chunk, "choices")
480
  and chunk.choices
481
+ and len(chunk.choices) > 0
482
+ and hasattr(chunk.choices[0], "delta")
483
  and chunk.choices[0].delta
484
+ and chunk.choices[0].delta.content
 
485
  ):
486
+ content = chunk.choices[0].delta.content
 
 
 
487
 
488
  if not content:
489
  continue
 
498
 
499
  yield (
500
  "data: "
501
+ + json.dumps(
502
+ payload,
503
+ ensure_ascii=False
504
+ )
505
  + "\n\n"
506
  )
507