notify_upload_complete.py 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. from asyncio import Lock, to_thread
  2. from pathlib import Path
  3. from pydantic import BaseModel
  4. from fastapi import BackgroundTasks
  5. from tcvectordb.model.collection_view import SplitterProcess, ParsingProcess
  6. from settings import settings
  7. from main import app
  8. from services import cos, collection_view
  9. class FileNotify(BaseModel):
  10. key: str
  11. lock = Lock()
  12. async def upload_to_vdb(local_path: str):
  13. collection_view.load_and_split_text(
  14. local_file_path=local_path,
  15. metadata={},
  16. splitter_process=SplitterProcess(
  17. append_title_to_chunk=False,
  18. append_keywords_to_chunk=True,
  19. chunk_splitter=None,
  20. ),
  21. parsing_process=ParsingProcess(
  22. parsing_type='VisionModelParsing',
  23. ),
  24. )
  25. async def process_uploaded_file(notify: FileNotify):
  26. async with lock:
  27. if not await to_thread(cos.object_exists, settings.bucket, notify.key):
  28. return
  29. local_path = f'/tmp/{notify.key}'
  30. await to_thread(cos.download_file, settings.bucket, notify.key, local_path)
  31. await to_thread(upload_to_vdb, local_path)
  32. Path(local_path).unlink()
  33. @app.post('/notify-upload-complete')
  34. def notify_upload_complete(notify: FileNotify, background_tasks: BackgroundTasks):
  35. background_tasks.add_task(process_uploaded_file, notify)
  36. return