Spaces:
Running
on
T4
Running
on
T4
fix authen
Browse files- Dockerfile +1 -3
- auth.py +16 -3
- data/users.json +0 -7
- main.py +3 -14
- routes/health.py +19 -2
Dockerfile
CHANGED
@@ -17,8 +17,6 @@ ENV MPLCONFIGDIR=/tmp/matplotlib-cache
|
|
17 |
ENV TRANSFORMERS_CACHE=/tmp/huggingface-cache
|
18 |
ENV HF_HOME=/tmp/huggingface-cache
|
19 |
ENV HF_DATASETS_CACHE=/tmp/huggingface-cache
|
20 |
-
ENV PYTHONUNBUFFERED=1
|
21 |
-
ENV LOG_LEVEL=DEBUG
|
22 |
|
23 |
# Copy requirements từ project hiện tại
|
24 |
COPY requirements.txt .
|
@@ -55,4 +53,4 @@ USER appuser
|
|
55 |
|
56 |
# Chạy ứng dụng với Uvicorn
|
57 |
# Lưu ý: Hugging Face Spaces sử dụng port 7860
|
58 |
-
CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "7860"
|
|
|
17 |
ENV TRANSFORMERS_CACHE=/tmp/huggingface-cache
|
18 |
ENV HF_HOME=/tmp/huggingface-cache
|
19 |
ENV HF_DATASETS_CACHE=/tmp/huggingface-cache
|
|
|
|
|
20 |
|
21 |
# Copy requirements từ project hiện tại
|
22 |
COPY requirements.txt .
|
|
|
53 |
|
54 |
# Chạy ứng dụng với Uvicorn
|
55 |
# Lưu ý: Hugging Face Spaces sử dụng port 7860
|
56 |
+
CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "7860"]
|
auth.py
CHANGED
@@ -10,7 +10,7 @@ from typing import Annotated, Optional
|
|
10 |
from jwt.exceptions import InvalidTokenError
|
11 |
|
12 |
pwd_context = CryptContext(schemes=["bcrypt"], deprecated="auto")
|
13 |
-
oauth2_scheme = OAuth2PasswordBearer(tokenUrl="token", auto_error=True)
|
14 |
|
15 |
# Authentication helper functions
|
16 |
def verify_password(plain_password, hashed_password):
|
@@ -47,16 +47,29 @@ async def get_current_user(token: Annotated[str, Depends(oauth2_scheme)]):
|
|
47 |
headers={"WWW-Authenticate": "Bearer"},
|
48 |
)
|
49 |
try:
|
|
|
50 |
payload = jwt.decode(token, SECRET_KEY, algorithms=[ALGORITHM])
|
|
|
51 |
username = payload.get("sub")
|
52 |
if username is None:
|
|
|
53 |
raise credentials_exception
|
54 |
token_data = TokenData(username=username)
|
55 |
-
|
|
|
|
|
56 |
raise credentials_exception
|
57 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
58 |
if user is None:
|
|
|
59 |
raise credentials_exception
|
|
|
60 |
return user
|
61 |
|
62 |
async def get_current_active_user(
|
|
|
10 |
from jwt.exceptions import InvalidTokenError
|
11 |
|
12 |
pwd_context = CryptContext(schemes=["bcrypt"], deprecated="auto")
|
13 |
+
oauth2_scheme = OAuth2PasswordBearer(tokenUrl="/token", auto_error=True)
|
14 |
|
15 |
# Authentication helper functions
|
16 |
def verify_password(plain_password, hashed_password):
|
|
|
47 |
headers={"WWW-Authenticate": "Bearer"},
|
48 |
)
|
49 |
try:
|
50 |
+
print(f"Decoding token: {token[:10]}...")
|
51 |
payload = jwt.decode(token, SECRET_KEY, algorithms=[ALGORITHM])
|
52 |
+
print(f"Token payload: {payload}")
|
53 |
username = payload.get("sub")
|
54 |
if username is None:
|
55 |
+
print("Username not found in token payload")
|
56 |
raise credentials_exception
|
57 |
token_data = TokenData(username=username)
|
58 |
+
print(f"Token data: {token_data}")
|
59 |
+
except InvalidTokenError as e:
|
60 |
+
print(f"Token validation error: {str(e)}")
|
61 |
raise credentials_exception
|
62 |
+
except Exception as e:
|
63 |
+
print(f"Unexpected error during token validation: {str(e)}")
|
64 |
+
raise credentials_exception
|
65 |
+
|
66 |
+
users = get_users()
|
67 |
+
print(f"Available users: {list(users.keys())}")
|
68 |
+
user = get_user(users, username=token_data.username)
|
69 |
if user is None:
|
70 |
+
print(f"User not found: {token_data.username}")
|
71 |
raise credentials_exception
|
72 |
+
print(f"User authenticated: {user.username}")
|
73 |
return user
|
74 |
|
75 |
async def get_current_active_user(
|
data/users.json
CHANGED
@@ -6,13 +6,6 @@
|
|
6 |
"hashed_password": "$2b$12$RtcKFk7B3hKd7vYkwxdFN.eBXSiryQIRUG.OoJ07Pl9lzHNUkugMi",
|
7 |
"disabled": false
|
8 |
},
|
9 |
-
"meiseidev": {
|
10 |
-
"username": "meiseidev",
|
11 |
-
"full_name": "meiseidev",
|
12 |
-
"email": "",
|
13 |
-
"hashed_password": "$2b$12$LXIbdQ388dMiN/ej76zuHeHuuz.VHz9rJfH4FpwdVbqfwCbSI55Va",
|
14 |
-
"disabled": false
|
15 |
-
},
|
16 |
"meisaidev": {
|
17 |
"username": "meisaidev",
|
18 |
"full_name": "meisaidev",
|
|
|
6 |
"hashed_password": "$2b$12$RtcKFk7B3hKd7vYkwxdFN.eBXSiryQIRUG.OoJ07Pl9lzHNUkugMi",
|
7 |
"disabled": false
|
8 |
},
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
9 |
"meisaidev": {
|
10 |
"username": "meisaidev",
|
11 |
"full_name": "meisaidev",
|
main.py
CHANGED
@@ -4,7 +4,6 @@ from fastapi import FastAPI
|
|
4 |
import uvicorn
|
5 |
import traceback
|
6 |
from contextlib import asynccontextmanager
|
7 |
-
from fastapi.middleware.cors import CORSMiddleware
|
8 |
|
9 |
current_dir = os.path.dirname(os.path.abspath(__file__))
|
10 |
sys.path.append(current_dir)
|
@@ -35,9 +34,6 @@ app = FastAPI(
|
|
35 |
description="API for MeisaiCheck AI System",
|
36 |
version="1.0",
|
37 |
lifespan=lifespan,
|
38 |
-
openapi_url="/openapi.json",
|
39 |
-
docs_url="/docs",
|
40 |
-
redoc_url="/redoc",
|
41 |
openapi_tags=[
|
42 |
{
|
43 |
"name": "Health",
|
@@ -52,15 +48,8 @@ app = FastAPI(
|
|
52 |
"description": "AI model endpoints for prediction and embedding",
|
53 |
},
|
54 |
],
|
55 |
-
|
56 |
-
|
57 |
-
# Add CORS middleware
|
58 |
-
app.add_middleware(
|
59 |
-
CORSMiddleware,
|
60 |
-
allow_origins=["*"],
|
61 |
-
allow_credentials=True,
|
62 |
-
allow_methods=["*"],
|
63 |
-
allow_headers=["*"],
|
64 |
)
|
65 |
|
66 |
# Include Routers
|
@@ -75,4 +64,4 @@ async def root():
|
|
75 |
|
76 |
if __name__ == "__main__":
|
77 |
create_directories()
|
78 |
-
uvicorn.run(app, host="0.0.0.0", port=
|
|
|
4 |
import uvicorn
|
5 |
import traceback
|
6 |
from contextlib import asynccontextmanager
|
|
|
7 |
|
8 |
current_dir = os.path.dirname(os.path.abspath(__file__))
|
9 |
sys.path.append(current_dir)
|
|
|
34 |
description="API for MeisaiCheck AI System",
|
35 |
version="1.0",
|
36 |
lifespan=lifespan,
|
|
|
|
|
|
|
37 |
openapi_tags=[
|
38 |
{
|
39 |
"name": "Health",
|
|
|
48 |
"description": "AI model endpoints for prediction and embedding",
|
49 |
},
|
50 |
],
|
51 |
+
# Add root_path for HF Spaces deployment
|
52 |
+
root_path="/api",
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
53 |
)
|
54 |
|
55 |
# Include Routers
|
|
|
64 |
|
65 |
if __name__ == "__main__":
|
66 |
create_directories()
|
67 |
+
uvicorn.run(app, host="0.0.0.0", port=7860)
|
routes/health.py
CHANGED
@@ -1,8 +1,25 @@
|
|
1 |
-
from fastapi import APIRouter
|
|
|
|
|
2 |
import time
|
3 |
|
4 |
router = APIRouter()
|
5 |
|
6 |
@router.get("/health")
|
7 |
async def health_check():
|
8 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from fastapi import APIRouter, Depends
|
2 |
+
from auth import get_current_user
|
3 |
+
from models import User
|
4 |
import time
|
5 |
|
6 |
router = APIRouter()
|
7 |
|
8 |
@router.get("/health")
|
9 |
async def health_check():
|
10 |
+
"""
|
11 |
+
Check if the API is running
|
12 |
+
"""
|
13 |
+
return {"status": "healthy"}
|
14 |
+
|
15 |
+
|
16 |
+
@router.get("/auth-check")
|
17 |
+
async def auth_check(current_user: User = Depends(get_current_user)):
|
18 |
+
"""
|
19 |
+
Debug endpoint to verify authentication is working
|
20 |
+
"""
|
21 |
+
return {
|
22 |
+
"authenticated": True,
|
23 |
+
"username": current_user.username,
|
24 |
+
"message": "Authentication successful",
|
25 |
+
}
|