# Import FastAPI Libs from fastapi import FastAPI, Query from pydantic import BaseModel import qcloud_cos_service import qcloud_ocr_service import tongyi_genai_service from fastapi.responses import StreamingResponse from fastapi.middleware.cors import CORSMiddleware from typing import Annotated from fastapi import Depends from fastapi.security import OAuth2PasswordBearer, OAuth2PasswordRequestForm import fastapi_security_util from fastapi_security_util import Token # ------------------------------------------------ # Init Global Variables # ------------------------------------------------ # Init Oauth2 schema, specify token endpoint oauth2_scheme = OAuth2PasswordBearer(tokenUrl="token") # ------------------------------------------------ # Define FastAPI # ------------------------------------------------ app = FastAPI() # ------------------------------------------------ # Call Token Service # ------------------------------------------------ @app.post("/token") def get_access_token(form_data: Annotated[OAuth2PasswordRequestForm, Depends()]) -> Token: return fastapi_security_util.get_access_token(form_data) # ------------------------------------------------ # Call Token Service without Credential # ------------------------------------------------ @app.post("/token_without_credential") def get_access_token_without_credential() -> Token: return fastapi_security_util.get_access_token_without_credential() # ------------------------------------------------ # Define CORS Options # ------------------------------------------------ # origins = [ # "http://localhost", # "http://localhost:8000", # "https://yourdomain.com", # # Add more origins if needed # ] app.add_middleware( CORSMiddleware, allow_origins=["*"], # List of allowed origins allow_credentials=True, # Whether to allow credentials (cookies, authorization headers, etc.) allow_methods=["*"], # List of allowed methods (GET, POST, PUT, DELETE, etc.) allow_headers=["*"], # List of allowed headers ) # ------------------------------------------------ # Call File Upload Service Process # ------------------------------------------------ @app.post("/upload/") async def upload(file_uplaod_req: qcloud_cos_service.file_upload_req_basemodel): return qcloud_cos_service.upload(file_uplaod_req) # ------------------------------------------------ # Call File Download URL Service Process # ------------------------------------------------ @app.post("/get_download_url/") async def get_download_url(file_download_req: qcloud_cos_service.file_download_req_basemodel): return qcloud_cos_service.get_download_url(file_download_req) # ------------------------------------------------ # Call OCR Service Process # ------------------------------------------------ @app.post("/get_detected_text/") async def get_detected_text(ocr_req: qcloud_ocr_service.ocr_req_basemodel): if ocr_req.tool == 'Q': # Call Qcloud API return qcloud_ocr_service.get_qcloud_detected_text(ocr_req) elif ocr_req.tool == 'LP': # Call Local PaddleOCR API return qcloud_ocr_service.get_paddleocr_detected_text(ocr_req) # ------------------------------------------------ # Call Gen AI Service Process # ------------------------------------------------ @app.post("/send_message_to_gen_ai/tongyi/") async def send_message(send_message_req: tongyi_genai_service.send_message_req_basemodel): return tongyi_genai_service.send_message(send_message_req) # ------------------------------------------------ # Call Gen AI Service Process (send question) # ------------------------------------------------ @app.post("/send_message_to_gen_ai_async/tongyi/send_question") async def send_question( token: Annotated[str, Depends(oauth2_scheme)], send_message_req: tongyi_genai_service.send_message_req_basemodel): print('token', token) return tongyi_genai_service.send_question(token, send_message_req) # ------------------------------------------------ # Call Gen AI Service Process (get answer) # ------------------------------------------------ @app.get("/send_message_to_gen_ai_async/tongyi/get_answer") async def get_answer( uuid: str = Query(None, title="UUID", description="UUID linked to request payload")): return StreamingResponse(tongyi_genai_service.get_answer(uuid), media_type="text/event-stream")