Spaces:
Runtime error
Runtime error
Update main.py
Browse files
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 |
-
#
|
58 |
apiKey = os.environ.get("API_KEY")
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
# "result": "Invalid API Key",
|
64 |
-
# }), 400
|
65 |
-
|
66 |
return {
|
67 |
"response": generate(item),
|
68 |
-
"
|
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 |
}
|