huedaya commited on
Commit
883ff33
·
verified ·
1 Parent(s): 752e6ae

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +10 -8
main.py CHANGED
@@ -1,9 +1,11 @@
1
- from fastapi import FastAPI, HTTPException, Header
2
  from fastapi.middleware.cors import CORSMiddleware
3
  from pydantic import BaseModel
4
  from huggingface_hub import InferenceClient
5
  import os
6
- import requests
 
 
7
 
8
  app = FastAPI()
9
 
@@ -54,14 +56,14 @@ def generate(item: Item):
54
  return output
55
 
56
  @app.post("/generate/")
57
- async def generate_text(item: Item, authorization: str = Header(None)):
58
  # Reject if not authenticated
59
  apiKey = os.environ.get("API_KEY")
60
- if not authorization or not authorization.startswith('Bearer ' + apiKey):
61
- raise HTTPException(
62
- status_code=401,
63
- detail="Invalid or missing Bearer token"
64
- )
65
 
66
  return {
67
  "response": generate(item)
 
1
+ from fastapi import FastAPI, HTTPException
2
  from fastapi.middleware.cors import CORSMiddleware
3
  from pydantic import BaseModel
4
  from huggingface_hub import InferenceClient
5
  import os
6
+ from fastapi.security import OAuth2PasswordBearer
7
+
8
+ oauth2_scheme = OAuth2PasswordBearer(tokenUrl="token")
9
 
10
  app = FastAPI()
11
 
 
56
  return output
57
 
58
  @app.post("/generate/")
59
+ async def generate_text(item: Item, token: Annotated[str, Depends(oauth2_scheme)]):
60
  # Reject if not authenticated
61
  apiKey = os.environ.get("API_KEY")
62
+ if apiKey != token:
63
+ return {
64
+ "message": "Not authenticated",
65
+ "token": token
66
+ }
67
 
68
  return {
69
  "response": generate(item)