[Python] How to read a file into memory in FastAPI and pass it to MarkItDown library?

Discussão em 'Python' iniciado por Stack, Janeiro 25, 2025.

  1. Stack

    Stack Membro Participativo

    The need is to upload a file to a FastAPI endpoint, convert it to Markdown and save the text to Redis (Files are up to 4mb in size).

    The only logic I have found so far is to upload the file as UploadFile, read the contents, save them to disk with the right extension, pass that path to the https://github.com/microsoft/markitdown library, Read that markdown file again, and then pass it to Redis. Way too much I/O. Is there a way to do all of this in Memory?

    (For sake of code simplicity I removed all error handling and I assume only text files)

    @router.post("/upload")
    async def uploadPost(filepond: UploadFile = File()):
    """
    Convert a textual file to markdown.
    Store in Redis
    """
    # Create a temporary file to save the uploaded content
    # for sake of simplicity I use txt for everything
    with NamedTemporaryFile(delete=False,suffix=".txt") as temp_file:
    temp_file_path = temp_file.name
    content = await filepond.read()
    temp_file.write(content)
    temp_file.close()

    md = MarkItDown()
    result = md.convert(temp_file_path)
    redis.setex("some key", 3600, result.text_content)

    os.remove(temp_file_path)

    Continue reading...

Compartilhe esta Página