kltn20133118 commited on
Commit
a130eff
·
verified ·
1 Parent(s): c7f21bc

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +77 -82
main.py CHANGED
@@ -1,82 +1,77 @@
1
- from fastapi import FastAPI, HTTPException
2
- from fastapi.middleware.cors import CORSMiddleware
3
- from pydantic import BaseModel
4
- import pipe_line_obsei
5
-
6
- # Định nghĩa model request body
7
- class URLProcessRequest(BaseModel):
8
- target_url: str # URL cần phân tích
9
- primary_db: str # Tên database chính
10
- primary_collection: str # Tên collection chính
11
- backup_db: str # Tên database dự phòng
12
- backup_collection: str # Tên collection dự phòng
13
-
14
- # Khởi tạo FastAPI
15
- app = FastAPI(
16
- title="ChatBot HCMUTE",
17
- 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",
18
- swagger_ui_parameters={"syntaxHighlight.theme": "obsidian"},
19
- version="1.0.0",
20
- contact={
21
- "name": "Vo Nhu Y",
22
- "url": "https://pychatbot1.streamlit.app",
23
- "email": "vonhuy5112002@gmail.com",
24
- },
25
- license_info={
26
- "name": "Apache 2.0",
27
- "url": "https://www.apache.org/licenses/LICENSE-2.0.html",
28
- }
29
- )
30
-
31
- # Cấu hình CORS
32
- origins = [
33
- "http://localhost:8000",
34
- "https://yourfrontendapp.com", # Thêm domain của frontend nếu cần
35
- ]
36
-
37
- app.add_middleware(
38
- CORSMiddleware,
39
- allow_origins=origins,
40
- allow_credentials=True,
41
- allow_methods=["*"],
42
- allow_headers=["*"],
43
- )
44
-
45
- @app.get("/")
46
- async def root():
47
- return {"message": "Welcome to ChatBot HCMUTE API!"}
48
-
49
- @app.post("/api/v1/obsei/process_url/")
50
- async def process_url_api(request: URLProcessRequest):
51
- """
52
- API nhận request body chứa thông tin URL và các thông tin database cần thiết,
53
- sau đó xử lý URL, phân tích và lưu dữ liệu vào MongoDB.
54
- """
55
- try:
56
- # Lấy dữ liệu từ request body
57
- target_url = request.target_url
58
- primary_db = request.primary_db
59
- primary_collection = request.primary_collection
60
- backup_db = request.backup_db
61
- backup_collection = request.backup_collection
62
-
63
- # Gọi hàm `process_url` đã định nghĩa
64
- processed_text, content_data = await pipe_line_obsei.process_url(
65
- target_url, primary_db, primary_collection, backup_db, backup_collection
66
- )
67
-
68
- return {
69
- "processed_text": processed_text,
70
- "content_data": content_data,
71
- }
72
- except Exception as e:
73
- raise HTTPException(
74
- status_code=500,
75
- detail=f"An error occurred while processing the request: {str(e)}"
76
- )
77
-
78
-
79
- # Chạy FastAPI server
80
- if __name__ == "__main__":
81
- import uvicorn
82
- uvicorn.run(app, port=8005)
 
1
+ from fastapi import FastAPI, HTTPException
2
+ from fastapi.middleware.cors import CORSMiddleware
3
+ from pydantic import BaseModel
4
+ import pipe_line_obsei
5
+
6
+ # Định nghĩa model request body
7
+ class URLProcessRequest(BaseModel):
8
+ target_url: str # URL cần phân tích
9
+ primary_db: str # Tên database chính
10
+ primary_collection: str # Tên collection chính
11
+ backup_db: str # Tên database dự phòng
12
+ backup_collection: str # Tên collection dự phòng
13
+
14
+ # Khởi tạo FastAPI
15
+ app = FastAPI(
16
+ title="ChatBot HCMUTE",
17
+ 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",
18
+ swagger_ui_parameters={"syntaxHighlight.theme": "obsidian"},
19
+ version="1.0.0",
20
+ contact={
21
+ "name": "Vo Nhu Y",
22
+ "url": "https://pychatbot1.streamlit.app",
23
+ "email": "vonhuy5112002@gmail.com",
24
+ },
25
+ license_info={
26
+ "name": "Apache 2.0",
27
+ "url": "https://www.apache.org/licenses/LICENSE-2.0.html",
28
+ }
29
+ )
30
+
31
+ # Cấu hình CORS
32
+ origins = [
33
+ "http://localhost:8000",
34
+ "https://yourfrontendapp.com", # Thêm domain của frontend nếu cần
35
+ ]
36
+
37
+ app.add_middleware(
38
+ CORSMiddleware,
39
+ allow_origins=origins,
40
+ allow_credentials=True,
41
+ allow_methods=["*"],
42
+ allow_headers=["*"],
43
+ )
44
+
45
+ @app.get("/")
46
+ async def root():
47
+ return {"message": "Welcome to ChatBot HCMUTE API!"}
48
+
49
+ @app.post("/api/v1/obsei/process_url/")
50
+ async def process_url_api(request: URLProcessRequest):
51
+ """
52
+ API nhận request body chứa thông tin URL và các thông tin database cần thiết,
53
+ sau đó xử lý URL, phân tích và lưu dữ liệu vào MongoDB.
54
+ """
55
+ try:
56
+ # Lấy dữ liệu từ request body
57
+ target_url = request.target_url
58
+ primary_db = request.primary_db
59
+ primary_collection = request.primary_collection
60
+ backup_db = request.backup_db
61
+ backup_collection = request.backup_collection
62
+
63
+ # Gọi hàm `process_url` đã định nghĩa
64
+ processed_text, content_data = await pipe_line_obsei.process_url(
65
+ target_url, primary_db, primary_collection, backup_db, backup_collection
66
+ )
67
+
68
+ return {
69
+ "processed_text": processed_text,
70
+ "content_data": content_data,
71
+ }
72
+ except Exception as e:
73
+ raise HTTPException(
74
+ status_code=500,
75
+ detail=f"An error occurred while processing the request: {str(e)}"
76
+ )
77
+