Spaces:
Paused
Paused
Update backend_api.py
Browse files- backend_api.py +49 -73
backend_api.py
CHANGED
|
@@ -535,78 +535,44 @@ async def get_session(session: str):
|
|
| 535 |
}
|
| 536 |
|
| 537 |
|
| 538 |
-
@app.get("/api/auth/status")
|
| 539 |
async def auth_status(authorization: Optional[str] = Header(None)):
|
| 540 |
-
"""
|
| 541 |
-
|
| 542 |
-
|
| 543 |
-
|
| 544 |
-
|
| 545 |
-
|
| 546 |
-
|
| 547 |
-
|
| 548 |
-
|
| 549 |
-
|
| 550 |
-
#
|
| 551 |
-
if
|
| 552 |
-
|
| 553 |
-
|
| 554 |
-
|
| 555 |
-
|
| 556 |
-
|
| 557 |
-
|
| 558 |
-
|
| 559 |
-
|
| 560 |
-
|
| 561 |
-
session_data = user_sessions[token]
|
| 562 |
-
|
| 563 |
-
# Check if session has expired
|
| 564 |
-
if is_session_expired(session_data):
|
| 565 |
-
# Clean up expired session
|
| 566 |
-
user_sessions.pop(token, None)
|
| 567 |
-
return AuthStatus(
|
| 568 |
-
authenticated=False,
|
| 569 |
-
username=None,
|
| 570 |
-
message="Session expired"
|
| 571 |
-
)
|
| 572 |
|
| 573 |
-
|
| 574 |
-
if not await validate_token_with_hf(session_data["access_token"]):
|
| 575 |
-
# Token is invalid, clean up session
|
| 576 |
-
user_sessions.pop(token, None)
|
| 577 |
-
return AuthStatus(
|
| 578 |
-
authenticated=False,
|
| 579 |
-
username=None,
|
| 580 |
-
message="Authentication expired"
|
| 581 |
-
)
|
| 582 |
|
| 583 |
return AuthStatus(
|
| 584 |
authenticated=True,
|
| 585 |
-
username=
|
| 586 |
-
message=f"Authenticated as {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 587 |
)
|
| 588 |
-
|
| 589 |
-
# For direct OAuth tokens, validate with HF
|
| 590 |
-
if auth.token:
|
| 591 |
-
is_valid = await validate_token_with_hf(auth.token)
|
| 592 |
-
if is_valid:
|
| 593 |
-
return AuthStatus(
|
| 594 |
-
authenticated=True,
|
| 595 |
-
username=auth.username,
|
| 596 |
-
message=f"Authenticated as {auth.username}"
|
| 597 |
-
)
|
| 598 |
-
else:
|
| 599 |
-
return AuthStatus(
|
| 600 |
-
authenticated=False,
|
| 601 |
-
username=None,
|
| 602 |
-
message="Token expired or invalid"
|
| 603 |
-
)
|
| 604 |
-
|
| 605 |
-
return AuthStatus(
|
| 606 |
-
authenticated=False,
|
| 607 |
-
username=None,
|
| 608 |
-
message="Not authenticated"
|
| 609 |
-
)
|
| 610 |
|
| 611 |
|
| 612 |
def cleanup_generated_code(code: str, language: str) -> str:
|
|
@@ -879,17 +845,27 @@ async def generate_code(
|
|
| 879 |
{"role": "user", "content": user_content}
|
| 880 |
]
|
| 881 |
|
| 882 |
-
# Stream the response
|
| 883 |
try:
|
| 884 |
-
#
|
| 885 |
-
|
| 886 |
-
|
| 887 |
-
messages=messages,
|
| 888 |
-
temperature=0.7,
|
| 889 |
-
max_tokens=10000,
|
| 890 |
stream=True
|
| 891 |
)
|
| 892 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 893 |
chunk_count = 0
|
| 894 |
|
| 895 |
# Only process stream if it exists
|
|
|
|
| 535 |
}
|
| 536 |
|
| 537 |
|
| 538 |
+
@app.get("/api/auth/status", response_model=AuthStatus)
|
| 539 |
async def auth_status(authorization: Optional[str] = Header(None)):
|
| 540 |
+
"""
|
| 541 |
+
Kiểm tra trạng thái xác thực bằng cách xác thực token trực tiếp với HuggingFace API.
|
| 542 |
+
Không phụ thuộc vào biến nhớ tạm (user_sessions) để tránh lỗi mất phiên.
|
| 543 |
+
"""
|
| 544 |
+
if not authorization:
|
| 545 |
+
return AuthStatus(authenticated=False, username=None, message="No token provided")
|
| 546 |
+
|
| 547 |
+
# Loại bỏ tiền tố 'Bearer ' nếu có
|
| 548 |
+
token = authorization.replace("Bearer ", "").strip()
|
| 549 |
+
|
| 550 |
+
# 1. Chế độ Dev (bỏ qua xác thực thực tế)
|
| 551 |
+
if token.startswith("dev_token_"):
|
| 552 |
+
username = token.split("_")[2] if len(token.split("_")) > 2 else "user"
|
| 553 |
+
return AuthStatus(authenticated=True, username=username, message=f"Auth as {username} (dev)")
|
| 554 |
+
|
| 555 |
+
# 2. Xác thực thật với HuggingFace Hub (không qua user_sessions)
|
| 556 |
+
try:
|
| 557 |
+
from huggingface_hub import HfApi
|
| 558 |
+
# Gọi trực tiếp API của HuggingFace để kiểm tra token
|
| 559 |
+
api = HfApi(token=token)
|
| 560 |
+
user_info = api.whoami()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 561 |
|
| 562 |
+
username = user_info.get("name") or user_info.get("preferred_username") or "user"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 563 |
|
| 564 |
return AuthStatus(
|
| 565 |
authenticated=True,
|
| 566 |
+
username=username,
|
| 567 |
+
message=f"Authenticated as {username}"
|
| 568 |
+
)
|
| 569 |
+
except Exception as e:
|
| 570 |
+
print(f"[Auth] Token validation failed: {e}")
|
| 571 |
+
return AuthStatus(
|
| 572 |
+
authenticated=False,
|
| 573 |
+
username=None,
|
| 574 |
+
message="Invalid or expired token"
|
| 575 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 576 |
|
| 577 |
|
| 578 |
def cleanup_generated_code(code: str, language: str) -> str:
|
|
|
|
| 845 |
{"role": "user", "content": user_content}
|
| 846 |
]
|
| 847 |
|
| 848 |
+
# Tìm đoạn "try: # Stream the response" trong backend_api.py và thay bằng:
|
| 849 |
try:
|
| 850 |
+
# Sử dụng Gemini stream
|
| 851 |
+
response = client.generate_content(
|
| 852 |
+
f"Generate a {language} application: {query}",
|
|
|
|
|
|
|
|
|
|
| 853 |
stream=True
|
| 854 |
)
|
| 855 |
|
| 856 |
+
for chunk in response:
|
| 857 |
+
if chunk.text:
|
| 858 |
+
chunk_content = chunk.text
|
| 859 |
+
generated_code += chunk_content
|
| 860 |
+
|
| 861 |
+
event_data = json.dumps({
|
| 862 |
+
"type": "chunk",
|
| 863 |
+
"content": chunk_content
|
| 864 |
+
})
|
| 865 |
+
yield f"data: {event_data}\n\n"
|
| 866 |
+
await asyncio.sleep(0.01) # Tránh nghẽn stream
|
| 867 |
+
|
| 868 |
+
|
| 869 |
chunk_count = 0
|
| 870 |
|
| 871 |
# Only process stream if it exists
|