2021-04-07 17:53:32 +02:00
|
|
|
import json
|
2020-06-17 16:28:26 +02:00
|
|
|
import logging
|
2020-06-24 15:05:30 +02:00
|
|
|
import os
|
2020-06-17 16:28:26 +02:00
|
|
|
import shutil
|
|
|
|
import uuid
|
|
|
|
from pathlib import Path
|
|
|
|
from typing import Optional, List
|
|
|
|
|
2021-04-07 17:53:32 +02:00
|
|
|
from fastapi import APIRouter, UploadFile, File, Form, HTTPException
|
2020-06-17 16:28:26 +02:00
|
|
|
|
2021-04-07 17:53:32 +02:00
|
|
|
from haystack.pipeline import Pipeline
|
|
|
|
from rest_api.config import PIPELINE_YAML_PATH, FILE_UPLOAD_PATH, INDEXING_PIPELINE_NAME
|
2020-06-17 16:28:26 +02:00
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
router = APIRouter()
|
|
|
|
|
2021-04-07 17:53:32 +02:00
|
|
|
try:
|
|
|
|
INDEXING_PIPELINE = Pipeline.load_from_yaml(Path(PIPELINE_YAML_PATH), pipeline_name=INDEXING_PIPELINE_NAME)
|
|
|
|
except KeyError:
|
|
|
|
INDEXING_PIPELINE = None
|
|
|
|
logger.info("Indexing Pipeline not found in the YAML configuration. File Upload API will not be available.")
|
2020-06-17 16:28:26 +02:00
|
|
|
|
|
|
|
|
2020-06-24 15:05:30 +02:00
|
|
|
os.makedirs(FILE_UPLOAD_PATH, exist_ok=True) # create directory for uploading files
|
|
|
|
|
2020-06-17 16:28:26 +02:00
|
|
|
|
|
|
|
@router.post("/file-upload")
|
2021-04-07 17:53:32 +02:00
|
|
|
def file_upload(
|
2020-06-17 16:28:26 +02:00
|
|
|
file: UploadFile = File(...),
|
2021-04-07 17:53:32 +02:00
|
|
|
meta: Optional[str] = Form("null"), # JSON serialized string
|
|
|
|
remove_numeric_tables: Optional[bool] = Form(None),
|
|
|
|
remove_whitespace: Optional[bool] = Form(None),
|
|
|
|
remove_empty_lines: Optional[bool] = Form(None),
|
|
|
|
remove_header_footer: Optional[bool] = Form(None),
|
|
|
|
valid_languages: Optional[List[str]] = Form(None),
|
|
|
|
split_by: Optional[str] = Form(None),
|
|
|
|
split_length: Optional[int] = Form(None),
|
|
|
|
split_overlap: Optional[int] = Form(None),
|
|
|
|
split_respect_sentence_boundary: Optional[bool] = Form(None),
|
2020-07-06 17:35:47 +02:00
|
|
|
):
|
2021-04-07 17:53:32 +02:00
|
|
|
if not INDEXING_PIPELINE:
|
|
|
|
raise HTTPException(status_code=501, detail="Indexing Pipeline is not configured.")
|
2020-06-17 16:28:26 +02:00
|
|
|
try:
|
|
|
|
file_path = Path(FILE_UPLOAD_PATH) / f"{uuid.uuid4().hex}_{file.filename}"
|
|
|
|
with file_path.open("wb") as buffer:
|
|
|
|
shutil.copyfileobj(file.file, buffer)
|
2021-04-30 14:16:30 +05:30
|
|
|
|
|
|
|
meta = json.loads(meta) or {}
|
|
|
|
meta["name"] = file.filename
|
|
|
|
|
2021-04-07 17:53:32 +02:00
|
|
|
INDEXING_PIPELINE.run(
|
|
|
|
file_path=file_path,
|
|
|
|
remove_numeric_tables=remove_numeric_tables,
|
|
|
|
remove_whitespace=remove_whitespace,
|
|
|
|
remove_empty_lines=remove_empty_lines,
|
|
|
|
remove_header_footer=remove_header_footer,
|
|
|
|
valid_languages=valid_languages,
|
2021-02-05 12:17:38 +01:00
|
|
|
split_by=split_by,
|
|
|
|
split_length=split_length,
|
|
|
|
split_overlap=split_overlap,
|
|
|
|
split_respect_sentence_boundary=split_respect_sentence_boundary,
|
2021-04-30 14:16:30 +05:30
|
|
|
meta=meta,
|
2021-02-05 12:17:38 +01:00
|
|
|
)
|
2020-06-17 16:28:26 +02:00
|
|
|
finally:
|
|
|
|
file.file.close()
|