File size: 2,525 Bytes
a130eff
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
from fastapi import FastAPI, HTTPException
from fastapi.middleware.cors import CORSMiddleware
from pydantic import BaseModel
import pipe_line_obsei

# Định nghĩa model request body
class URLProcessRequest(BaseModel):
    target_url: str  # URL cần phân tích
    primary_db: str  # Tên database chính
    primary_collection: str  # Tên collection chính
    backup_db: str  # Tên database dự phòng
    backup_collection: str  # Tên collection dự phòng

# Khởi tạo FastAPI
app = FastAPI(
    title="ChatBot HCMUTE",
    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",
    swagger_ui_parameters={"syntaxHighlight.theme": "obsidian"},
    version="1.0.0",
    contact={
        "name": "Vo Nhu Y",
        "url": "https://pychatbot1.streamlit.app",
        "email": "vonhuy5112002@gmail.com",
    },
    license_info={
        "name": "Apache 2.0",
        "url": "https://www.apache.org/licenses/LICENSE-2.0.html",
    }
)

# Cấu hình CORS
origins = [
    "http://localhost:8000",
    "https://yourfrontendapp.com",  # Thêm domain của frontend nếu cần
]

app.add_middleware(
    CORSMiddleware,
    allow_origins=origins,
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)

@app.get("/")
async def root():
    return {"message": "Welcome to ChatBot HCMUTE API!"}

@app.post("/api/v1/obsei/process_url/")
async def process_url_api(request: URLProcessRequest):
    """
    API nhận request body chứa thông tin URL và các thông tin database cần thiết,
    sau đó xử lý URL, phân tích và lưu dữ liệu vào MongoDB.
    """
    try:
        # Lấy dữ liệu từ request body
        target_url = request.target_url
        primary_db = request.primary_db
        primary_collection = request.primary_collection
        backup_db = request.backup_db
        backup_collection = request.backup_collection

        # Gọi hàm `process_url` đã định nghĩa
        processed_text, content_data = await pipe_line_obsei.process_url(
            target_url, primary_db, primary_collection, backup_db, backup_collection
        )

        return {
            "processed_text": processed_text,
            "content_data": content_data,
        }
    except Exception as e:
        raise HTTPException(
            status_code=500,
            detail=f"An error occurred while processing the request: {str(e)}"
        )