huedaya commited on
Commit
f445a1c
·
verified ·
1 Parent(s): 887a39c

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +7 -9
main.py CHANGED
@@ -3,6 +3,7 @@ from fastapi.middleware.cors import CORSMiddleware
3
  from pydantic import BaseModel
4
  from huggingface_hub import InferenceClient
5
  import os
 
6
 
7
  app = FastAPI()
8
 
@@ -54,16 +55,13 @@ def generate(item: Item):
54
 
55
  @app.post("/generate/")
56
  async def generate_text(item: Item):
57
- # reject if key not the same
58
  apiKey = os.environ.get("API_KEY")
59
- # if apiKey != key:
60
- # return jsonify({
61
- # "image_url": image_url,
62
- # "model": model,
63
- # "result": "Invalid API Key",
64
- # }), 400
65
-
66
  return {
67
  "response": generate(item),
68
- "key": apiKey
69
  }
 
3
  from pydantic import BaseModel
4
  from huggingface_hub import InferenceClient
5
  import os
6
+ import requests
7
 
8
  app = FastAPI()
9
 
 
55
 
56
  @app.post("/generate/")
57
  async def generate_text(item: Item):
58
+ # Reject if not authenticated
59
  apiKey = os.environ.get("API_KEY")
60
+ auth_header = request.headers['Authorization']
61
+ if not auth_header.startswith('Bearer ' + apiKey):
62
+ return jsonify({'message': 'Invalid or missing Bearer token'}), 401
63
+
 
 
 
64
  return {
65
  "response": generate(item),
66
+ "token": auth_header
67
  }