nanoppa commited on
Commit
b158aaf
·
verified ·
1 Parent(s): 80385c8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -6
app.py CHANGED
@@ -19,6 +19,10 @@ logging.basicConfig(
19
  )
20
  logger = logging.getLogger(__name__)
21
 
 
 
 
 
22
  app = FastAPI()
23
 
24
  app.add_middleware(
@@ -54,8 +58,11 @@ async def verify_authorization(authorization: str = Header(None)):
54
  raise HTTPException(
55
  status_code=401, detail="Invalid Authorization header format"
56
  )
57
- token = authorization.replace("Bearer ", "")
58
- return token
 
 
 
59
 
60
  def get_openai_models(api_keys):
61
  api_key = random.choice(api_keys)
@@ -181,8 +188,8 @@ async def convert_gemini_response_to_openai(response, model, stream=False):
181
  @app.get("/v1/models")
182
  @app.get("/hf/v1/models")
183
  async def list_models(authorization: str = Header(None)):
184
- token = await verify_authorization(authorization)
185
- api_keys = [key.strip() for key in token.split(',')]
186
 
187
  all_models = []
188
  error_messages = []
@@ -209,8 +216,8 @@ async def list_models(authorization: str = Header(None)):
209
  @app.post("/v1/chat/completions")
210
  @app.post("/hf/v1/chat/completions")
211
  async def chat_completion(request: ChatRequest, authorization: str = Header(None)):
212
- token = await verify_authorization(authorization)
213
- api_keys = [key.strip() for key in token.split(',')]
214
  logger.info(f"Chat completion request - Model: {request.model}")
215
 
216
  retries = 0
 
19
  )
20
  logger = logging.getLogger(__name__)
21
 
22
+ API_KEYS = os.getenv("API_KEYS", "")
23
+
24
+ AUTH_TOKEN = os.getenv("AUTH_TOKEN", "linux.do")
25
+
26
  app = FastAPI()
27
 
28
  app.add_middleware(
 
58
  raise HTTPException(
59
  status_code=401, detail="Invalid Authorization header format"
60
  )
61
+ if authorization != "Bearer " + AUTH_TOKEN:
62
+ logger.error("Invalid Authorization")
63
+ raise HTTPException(
64
+ status_code=403, detail="Invalid Authorization"
65
+ )
66
 
67
  def get_openai_models(api_keys):
68
  api_key = random.choice(api_keys)
 
188
  @app.get("/v1/models")
189
  @app.get("/hf/v1/models")
190
  async def list_models(authorization: str = Header(None)):
191
+ await verify_authorization(authorization)
192
+ api_keys = [key.strip() for key in API_KEYS.split(',')]
193
 
194
  all_models = []
195
  error_messages = []
 
216
  @app.post("/v1/chat/completions")
217
  @app.post("/hf/v1/chat/completions")
218
  async def chat_completion(request: ChatRequest, authorization: str = Header(None)):
219
+ await verify_authorization(authorization)
220
+ api_keys = [key.strip() for key in API_KEYS.split(',')]
221
  logger.info(f"Chat completion request - Model: {request.model}")
222
 
223
  retries = 0