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)}" ) # Chạy FastAPI server if __name__ == "__main__": import uvicorn uvicorn.run(app, port=8005)