Spaces:
Sleeping
Sleeping
| from fastapi import FastAPI, HTTPException, Depends, Request | |
| from fastapi.middleware.cors import CORSMiddleware | |
| from fastapi.security import HTTPBearer, HTTPAuthorizationCredentials | |
| import decode_token | |
| import function.recommender | |
| import function.recommender.recommend | |
| from controller import CallbackController,ChatController,RedirectController,GroupOrderController,RefundController | |
| from request import RequestChat | |
| from typing import Dict | |
| from fastapi.responses import RedirectResponse | |
| app = FastAPI( | |
| title="ChatBot HMDrinks", | |
| description="""ChatBot HMDrinks for Customizing Chatbots by Nguyen Le Gia Han(21110432) & Nguyen Thuy Diem My(21110549) | |
| ### 🌐 Liên kết hệ thống | |
| - 🔗 **Frontend Website**: [HMDrinks Website](https://hmdrinks-t8e3-giahans-projects.vercel.app/) | |
| - 🔗 **API Java Docs**: [Swagger UI](https://hmdrinks-be.up.railway.app/swagger-ui/index.html) | |
| - 🔗 **API Python Docs**: [Swagger UI](https://kltn21110-chatbot-server.hf.space/docs) | |
| --- | |
| """, | |
| version="1.0.0", | |
| contact={ | |
| "name": "Nguyen Le Gia Han", | |
| "url": "https://hmdrinks-t8e3-giahans-projects.vercel.app/", | |
| "email": "21110432@student.hcmute.edu.vn", | |
| }, | |
| license_info={ | |
| "name": "Apache 2.0", | |
| "url": "https://www.apache.org/licenses/LICENSE-2.0.html", | |
| }, | |
| swagger_ui_parameters={ | |
| "syntaxHighlight.theme": "monokai" | |
| } | |
| ) | |
| app.add_middleware( | |
| CORSMiddleware, | |
| allow_origins=["https://kltn21110-chatbot-server.hf.space"], | |
| allow_credentials=True, | |
| allow_methods=["*"], | |
| allow_headers=["*"], | |
| ) | |
| class JWTBearer(HTTPBearer): | |
| def __init__(self, auto_error: bool = True): | |
| super(JWTBearer, self).__init__(auto_error=auto_error) | |
| async def __call__(self, request: Request): | |
| credentials: HTTPAuthorizationCredentials = await super(JWTBearer, self).__call__(request) | |
| if credentials: | |
| if credentials.scheme != "Bearer": | |
| raise HTTPException(status_code=401, detail="Invalid authentication scheme.") | |
| return credentials.credentials | |
| else: | |
| raise HTTPException(status_code=401, detail="Invalid authorization code.") | |
| jwt_bearer = JWTBearer() | |
| from fastapi.responses import JSONResponse | |
| async def get_chat_details(request: RequestChat.RecommendRequest,token: str = Depends(jwt_bearer)): | |
| try: | |
| user_id_token = decode_token.JwtService.extract_user_id(token) | |
| data = await function.recommender.recommend.recommend(int(user_id_token),request.number) | |
| return data | |
| except Exception as e: | |
| print("Lỗi khi gọi recommend:", e) | |
| return JSONResponse(content={"error": "Internal server error"}, status_code=500) | |
| async def group_order_redirect(request: Request): | |
| try: | |
| params: Dict[str, str] = dict(request.query_params) | |
| code = params.get("code") | |
| print(code) | |
| if not code: | |
| return RedirectResponse(url="myapp://open/group-order?status=-1") | |
| return RedirectResponse(url=f"myapp://open/group-order?status=1&code={code}") | |
| except Exception as e: | |
| print("Lỗi khi gọi group order:", e) | |
| return JSONResponse(content={"error": "Internal server error"}, status_code=500) | |
| app.include_router(CallbackController.router, prefix="/api/v1/callback",tags=["CallBack"]) | |
| app.include_router(ChatController.router, prefix="/api/v1/chat",tags=["ChatBot"]) | |
| app.include_router(RedirectController.router, prefix="/api/v1/redirect",tags=["Redirect"]) | |
| app.include_router(GroupOrderController.router, prefix="/api/v1/group-order",tags=["GroupOrder"]) | |
| app.include_router(RefundController.router, prefix="/api/v1/refund-payment",tags=["Refund"]) | |