kltn20133118 commited on
Commit
b58eaeb
·
verified ·
1 Parent(s): fc06cd8

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +331 -335
main.py CHANGED
@@ -1,336 +1,332 @@
1
- from fastapi import FastAPI, HTTPException, Depends, Request
2
- from fastapi.middleware.cors import CORSMiddleware
3
- from fastapi.middleware.trustedhost import TrustedHostMiddleware
4
- from controller import LoginController, FileController, MySQLController, ChatController, DefaultController, OTPController
5
- import firebase_admin
6
- from firebase_admin import credentials
7
- from fastapi.security import HTTPBearer, HTTPAuthorizationCredentials
8
- from service import MySQLService,LoginService,ChatService
9
- from request import RequestMySQL,RequestLogin,RequestDefault
10
- from auth.authentication import decodeJWT
11
- from repository import UserRepository
12
- from auth import authentication
13
- from datetime import datetime, timedelta
14
- from fastapi import Depends, HTTPException, Form, File, UploadFile,Query
15
- from typing import List,Optional
16
- from service import FileService,DefaultService,LoginServiceF
17
- from request import RequestFile,RequestChat,RequestDefault
18
- from fastapi import FastAPI, Request, HTTPException
19
- from fastapi.responses import JSONResponse
20
- from pydantic.error_wrappers import ErrorWrapper
21
- import json
22
- from function import support_function
23
- from response import ResponseDefault as res
24
-
25
- app = FastAPI(
26
- title="ChatBot HCMUTE",
27
- description="Python ChatBot is intended for use in the topic Customizing chatbots. With the construction of 2 students Vo Nhu Y - 20133118 and Nguyen Quang Phuc 20133080",
28
- swagger_ui_parameters={"syntaxHighlight.theme": "obsidian"},
29
- version="1.0.0",
30
- contact={
31
- "name": "Vo Nhu Y",
32
- "url": "https://pychatbot1.streamlit.app",
33
- "email": "vonhuy5112002@gmail.com",
34
- },
35
- license_info={
36
- "name": "Apache 2.0",
37
- "url": "https://www.apache.org/licenses/LICENSE-2.0.html",
38
- }
39
- )
40
- origins = [
41
- "http://localhost:8000",
42
- ]
43
-
44
- app.add_middleware(
45
- CORSMiddleware,
46
- allow_origins=origins,
47
- allow_credentials=True,
48
- allow_methods=["*"],
49
- allow_headers=["*"],
50
- )
51
- ALLOWED_EXTENSIONS = {'csv', 'txt', 'doc', 'docx', 'pdf', 'xlsx', 'pptx', 'json', 'md'}
52
-
53
- def allowed_file(filename):
54
- return '.' in filename and filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
55
-
56
- if not firebase_admin._apps:
57
- cred = credentials.Certificate("firebase_certificate.json")
58
- fred = firebase_admin.initialize_app(cred)
59
-
60
- class JWTBearer(HTTPBearer):
61
- def __init__(self, auto_error: bool = True):
62
- super(JWTBearer, self).__init__(auto_error=auto_error)
63
-
64
- async def __call__(self, request: Request):
65
- credentials: HTTPAuthorizationCredentials = await super(JWTBearer, self).__call__(request)
66
- if credentials:
67
- if credentials.scheme != "Bearer":
68
- raise HTTPException(status_code=403, detail="Invalid authentication scheme.")
69
- if not self.verify_accesstoken(credentials.credentials):
70
- raise HTTPException(status_code=403, detail="Token does not exist")
71
- if not self.verify_jwt(credentials.credentials):
72
- raise HTTPException(status_code=403, detail="Invalid token or expired token.")
73
- return credentials.credentials
74
- else:
75
- raise HTTPException(status_code=401, detail="Invalid authorization code.")
76
-
77
- def verify_accesstoken(self, jwtoken: str) -> bool:
78
- check = LoginService.check_token_is_valid(jwtoken)
79
- return check
80
- def verify_jwt(self, jwtoken: str) -> bool:
81
- try:
82
- payload = decodeJWT(jwtoken)
83
- email_encode = payload.get('sub')
84
- self.email = authentication.str_decode(email_encode)
85
- return True
86
- except Exception as e:
87
- print(e)
88
- return False
89
-
90
- def get_current_user_email(credentials: str = Depends(JWTBearer())):
91
- try:
92
- payload = decodeJWT(credentials)
93
- email_encode = payload.get('sub')
94
- email = authentication.str_decode(email_encode)
95
- return email
96
- except Exception as e:
97
- print(e)
98
- raise HTTPException(status_code=403, detail="Invalid token or expired token.")
99
-
100
- @app.get("/api/mysql/render_chat_history",dependencies=[Depends(JWTBearer())], tags=["MySQL"])
101
- async def override_render_chat(user_id: Optional[str] = Query(None), current_user_email: str = Depends(get_current_user_email)):
102
- check = support_function.check_value_user_id(user_id, current_user_email)
103
- if check != True:
104
- return check
105
- request = RequestMySQL.RequestRenderChatHistory(user_id=user_id)
106
- return MySQLService.render_chat_history(request)
107
-
108
- @app.put("/api/mysql/edit_chat/", dependencies=[Depends(JWTBearer())], tags=["MySQL"])
109
- async def override_edit_chat(request: RequestMySQL.RequestEditNameChat, current_user_email: str = Depends(get_current_user_email)):
110
- user_id = request.user_id
111
- check = support_function.check_value_user_id(user_id, current_user_email)
112
- if check != True:
113
- return check
114
- name_new = request.name_new
115
- if name_new is None or name_new.strip() == "":
116
- raise HTTPException(status_code=400, detail="name_new field is required.")
117
- name_old = request.name_old
118
- if name_old is None or name_old.strip() == "":
119
- raise HTTPException(status_code=400, detail="name_old field is required.")
120
- return MySQLService.edit_chat(request)
121
-
122
- @app.delete("/api/mysql/delete_chat/", dependencies=[Depends(JWTBearer())], tags=["MySQL"])
123
- async def override_delete_chat(request: RequestMySQL.RequestDeleteChat, current_user_email: str = Depends(get_current_user_email)):
124
- user_id = request.user_id
125
- check = support_function.check_value_user_id(user_id, current_user_email)
126
- if check is not True:
127
- return check
128
- chat_name= request.chat_name
129
- if chat_name is None or chat_name.strip() == "":
130
- raise HTTPException(status_code=400, detail="chat_name field is required.")
131
- return MySQLService.delete_chat(request)
132
-
133
-
134
- @app.get("/load_chat_history",dependencies=[Depends(JWTBearer())] ,tags=["MySQL"])
135
- async def override_load_chat(chat_id: Optional[str] = Query(None), user_id: Optional[str] = Query(None), current_user_email: str = Depends(get_current_user_email)):
136
- check = support_function.check_value_user_id(user_id, current_user_email)
137
- if check is not True:
138
- return check
139
- if chat_id is None or chat_id.strip() == "":
140
- return res.ReponseError(status=400,
141
- data=res.Message(message="Chat id field is required."))
142
- chat_id = chat_id.strip("'").strip('"')
143
- try:
144
- chat_id_int = int(chat_id)
145
- except ValueError:
146
- return res.ReponseError(status=400,
147
- data=res.Message(message="Value must be an integer"))
148
- if not support_function.is_positive_integer(chat_id_int):
149
- return res.ReponseError(status=400,
150
- data=res.Message(message="Value must be greater than 0"))
151
- request = RequestMySQL.RequestLoadChatHistory(chat_id=chat_id)
152
- return MySQLService.load_chat_history(request)
153
-
154
- @app.get("/info_user",dependencies=[Depends(JWTBearer())], tags=["Default"])
155
- async def override_get_user(user_id: Optinal[str] = Query(None)):
156
- check = support_function.check_value_user_id_controller(user_id)
157
- if check is not True:
158
- return check
159
- request = RequestDefault.RequestInfoUser(user_id=user_id)
160
- return DefaultService.info_user(request)
161
-
162
- @app.post("/api/users/update_user_info", dependencies=[Depends(JWTBearer())], tags=["Login"])
163
- async def override_update_user_info(request: RequestLogin.RequestUpdateUserInfo, current_user_email: str = Depends(get_current_user_email)):
164
- user_id = request.user_id
165
- check = support_function.check_value_user_id(user_id, current_user_email)
166
- if check != True:
167
- return check
168
- uid = request.uid
169
- email = request.email
170
- display_name = request.display_name
171
- photo_url = request.photo_url
172
- if uid is None or uid.strip() == "":
173
- raise HTTPException(status_code=400, detail="uid field is required.")
174
- if email is None or email.strip() == "":
175
- return res.ReponseError(status=400,
176
- data=res.Message(message="email field is required."))
177
- if display_name is None or display_name.strip() == "":
178
- return res.ReponseError(status=400,
179
- data=res.Message(message="display_name field is required."))
180
- if photo_url is None or photo_url.strip() == "":
181
- return res.ReponseError(status=400,
182
- data=res.Message(message="photo_url field is required."))
183
- return LoginService.update_user_info(request)
184
-
185
- @app.put('/api/users/change_password', dependencies=[Depends(JWTBearer())], tags=["Login"])
186
- async def override_reset_password_firebase(request: RequestLogin.RequestChangePassword, current_user_email: str = Depends(get_current_user_email)):
187
- user_id = request.user_id
188
- check = support_function.check_value_user_id(user_id, current_user_email)
189
- if check != True:
190
- return check
191
- new_password = request.new_password
192
- current_password = request.current_password
193
- if new_password is None or new_password.strip() == "":
194
- return res.ReponseError(status=400,
195
- data=res.Message(message="new_password field is required."))
196
- if current_password is None or current_password.strip() == "":
197
- return res.ReponseError(status=400,
198
- data=res.Message(message="current_password field is required."))
199
- return LoginService.change_password(request)
200
- @app.delete("/api/file/delete_all_file/", dependencies=[Depends(JWTBearer())], tags=["File"])
201
- async def override_delete_folder(request: RequestFile.RequestDeleteAllFile, current_user_email: str = Depends(get_current_user_email)):
202
- check = support_function.check_value_user_id(request.user_id, current_user_email)
203
- if check != True:
204
- return check
205
- return FileService.deleteAllFile(request)
206
-
207
- # @app.get("/api/file/list_name_files/", dependencies=[Depends(JWTBearer())], tags=["File"])
208
- # async def override_get_list_name_files(user_id: str , current_user_email: str = Depends(get_current_user_email)):
209
- # check = support_function.check_value_user_id(user_id, current_user_email)
210
- # if check != True:
211
- # return check
212
- # request = RequestFile.RequestGetNameFile(user_id=user_id)
213
- # return FileService.listNameFiles(request)
214
-
215
- @app.delete("/api/file/delete_one_file/", dependencies=[Depends(JWTBearer())], tags=["File"])
216
- async def override_delete_one_file(request: RequestFile.RequestDeleteFile, current_user_email: str = Depends(get_current_user_email)):
217
- user_id = request.user_id
218
- check = support_function.check_value_user_id(user_id, current_user_email)
219
- if check != True:
220
- return check
221
- name_file = request.name_file
222
- if name_file is None or name_file.strip() == "":
223
- return res.ReponseError(status=400,
224
- data=res.Message(message="name_file is required."))
225
- return FileService.deleteFile(request)
226
-
227
- @app.post("/api/file/chatbot/download_folder/", dependencies=[Depends(JWTBearer())], tags=["File"])
228
- async def override_download_folder_from_dropbox(request: RequestFile.RequestDownLoadFolder, current_user_email: str = Depends(get_current_user_email)):
229
- user_id = request.user_id
230
- check = support_function.check_value_user_id(user_id, current_user_email)
231
- if check != True:
232
- return check
233
- return FileService.download_folder(request)
234
-
235
- @app.post("/api/file/chatbot/download_files/", dependencies=[Depends(JWTBearer())], tags=["File"])
236
- async def override_download_file_by_id(request: RequestFile.RequestDownLoadFile, current_user_email: str = Depends(get_current_user_email)):
237
- user_id = request.user_id
238
- check = support_function.check_value_user_id(user_id, current_user_email)
239
- if check != True:
240
- return check
241
- return FileService.download_file(request)
242
-
243
- @app.post("/api/file/upload_files/", dependencies=[Depends(JWTBearer())], tags=["File"])
244
- async def override_upload_files_dropbox(
245
- user_id: str = Form(None),
246
- files: List[UploadFile] = File(None),
247
- current_user_email: str = Depends(get_current_user_email)
248
- ):
249
- check = support_function.check_value_user_id(user_id, current_user_email)
250
- if check != True:
251
- return check
252
- request = RequestFile.RequestUploadFile(files=files, user_id=user_id)
253
- return FileService.upload_files(request)
254
-
255
- @app.post("/api/chat/chatbot/query/", dependencies=[Depends(JWTBearer())], tags=["Chatbot"])
256
- async def override_handle_query2_upgrade_old(request: Request, user_id: str = Form(None), text_all: str = Form(...), question: str = Form(None), chat_name: str = Form(None), current_user_email: str = Depends(get_current_user_email)):
257
- check = support_function.check_value_user_id(user_id, current_user_email)
258
- if check != True:
259
- return check
260
- request = RequestChat.RequestQuery2UpgradeOld(user_id=user_id, text_all=text_all, question=question, chat_name=chat_name)
261
- return ChatService.query2_upgrade_old(request)
262
-
263
- @app.get("/api/chat/chatbot/extract_file/", dependencies=[Depends(JWTBearer())], tags=["Chatbot"])
264
- async def override_extract_file(user_id: Optional[str] = Query(None), current_user_email: str = Depends(get_current_user_email)):
265
- check = support_function.check_value_user_id(user_id,current_user_email)
266
- if check != True:
267
- return check
268
- request = RequestChat.RequestExtractFile(user_id=user_id)
269
- return ChatService.extract_file(request)
270
-
271
- @app.get("/api/chat/chatbot/generate_question/", dependencies=[Depends(JWTBearer())], tags=["Chatbot"])
272
- async def override_generate_question(user_id: Optional[str] = Query(None) , current_user_email: str = Depends(get_current_user_email)):
273
- check = support_function.check_value_user_id(user_id,current_user_email)
274
- if check != True:
275
- return check
276
- request = RequestChat.RequestGenerateQuestion(user_id=user_id)
277
- return ChatService.generate_question(request)
278
-
279
- @app.post("/api/default/upload_image/", dependencies=[Depends(JWTBearer())], tags=["Default"])
280
- async def override_upload_image(user_id: str = Form(None), file: UploadFile = File(...),current_user_email: str = Depends(get_current_user_email)):
281
- check = support_function.check_value_user_id(user_id,current_user_email)
282
- if check != True:
283
- return check
284
- request = RequestDefault.RequestUpLoadImage(user_id=user_id, files=file)
285
- return DefaultService.upload_image_service(request)
286
-
287
- app.include_router(MySQLController.router, prefix="/api/mysql")
288
- app.include_router(LoginController.router, prefix="/api/users")
289
- app.include_router(FileController.router,prefix="/api/file")
290
- app.include_router(ChatController.router,prefix="/api/chat")
291
- app.include_router(DefaultController.router,prefix="/api/default")
292
-
293
- routes_to_override = {
294
- "/api/mysql/render_chat_history":{"GET"},
295
- "/api/mysql/load_chat_history":{"GET"},
296
- "/api/mysql/edit_chat/": {"PUT"},
297
- "/api/mysql/delete_chat/": {"DELETE"},
298
- "/api/users/update_user_info" :{"POST"},
299
- "/api/users/change_password" :{"PUT"},
300
- "/api/file/delete_all_file/" :{"DELETE"},
301
- "/api/file/delete_one_file/" :{"DELETE"},
302
- "/api/file/chatbot/download_folder/" :{"POST"},
303
- "/api/file/chatbot/download_files/" :{"POST"},
304
- "/api/file/upload_files/":{"POST"},
305
- "/api/chat/query2_upgrade/":{"POST"},
306
- "/api/chat/chatbot/query/" : {"POST"},
307
- "/api/chat/chatbot/extract_file/":{"GET"},
308
- "/api/chat/chatbot/generate_question/":{"GET"},
309
- "/api/default/upload_image/":{"POST"}
310
- }
311
-
312
- app.router.routes = [
313
- route for route in app.router.routes
314
- if not (
315
- route.path in routes_to_override and
316
- route.methods.intersection(routes_to_override[route.path])
317
- )
318
- ]
319
-
320
- app.add_api_route("/api/mysql/render_chat_history", override_render_chat, methods=["GET"], dependencies=[Depends(JWTBearer())], tags=["MySQL"])
321
- app.add_api_route("/api/mysql/load_chat_history/", override_load_chat, methods=["GET"], dependencies=[Depends(JWTBearer())], tags=["MySQL"])
322
- app.add_api_route("/api/mysql/edit_chat/", override_edit_chat, methods=["PUT"], dependencies=[Depends(JWTBearer())], tags=["MySQL"])
323
- app.add_api_route("/api/mysql/delete_chat/", override_delete_chat, methods=["DELETE"], dependencies=[Depends(JWTBearer())], tags=["MySQL"])
324
- app.add_api_route("/api/users/update_user_info",override_update_user_info,methods=["POST"],dependencies=[Depends(JWTBearer())], tags=["Login"])
325
- app.add_api_route("/api/users/change_password",override_reset_password_firebase,methods=["PUT"],dependencies=[Depends(JWTBearer())], tags=["Login"])
326
- app.add_api_route("/api/file/delete_all_file/",override_delete_folder,methods=["DELETE"],dependencies=[Depends(JWTBearer())], tags=["File"])
327
- # app.add_api_route("/api/file/list_name_files/",override_get_list_name_files,methods=["GET"],dependencies=[Depends(JWTBearer())], tags=["File"])
328
- app.add_api_route("/api/file/delete_one_file/",override_delete_one_file,methods=["DELETE"],dependencies=[Depends(JWTBearer())], tags=["File"])
329
- app.add_api_route("/api/file/chatbot/download_folder/",override_download_folder_from_dropbox,methods=["POST"],dependencies=[Depends(JWTBearer())], tags=["File"])
330
- app.add_api_route("/api/file/chatbot/download_files/",override_download_file_by_id,methods=["POST"],dependencies=[Depends(JWTBearer())], tags=["File"])
331
- app.add_api_route("/api/file/upload_files/",override_upload_files_dropbox,methods=["POST"],dependencies=[Depends(JWTBearer())], tags=["File"])
332
- app.add_api_route("/api/chat/chatbot/query/",override_handle_query2_upgrade_old,methods=["POST"],dependencies=[Depends(JWTBearer())], tags=["Chat"])
333
- app.add_api_route("/api/chat/chatbot/extract_file/",override_extract_file,methods=["GET"],dependencies=[Depends(JWTBearer())], tags=["Chat"])
334
- app.add_api_route("/api/chat/chatbot/generate_question/",override_generate_question,methods=["GET"],dependencies=[Depends(JWTBearer())], tags=["Chat"])
335
- app.add_api_route("/api/default/upload_image/",override_upload_image,methods=["POST"],dependencies=[Depends(JWTBearer())], tags=["Default"])
336
  app.include_router(OTPController.router, tags = ["OTP"], prefix="/api/otp")
 
1
+ from fastapi import FastAPI, HTTPException, Depends, Request
2
+ from fastapi.middleware.cors import CORSMiddleware
3
+ from fastapi.middleware.trustedhost import TrustedHostMiddleware
4
+ from controller import LoginController, FileController, MySQLController, DefaultController, OTPController
5
+ import firebase_admin
6
+ from controller import ChatController
7
+ from firebase_admin import credentials
8
+ from fastapi.security import HTTPBearer, HTTPAuthorizationCredentials
9
+ from service import MySQLService,LoginService,ChatService
10
+ from request import RequestMySQL,RequestLogin,RequestDefault
11
+ from auth.authentication import decodeJWT
12
+ from repository import UserRepository
13
+ from auth import authentication
14
+ from datetime import datetime, timedelta
15
+ from fastapi import Depends, HTTPException, Form, File, UploadFile
16
+ from typing import List
17
+ from service import FileService,DefaultService,LoginService
18
+ from request import RequestFile,RequestChat,RequestDefault
19
+ from fastapi import FastAPI, Request, HTTPException
20
+ from fastapi.responses import JSONResponse
21
+ from pydantic.error_wrappers import ErrorWrapper
22
+ from fastapi import Query
23
+ from typing import Optional
24
+ import json
25
+ from function import support_function
26
+ from response import ResponseDefault as res
27
+
28
+ app = FastAPI(
29
+ title="ChatBot HCMUTE",
30
+ description="Python ChatBot is intended for use in the topic Customizing chatbots. With the construction of 2 students Vo Nhu Y - 20133118 and Nguyen Quang Phuc 20133080",
31
+ swagger_ui_parameters={"syntaxHighlight.theme": "obsidian"},
32
+ version="1.0.0",
33
+ contact={
34
+ "name": "Vo Nhu Y",
35
+ "url": "https://pychatbot1.streamlit.app",
36
+ "email": "vonhuy5112002@gmail.com",
37
+ },
38
+ license_info={
39
+ "name": "Apache 2.0",
40
+ "url": "https://www.apache.org/licenses/LICENSE-2.0.html",
41
+ }
42
+ )
43
+ origins = [
44
+ "http://localhost:9090",
45
+ ]
46
+
47
+ app.add_middleware(
48
+ CORSMiddleware,
49
+ allow_origins=origins,
50
+ allow_credentials=True,
51
+ allow_methods=["*"],
52
+ allow_headers=["*"],
53
+ )
54
+ ALLOWED_EXTENSIONS = {'csv', 'txt', 'doc', 'docx', 'pdf', 'xlsx', 'pptx', 'json', 'md', 'xlsx'}
55
+
56
+ def allowed_file(filename):
57
+ return '.' in filename and filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
58
+
59
+ if not firebase_admin._apps:
60
+ cred = credentials.Certificate("firebase_certificate.json")
61
+ fred = firebase_admin.initialize_app(cred)
62
+
63
+ class JWTBearer(HTTPBearer):
64
+ def __init__(self, auto_error: bool = True):
65
+ super(JWTBearer, self).__init__(auto_error=auto_error)
66
+
67
+ async def __call__(self, request: Request):
68
+ credentials: HTTPAuthorizationCredentials = await super(JWTBearer, self).__call__(request)
69
+ if credentials:
70
+ if credentials.scheme != "Bearer":
71
+ raise HTTPException(status_code=403, detail="Invalid authentication scheme.")
72
+ if not self.verify_accesstoken(credentials.credentials):
73
+ raise HTTPException(status_code=403, detail="Token does not exist")
74
+ if not self.verify_jwt(credentials.credentials):
75
+ raise HTTPException(status_code=403, detail="Invalid token or expired token.")
76
+ return credentials.credentials
77
+ else:
78
+ raise HTTPException(status_code=401, detail="Invalid authorization code.")
79
+
80
+ def verify_accesstoken(self, jwtoken: str) -> bool:
81
+ check = LoginService.check_token_is_valid(jwtoken)
82
+ return check
83
+ def verify_jwt(self, jwtoken: str) -> bool:
84
+ try:
85
+ payload = decodeJWT(jwtoken)
86
+ email_encode = payload.get('sub')
87
+ self.email = authentication.str_decode(email_encode)
88
+ return True
89
+ except Exception as e:
90
+ print(e)
91
+ return False
92
+
93
+ def get_current_user_email(credentials: str = Depends(JWTBearer())):
94
+ try:
95
+ payload = decodeJWT(credentials)
96
+ email_encode = payload.get('sub')
97
+ email = authentication.str_decode(email_encode)
98
+ return email
99
+ except Exception as e:
100
+ print(e)
101
+ raise HTTPException(status_code=403, detail="Invalid token or expired token.")
102
+
103
+ @app.get("/api/mysql/render_chat_history",dependencies=[Depends(JWTBearer())], tags=["MySQL"])
104
+ async def override_render_chat(user_id: Optional[str] = Query(None), current_user_email: str = Depends(get_current_user_email)):
105
+ check = support_function.check_value_user_id(user_id, current_user_email)
106
+ if check is not True:
107
+ return check
108
+ request = RequestMySQL.RequestRenderChatHistory(user_id=user_id)
109
+ return MySQLService.render_chat_history(request)
110
+
111
+ @app.put("/api/mysql/edit_chat/", dependencies=[Depends(JWTBearer())], tags=["MySQL"])
112
+ async def override_edit_chat(request: RequestMySQL.RequestEditNameChat, current_user_email: str = Depends(get_current_user_email)):
113
+ user_id = request.user_id
114
+ check = support_function.check_value_user_id(user_id, current_user_email)
115
+ if check is not True:
116
+ return check
117
+ name_new = request.name_new
118
+ if name_new is None or name_new.strip() == "":
119
+ raise HTTPException(status_code=400, detail="name_new field is required.")
120
+ name_old = request.name_old
121
+ if name_old is None or name_old.strip() == "":
122
+ raise HTTPException(status_code=400, detail="name_old field is required.")
123
+ return MySQLService.edit_chat(request)
124
+
125
+ @app.delete("/api/mysql/delete_chat/", dependencies=[Depends(JWTBearer())], tags=["MySQL"])
126
+ async def override_delete_chat(request: RequestMySQL.RequestDeleteChat, current_user_email: str = Depends(get_current_user_email)):
127
+ user_id = request.user_id
128
+ check = support_function.check_value_user_id(user_id, current_user_email)
129
+ if check is not True:
130
+ return check
131
+ chat_name= request.chat_name
132
+ if chat_name is None or chat_name.strip() == "":
133
+ raise HTTPException(status_code=400, detail="chat_name field is required.")
134
+ return MySQLService.delete_chat(request)
135
+
136
+
137
+ @app.get("/api/mysql/load_chat_history",dependencies=[Depends(JWTBearer())] ,tags=["MySQL"])
138
+ async def override_load_chat(chat_id: Optional[str] = Query(None), user_id: Optional[str] = Query(None), current_user_email: str = Depends(get_current_user_email)):
139
+ check = support_function.check_value_user_id(user_id, current_user_email)
140
+ if check is not True:
141
+ return check
142
+ if chat_id is None or chat_id.strip() == "":
143
+ return res.ReponseError(status=400,
144
+ data=res.Message(message="chat_id field is required."))
145
+ chat_id = chat_id.strip("'").strip('"')
146
+ try:
147
+ chat_id_int = int(chat_id)
148
+ except ValueError:
149
+ return res.ReponseError(status=400,
150
+ data=res.Message(message="chat_id must be an integer"))
151
+ if not support_function.is_positive_integer(chat_id_int):
152
+ return res.ReponseError(status=400,
153
+ data=res.Message(message="chat_id must be greater than 0"))
154
+ request = RequestMySQL.RequestLoadChatHistory(chat_id=chat_id,user_id = user_id)
155
+ return MySQLService.load_chat_history(request)
156
+
157
+ @app.get("/api/default/info_user",dependencies=[Depends(JWTBearer())], tags=["Default"])
158
+ async def override_get_user(user_id: str = Query(None),current_user_email: str = Depends(get_current_user_email)):
159
+ check = support_function.check_value_user_id(user_id, current_user_email)
160
+ if check is not True:
161
+ return check
162
+ request = RequestDefault.RequestInfoUser(user_id=user_id)
163
+ return DefaultService.info_user(request)
164
+
165
+ @app.post("/api/users/update_user_info", dependencies=[Depends(JWTBearer())], tags=["Login"])
166
+ async def override_update_user_info(request: RequestLogin.RequestUpdateUserInfo, current_user_email: str = Depends(get_current_user_email)):
167
+ user_id = request.user_id
168
+ check = support_function.check_value_user_id(user_id, current_user_email)
169
+ if check != True:
170
+ return check
171
+ uid = request.uid
172
+ email = request.email
173
+ display_name = request.display_name
174
+ photo_url = request.photo_url
175
+ if uid is None or uid.strip() == "":
176
+ raise HTTPException(status_code=400, detail="uid field is required.")
177
+ if email is None or email.strip() == "":
178
+ return res.ReponseError(status=400,
179
+ data=res.Message(message="email field is required."))
180
+ if display_name is None or display_name.strip() == "":
181
+ return res.ReponseError(status=400,
182
+ data=res.Message(message="display_name field is required."))
183
+ if photo_url is None or photo_url.strip() == "":
184
+ return res.ReponseError(status=400,
185
+ data=res.Message(message="photo_url field is required."))
186
+ return LoginService.update_user_info(request)
187
+
188
+ @app.put('/api/users/change_password', dependencies=[Depends(JWTBearer())], tags=["Login"])
189
+ async def override_reset_password_firebase(request: RequestLogin.RequestChangePassword, current_user_email: str = Depends(get_current_user_email)):
190
+ user_id = request.user_id
191
+ check = support_function.check_value_user_id(user_id, current_user_email)
192
+ if check != True:
193
+ return check
194
+ new_password = request.new_password
195
+ current_password = request.current_password
196
+ if new_password is None or new_password.strip() == "":
197
+ return res.ReponseError(status=400,
198
+ data=res.Message(message="new_password field is required."))
199
+ if current_password is None or current_password.strip() == "":
200
+ return res.ReponseError(status=400,
201
+ data=res.Message(message="current_password field is required."))
202
+ return LoginService.change_password(request)
203
+ @app.delete("/api/file/delete_all_file/", dependencies=[Depends(JWTBearer())], tags=["File"])
204
+ async def override_delete_folder(request: RequestFile.RequestDeleteAllFile, current_user_email: str = Depends(get_current_user_email)):
205
+ check = support_function.check_value_user_id(request.user_id, current_user_email)
206
+ if check != True:
207
+ return check
208
+ return FileService.deleteAllFile(request)
209
+ @app.delete("/api/file/delete_one_file/", dependencies=[Depends(JWTBearer())], tags=["File"])
210
+ async def override_delete_one_file(request: RequestFile.RequestDeleteFile, current_user_email: str = Depends(get_current_user_email)):
211
+ user_id = request.user_id
212
+ check = support_function.check_value_user_id(user_id, current_user_email)
213
+ if check != True:
214
+ return check
215
+ name_file = request.name_file
216
+ if name_file is None or name_file.strip() == "":
217
+ return res.ReponseError(status=400,
218
+ data=res.Message(message="name_file is required."))
219
+ return FileService.deleteFile(request)
220
+
221
+ @app.post("/api/file/chatbot/download_folder/", dependencies=[Depends(JWTBearer())], tags=["File"])
222
+ async def override_download_folder_from_dropbox(request: RequestFile.RequestDownLoadFolder, current_user_email: str = Depends(get_current_user_email)):
223
+ user_id = request.user_id
224
+ check = support_function.check_value_user_id(user_id, current_user_email)
225
+ if check is not True:
226
+ return check
227
+ return FileService.download_folder(request)
228
+
229
+ @app.post("/api/file/chatbot/download_files/", dependencies=[Depends(JWTBearer())], tags=["File"])
230
+ async def override_download_file_by_id(request: RequestFile.RequestDownLoadFile, current_user_email: str = Depends(get_current_user_email)):
231
+ user_id = request.user_id
232
+ check = support_function.check_value_user_id(user_id, current_user_email)
233
+ if check is not True:
234
+ return check
235
+ return FileService.download_file(request)
236
+
237
+ @app.post("/api/file/upload_files/", dependencies=[Depends(JWTBearer())], tags=["File"])
238
+ async def override_upload_files_dropbox(
239
+ user_id: str = Form(None),
240
+ files: List[UploadFile] = File(None),
241
+ current_user_email: str = Depends(get_current_user_email)
242
+ ):
243
+ check = support_function.check_value_user_id(user_id, current_user_email)
244
+ if check is not True:
245
+ return check
246
+ request = RequestFile.RequestUploadFile(files=files, user_id=user_id)
247
+ return FileService.upload_files(request)
248
+
249
+ @app.post("/api/chat/chatbot/query/", dependencies=[Depends(JWTBearer())], tags=["Chatbot"])
250
+ async def override_handle_query2_upgrade_old(request: Request, user_id: str = Form(None), text_all: str = Form(...), question: str = Form(None), chat_name: str = Form(None), current_user_email: str = Depends(get_current_user_email)):
251
+ check = support_function.check_value_user_id(user_id, current_user_email)
252
+ if check is not True:
253
+ return check
254
+ request = RequestChat.RequestQuery2UpgradeOld(user_id=user_id, text_all=text_all, question=question, chat_name=chat_name)
255
+ return ChatService.query2_upgrade_old(request)
256
+
257
+ @app.get("/api/chat/chatbot/extract_file/", dependencies=[Depends(JWTBearer())], tags=["Chatbot"])
258
+ async def override_extract_file(user_id: str, current_user_email: str = Depends(get_current_user_email)):
259
+ check = support_function.check_value_user_id(user_id,current_user_email)
260
+ if check is not True:
261
+ return check
262
+ request = RequestChat.RequestExtractFile(user_id=user_id)
263
+ return ChatService.extract_file(request)
264
+
265
+ @app.get("/api/chat/chatbot/generate_question/", dependencies=[Depends(JWTBearer())], tags=["Chatbot"])
266
+ async def override_generate_question(user_id: str , current_user_email: str = Depends(get_current_user_email)):
267
+ check = support_function.check_value_user_id(user_id,current_user_email)
268
+ if check is not True:
269
+ return check
270
+ request = RequestChat.RequestGenerateQuestion(user_id=user_id)
271
+ return ChatService.generate_question(request)
272
+
273
+ @app.post("/api/default/upload_image/", dependencies=[Depends(JWTBearer())], tags=["Default"])
274
+ async def override_upload_image(user_id: str = Form(None), file: UploadFile = File(...),current_user_email: str = Depends(get_current_user_email)):
275
+ check = support_function.check_value_user_id(user_id,current_user_email)
276
+ if check is not True:
277
+ return check
278
+ request = RequestDefault.RequestUpLoadImage(user_id=user_id, files=file)
279
+ return DefaultService.upload_image_service(request)
280
+
281
+ app.include_router(MySQLController.router, prefix="/api/mysql")
282
+ app.include_router(LoginController.router, prefix="/api/users")
283
+ app.include_router(FileController.router, prefix="/api/file")
284
+ app.include_router(ChatController.router, prefix="/api/chat")
285
+ app.include_router(DefaultController.router, prefix="/api/default")
286
+
287
+ routes_to_override = {
288
+ "/api/mysql/render_chat_history": {"GET"},
289
+ "/api/mysql/load_chat_history": {"GET"},
290
+ "/api/mysql/edit_chat/": {"PUT"},
291
+ "/api/mysql/delete_chat/": {"DELETE"},
292
+ "/api/users/update_user_info": {"POST"},
293
+ "/api/users/change_password": {"PUT"},
294
+ "/api/file/delete_all_file/": {"DELETE"},
295
+ "/api/file/delete_one_file/": {"DELETE"},
296
+ "/api/file/chatbot/download_folder/": {"POST"},
297
+ "/api/file/chatbot/download_files/": {"POST"},
298
+ "/api/file/upload_files/": {"POST"},
299
+ "/api/chat/query2_upgrade/": {"POST"},
300
+ "/api/chat/chatbot/query/": {"POST"},
301
+ "/api/chat/chatbot/extract_file/": {"GET"},
302
+ "/api/chat/chatbot/generate_question/": {"GET"},
303
+ "/api/default/upload_image/": {"POST"},
304
+ "/api/default/info_user": {"GET"}
305
+
306
+ }
307
+
308
+ app.router.routes = [
309
+ route for route in app.router.routes
310
+ if not (
311
+ route.path in routes_to_override and
312
+ route.methods.intersection(routes_to_override[route.path])
313
+ )
314
+ ]
315
+
316
+ app.add_api_route("/api/mysql/render_chat_history", override_render_chat, methods=["GET"], dependencies=[Depends(JWTBearer())], tags=["MySQL"])
317
+ app.add_api_route("/api/mysql/load_chat_history/", override_load_chat, methods=["GET"], dependencies=[Depends(JWTBearer())], tags=["MySQL"])
318
+ app.add_api_route("/api/mysql/edit_chat/", override_edit_chat, methods=["PUT"], dependencies=[Depends(JWTBearer())], tags=["MySQL"])
319
+ app.add_api_route("/api/mysql/delete_chat/", override_delete_chat, methods=["DELETE"], dependencies=[Depends(JWTBearer())], tags=["MySQL"])
320
+ app.add_api_route("/api/users/update_user_info", override_update_user_info, methods=["POST"],dependencies=[Depends(JWTBearer())], tags=["Login"])
321
+ app.add_api_route("/api/users/change_password", override_reset_password_firebase, methods=["PUT"],dependencies=[Depends(JWTBearer())], tags=["Login"])
322
+ app.add_api_route("/api/file/delete_all_file/", override_delete_folder, methods=["DELETE"],dependencies=[Depends(JWTBearer())], tags=["File"])
323
+ app.add_api_route("/api/file/delete_one_file/", override_delete_one_file, methods=["DELETE"],dependencies=[Depends(JWTBearer())], tags=["File"])
324
+ app.add_api_route("/api/file/chatbot/download_folder/", override_download_folder_from_dropbox, methods=["POST"],dependencies=[Depends(JWTBearer())], tags=["File"])
325
+ app.add_api_route("/api/file/chatbot/download_files/", override_download_file_by_id, methods=["POST"],dependencies=[Depends(JWTBearer())], tags=["File"])
326
+ app.add_api_route("/api/file/upload_files/", override_upload_files_dropbox, methods=["POST"],dependencies=[Depends(JWTBearer())], tags=["File"])
327
+ app.add_api_route("/api/chat/chatbot/query/", override_handle_query2_upgrade_old, methods=["POST"],dependencies=[Depends(JWTBearer())], tags=["Chat"])
328
+ app.add_api_route("/api/chat/chatbot/extract_file/", override_extract_file, methods=["GET"], dependencies=[Depends(JWTBearer())], tags=["Chat"])
329
+ app.add_api_route("/api/chat/chatbot/generate_question/", override_generate_question,methods=["GET"], dependencies=[Depends(JWTBearer())], tags=["Chat"])
330
+ app.add_api_route("/api/default/upload_image/", override_upload_image, methods=["POST"], dependencies=[Depends(JWTBearer())], tags=["Default"])
331
+ app.add_api_route("/api/default/info_user",override_get_user, methods=["GET"], dependencies=[Depends(JWTBearer())], tags=["Default"])
 
 
 
 
332
  app.include_router(OTPController.router, tags = ["OTP"], prefix="/api/otp")