Spaces:
Sleeping
Sleeping
Update main.py
Browse files
main.py
CHANGED
|
@@ -1,99 +1,91 @@
|
|
| 1 |
-
from fastapi import FastAPI, HTTPException, Depends, Request
|
| 2 |
-
from fastapi.middleware.cors import CORSMiddleware
|
| 3 |
-
from fastapi.security import HTTPBearer, HTTPAuthorizationCredentials
|
| 4 |
-
import decode_token
|
| 5 |
-
import function.recommender
|
| 6 |
-
import function.recommender.recommend
|
| 7 |
-
from controller import CallbackController,ChatController,RedirectController,GroupOrderController
|
| 8 |
-
from request import RequestChat
|
| 9 |
-
from typing import Dict
|
| 10 |
-
from fastapi.responses import RedirectResponse
|
| 11 |
-
|
| 12 |
-
app = FastAPI(
|
| 13 |
-
title="ChatBot HMDrinks",
|
| 14 |
-
description="ChatBot HMDrinks for Customizing Chatbots by Nguyen Le Gia Han(21110432) & Nguyen Thuy Diem My(21110)",
|
| 15 |
-
version="1.0.0",
|
| 16 |
-
contact={
|
| 17 |
-
"name": "Nguyen Le Gia Han",
|
| 18 |
-
"url": "https://pychatbot1.streamlit.app",
|
| 19 |
-
"email": "21110432@student.hcmute.edu.vn",
|
| 20 |
-
},
|
| 21 |
-
license_info={
|
| 22 |
-
"name": "Apache 2.0",
|
| 23 |
-
"url": "https://www.apache.org/licenses/LICENSE-2.0.html",
|
| 24 |
-
},
|
| 25 |
-
swagger_ui_parameters={
|
| 26 |
-
"syntaxHighlight.theme": "monokai"
|
| 27 |
-
}
|
| 28 |
-
)
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
app.add_middleware(
|
| 32 |
-
CORSMiddleware,
|
| 33 |
-
allow_origins=["http://localhost:1010"],
|
| 34 |
-
allow_credentials=True,
|
| 35 |
-
allow_methods=["*"],
|
| 36 |
-
allow_headers=["*"],
|
| 37 |
-
)
|
| 38 |
-
|
| 39 |
-
class JWTBearer(HTTPBearer):
|
| 40 |
-
def __init__(self, auto_error: bool = True):
|
| 41 |
-
super(JWTBearer, self).__init__(auto_error=auto_error)
|
| 42 |
-
|
| 43 |
-
async def __call__(self, request: Request):
|
| 44 |
-
credentials: HTTPAuthorizationCredentials = await super(JWTBearer, self).__call__(request)
|
| 45 |
-
if credentials:
|
| 46 |
-
if credentials.scheme != "Bearer":
|
| 47 |
-
raise HTTPException(status_code=401, detail="Invalid authentication scheme.")
|
| 48 |
-
return credentials.credentials
|
| 49 |
-
else:
|
| 50 |
-
raise HTTPException(status_code=401, detail="Invalid authorization code.")
|
| 51 |
-
|
| 52 |
-
jwt_bearer = JWTBearer()
|
| 53 |
-
from fastapi.responses import JSONResponse
|
| 54 |
-
|
| 55 |
-
@app.post("/api/v1/recommend", tags=["Recommend"])
|
| 56 |
-
async def get_chat_details(request: RequestChat.RecommendRequest,token: str = Depends(jwt_bearer)):
|
| 57 |
-
try:
|
| 58 |
-
user_id_token = decode_token.JwtService.extract_user_id(token)
|
| 59 |
-
data = await function.recommender.recommend.recommend(int(user_id_token),request.number)
|
| 60 |
-
return data
|
| 61 |
-
except Exception as e:
|
| 62 |
-
print("Lỗi khi gọi recommend:", e)
|
| 63 |
-
return JSONResponse(content={"error": "Internal server error"}, status_code=500)
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
@app.get("/grouporder/",tags=["GroupOrder"])
|
| 67 |
-
async def group_order_redirect(request: Request):
|
| 68 |
-
try:
|
| 69 |
-
params: Dict[str, str] = dict(request.query_params)
|
| 70 |
-
code = params.get("code")
|
| 71 |
-
print(code)
|
| 72 |
-
if not code:
|
| 73 |
-
return RedirectResponse(url="myapp://open/group-order?status=-1")
|
| 74 |
-
|
| 75 |
-
return RedirectResponse(url=f"myapp://open/group-order?status=1&code={code}")
|
| 76 |
-
except Exception as e:
|
| 77 |
-
print("Lỗi khi gọi group order:", e)
|
| 78 |
-
return JSONResponse(content={"error": "Internal server error"}, status_code=500)
|
| 79 |
-
|
| 80 |
-
app.include_router(CallbackController.router, prefix="/api/v1/callback",tags=["CallBack"])
|
| 81 |
-
app.include_router(ChatController.router, prefix="/api/v1/chat",tags=["ChatBot"])
|
| 82 |
-
app.include_router(RedirectController.router, prefix="/api/v1/redirect",tags=["Redirect"])
|
| 83 |
-
app.include_router(GroupOrderController.router, prefix="/api/v1/group-order",tags=["GroupOrder"])
|
| 84 |
-
|
| 85 |
-
import nest_asyncio
|
| 86 |
-
from uvicorn import run
|
| 87 |
-
nest_asyncio.apply()
|
| 88 |
-
from pyngrok import ngrok
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
# NGROK_STATIC_DOMAIN = "living-accurately-mackerel.ngrok-free.app"
|
| 93 |
-
# NGROK_TOKEN = "2osXAUfmjMduYmyQUwjR2wSbUJ7_2nr9qR43nduNTWrwEYHmq"
|
| 94 |
-
|
| 95 |
-
if __name__ == "__main__":
|
| 96 |
-
ngrok.set_auth_token(NGROK_TOKEN)
|
| 97 |
-
ngrok_tunnel = ngrok.connect(8085, domain=NGROK_STATIC_DOMAIN)
|
| 98 |
-
print('Public URL:', ngrok_tunnel.public_url)
|
| 99 |
run(app, port=8085)
|
|
|
|
| 1 |
+
from fastapi import FastAPI, HTTPException, Depends, Request
|
| 2 |
+
from fastapi.middleware.cors import CORSMiddleware
|
| 3 |
+
from fastapi.security import HTTPBearer, HTTPAuthorizationCredentials
|
| 4 |
+
import decode_token
|
| 5 |
+
import function.recommender
|
| 6 |
+
import function.recommender.recommend
|
| 7 |
+
from controller import CallbackController,ChatController,RedirectController,GroupOrderController
|
| 8 |
+
from request import RequestChat
|
| 9 |
+
from typing import Dict
|
| 10 |
+
from fastapi.responses import RedirectResponse
|
| 11 |
+
|
| 12 |
+
app = FastAPI(
|
| 13 |
+
title="ChatBot HMDrinks",
|
| 14 |
+
description="ChatBot HMDrinks for Customizing Chatbots by Nguyen Le Gia Han(21110432) & Nguyen Thuy Diem My(21110)",
|
| 15 |
+
version="1.0.0",
|
| 16 |
+
contact={
|
| 17 |
+
"name": "Nguyen Le Gia Han",
|
| 18 |
+
"url": "https://pychatbot1.streamlit.app",
|
| 19 |
+
"email": "21110432@student.hcmute.edu.vn",
|
| 20 |
+
},
|
| 21 |
+
license_info={
|
| 22 |
+
"name": "Apache 2.0",
|
| 23 |
+
"url": "https://www.apache.org/licenses/LICENSE-2.0.html",
|
| 24 |
+
},
|
| 25 |
+
swagger_ui_parameters={
|
| 26 |
+
"syntaxHighlight.theme": "monokai"
|
| 27 |
+
}
|
| 28 |
+
)
|
| 29 |
+
|
| 30 |
+
|
| 31 |
+
app.add_middleware(
|
| 32 |
+
CORSMiddleware,
|
| 33 |
+
allow_origins=["http://localhost:1010"],
|
| 34 |
+
allow_credentials=True,
|
| 35 |
+
allow_methods=["*"],
|
| 36 |
+
allow_headers=["*"],
|
| 37 |
+
)
|
| 38 |
+
|
| 39 |
+
class JWTBearer(HTTPBearer):
|
| 40 |
+
def __init__(self, auto_error: bool = True):
|
| 41 |
+
super(JWTBearer, self).__init__(auto_error=auto_error)
|
| 42 |
+
|
| 43 |
+
async def __call__(self, request: Request):
|
| 44 |
+
credentials: HTTPAuthorizationCredentials = await super(JWTBearer, self).__call__(request)
|
| 45 |
+
if credentials:
|
| 46 |
+
if credentials.scheme != "Bearer":
|
| 47 |
+
raise HTTPException(status_code=401, detail="Invalid authentication scheme.")
|
| 48 |
+
return credentials.credentials
|
| 49 |
+
else:
|
| 50 |
+
raise HTTPException(status_code=401, detail="Invalid authorization code.")
|
| 51 |
+
|
| 52 |
+
jwt_bearer = JWTBearer()
|
| 53 |
+
from fastapi.responses import JSONResponse
|
| 54 |
+
|
| 55 |
+
@app.post("/api/v1/recommend", tags=["Recommend"])
|
| 56 |
+
async def get_chat_details(request: RequestChat.RecommendRequest,token: str = Depends(jwt_bearer)):
|
| 57 |
+
try:
|
| 58 |
+
user_id_token = decode_token.JwtService.extract_user_id(token)
|
| 59 |
+
data = await function.recommender.recommend.recommend(int(user_id_token),request.number)
|
| 60 |
+
return data
|
| 61 |
+
except Exception as e:
|
| 62 |
+
print("Lỗi khi gọi recommend:", e)
|
| 63 |
+
return JSONResponse(content={"error": "Internal server error"}, status_code=500)
|
| 64 |
+
|
| 65 |
+
|
| 66 |
+
@app.get("/grouporder/",tags=["GroupOrder"])
|
| 67 |
+
async def group_order_redirect(request: Request):
|
| 68 |
+
try:
|
| 69 |
+
params: Dict[str, str] = dict(request.query_params)
|
| 70 |
+
code = params.get("code")
|
| 71 |
+
print(code)
|
| 72 |
+
if not code:
|
| 73 |
+
return RedirectResponse(url="myapp://open/group-order?status=-1")
|
| 74 |
+
|
| 75 |
+
return RedirectResponse(url=f"myapp://open/group-order?status=1&code={code}")
|
| 76 |
+
except Exception as e:
|
| 77 |
+
print("Lỗi khi gọi group order:", e)
|
| 78 |
+
return JSONResponse(content={"error": "Internal server error"}, status_code=500)
|
| 79 |
+
|
| 80 |
+
app.include_router(CallbackController.router, prefix="/api/v1/callback",tags=["CallBack"])
|
| 81 |
+
app.include_router(ChatController.router, prefix="/api/v1/chat",tags=["ChatBot"])
|
| 82 |
+
app.include_router(RedirectController.router, prefix="/api/v1/redirect",tags=["Redirect"])
|
| 83 |
+
app.include_router(GroupOrderController.router, prefix="/api/v1/group-order",tags=["GroupOrder"])
|
| 84 |
+
|
| 85 |
+
import nest_asyncio
|
| 86 |
+
from uvicorn import run
|
| 87 |
+
nest_asyncio.apply()
|
| 88 |
+
from pyngrok import ngrok
|
| 89 |
+
|
| 90 |
+
if __name__ == "__main__":
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 91 |
run(app, port=8085)
|