Abhaykoul commited on
Commit
bd69830
·
verified ·
1 Parent(s): 4bb7fcc

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +49 -49
app.py CHANGED
@@ -57,7 +57,7 @@ IMAGE_STORAGE = {}
57
 
58
  # Simple API key verification (demo purposes only)
59
  # In production, you'd want a more secure authentication system
60
- API_KEYS = {"sk-demo-key": "demo"}
61
 
62
  # Provider mapping
63
  PROVIDER_MAP = {
@@ -294,54 +294,54 @@ class APIError(Exception):
294
  self.param = param
295
  self.type = type
296
 
297
- # Authentication dependency
298
- async def verify_api_key(authorization: Optional[str] = Header(None)):
299
- if authorization is None:
300
- raise HTTPException(
301
- status_code=401,
302
- detail={
303
- "error": {
304
- "message": "No API key provided",
305
- "type": "authentication_error",
306
- "param": None,
307
- "code": "no_api_key"
308
- }
309
- }
310
- )
311
 
312
- # Extract the key from the Authorization header
313
- parts = authorization.split()
314
- if len(parts) != 2 or parts[0].lower() != "bearer":
315
- raise HTTPException(
316
- status_code=401,
317
- detail={
318
- "error": {
319
- "message": "Invalid authentication format. Use 'Bearer YOUR_API_KEY'",
320
- "type": "authentication_error",
321
- "param": None,
322
- "code": "invalid_auth_format"
323
- }
324
- }
325
- )
326
 
327
- api_key = parts[1]
328
 
329
- # Check if the API key is valid
330
- # In production, you'd want to use a more secure method
331
- if api_key not in API_KEYS:
332
- raise HTTPException(
333
- status_code=401,
334
- detail={
335
- "error": {
336
- "message": "Invalid API key",
337
- "type": "authentication_error",
338
- "param": None,
339
- "code": "invalid_api_key"
340
- }
341
- }
342
- )
343
 
344
- return api_key
345
 
346
  # Find provider from model ID - updating this function to support provider/model format
347
  def get_provider_for_model(model: str):
@@ -395,7 +395,7 @@ async def health_check():
395
  # OpenAI-compatible endpoints
396
 
397
  # List available models
398
- @app.get("/v1/models", response_model=ModelsListResponse, dependencies=[Depends(verify_api_key)])
399
  async def list_models():
400
  models_data = []
401
 
@@ -422,7 +422,7 @@ async def list_models():
422
  }
423
 
424
  # Get model information
425
- @app.get("/v1/models/{model_id}", dependencies=[Depends(verify_api_key)])
426
  async def get_model(model_id: str):
427
  try:
428
  provider_name, model = get_provider_for_model(model_id)
@@ -445,7 +445,7 @@ async def get_model(model_id: str):
445
  )
446
 
447
  # Generate images
448
- @app.post("/v1/images/generations", response_model=ImageGenerationResponse, dependencies=[Depends(verify_api_key)])
449
  async def create_image(request: ImageGenerationRequest, background_tasks: BackgroundTasks):
450
  try:
451
  # Get provider for the requested model
@@ -662,7 +662,7 @@ async def create_image(request: ImageGenerationRequest, background_tasks: Backgr
662
  )
663
 
664
  # Image retrieval endpoint
665
- @app.get("/v1/images/{task_id}/{image_id}", dependencies=[Depends(verify_api_key)])
666
  async def get_image(task_id: str, image_id: str):
667
  if task_id not in IMAGE_STORAGE:
668
  return JSONResponse(
 
57
 
58
  # Simple API key verification (demo purposes only)
59
  # In production, you'd want a more secure authentication system
60
+ # API_KEYS = {"sk-demo-key": "demo"}
61
 
62
  # Provider mapping
63
  PROVIDER_MAP = {
 
294
  self.param = param
295
  self.type = type
296
 
297
+ # # Authentication dependency
298
+ # async def verify_api_key(authorization: Optional[str] = Header(None)):
299
+ # if authorization is None:
300
+ # raise HTTPException(
301
+ # status_code=401,
302
+ # detail={
303
+ # "error": {
304
+ # "message": "No API key provided",
305
+ # "type": "authentication_error",
306
+ # "param": None,
307
+ # "code": "no_api_key"
308
+ # }
309
+ # }
310
+ # )
311
 
312
+ # # Extract the key from the Authorization header
313
+ # parts = authorization.split()
314
+ # if len(parts) != 2 or parts[0].lower() != "bearer":
315
+ # raise HTTPException(
316
+ # status_code=401,
317
+ # detail={
318
+ # "error": {
319
+ # "message": "Invalid authentication format. Use 'Bearer YOUR_API_KEY'",
320
+ # "type": "authentication_error",
321
+ # "param": None,
322
+ # "code": "invalid_auth_format"
323
+ # }
324
+ # }
325
+ # )
326
 
327
+ # api_key = parts[1]
328
 
329
+ # # Check if the API key is valid
330
+ # # In production, you'd want to use a more secure method
331
+ # if api_key not in API_KEYS:
332
+ # raise HTTPException(
333
+ # status_code=401,
334
+ # detail={
335
+ # "error": {
336
+ # "message": "Invalid API key",
337
+ # "type": "authentication_error",
338
+ # "param": None,
339
+ # "code": "invalid_api_key"
340
+ # }
341
+ # }
342
+ # )
343
 
344
+ # return api_key
345
 
346
  # Find provider from model ID - updating this function to support provider/model format
347
  def get_provider_for_model(model: str):
 
395
  # OpenAI-compatible endpoints
396
 
397
  # List available models
398
+ @app.get("/v1/models", response_model=ModelsListResponse)
399
  async def list_models():
400
  models_data = []
401
 
 
422
  }
423
 
424
  # Get model information
425
+ @app.get("/v1/models/{model_id}")
426
  async def get_model(model_id: str):
427
  try:
428
  provider_name, model = get_provider_for_model(model_id)
 
445
  )
446
 
447
  # Generate images
448
+ @app.post("/v1/images/generations", response_model=ImageGenerationResponse)
449
  async def create_image(request: ImageGenerationRequest, background_tasks: BackgroundTasks):
450
  try:
451
  # Get provider for the requested model
 
662
  )
663
 
664
  # Image retrieval endpoint
665
+ @app.get("/v1/images/{task_id}/{image_id}")
666
  async def get_image(task_id: str, image_id: str):
667
  if task_id not in IMAGE_STORAGE:
668
  return JSONResponse(