Chainlit / chainlit

Build Conversational AI in minutes ⚡️
https://docs.chainlit.io
Apache License 2.0
6.76k stars 878 forks source link

TypeError: argument of type 'AskFileResponse' is not iterable #311

Closed poojitharamachandra closed 1 year ago

poojitharamachandra commented 1 year ago

I am trying to upload a pdf file , but unable to read it.(the same code runs without any error if i upload a text file) here is the code:

# Wait for the user to upload a file
while files == None:
    files = await cl.AskFileMessage(
        content="Please upload a text file to begin!", accept=["application/pdf"],max_size_mb=100
    ).send()

file = files[0]

TypeError: argument of type 'AskFileResponse' is not iterable

willydouhard commented 1 year ago

What version of chainlit are you using? You can run pip show chainlit to find out

poojitharamachandra commented 1 year ago

chainlit==0.4.101

willydouhard commented 1 year ago

Yes this is an old version. Try run pip install -U chainlit to upgrade

poojitharamachandra commented 1 year ago

even with 0.6.3, the same issue exists.

willydouhard commented 1 year ago

Are you sure you are running the correct version of chainlit ? You might have multiple python version installed. You can try to start chainlit like that to make sure:

python -m chainlit run …

poojitharamachandra commented 1 year ago

is this implemented in 0.6.3? Upload File button in front-end UI, just like OpenAI code interpretor · Issue #198 · Chainlit/chainlit (github.com)

poojitharamachandra commented 1 year ago

I am able to read .txt file in 0.6.3(TypeError: argument of type 'AskFileResponse' is not iterable is not there anymore), but unable to read a pdf file. How do I correctly read a pdf file here?

willydouhard commented 1 year ago

Here is an example with langchain https://github.com/Chainlit/cookbook/blob/c65d989e90614da867fed2a4e06ad9330d95ff06/pdf-qa/app.py#L75 check the process_file function

poojitharamachandra commented 1 year ago

thanks, i am still unable to process the content of pdf, as it contains special characters. none of the decoding techniques such as utf-8, latin-1,unicode etc are working

willydouhard commented 1 year ago

There are multiple python pdf loader packages available. You can probably find one that fit your need :)

poojitharamachandra commented 1 year ago

is there any way to give the loader as input to AskFileMessage? Because, my default, AskFileMessage give data in form of bytes, which is causing problem for my usecase, as I have multiple special characters in the pdf.

poojitharamachandra commented 1 year ago

hi,

is this implemented in 0.6.3? https://github.com/Chainlit/chainlit/issues/198

willydouhard commented 1 year ago

I don't think it is the responsibility of AskFileMessage to parse the file. The developer should be able to use any loader from the received bytes.

willydouhard commented 1 year ago

hi,

is this implemented in 0.6.3? #198

This will be part of the next release.

poojitharamachandra commented 1 year ago

Here is an example with langchain https://github.com/Chainlit/cookbook/blob/c65d989e90614da867fed2a4e06ad9330d95ff06/pdf-qa/app.py#L75 check the process_file function

is it possible to write the contents to a pdf/word file instead of a temp file?

willydouhard commented 1 year ago

To update an existing file or to create it from scratch? If you just want to create the file you can do

with open('output.pdf', 'wb') as f:
    # Write the bytes to the file
    f.write(file.content)
AbhijitManepatil commented 8 months ago

chainlit==0.7.400 version works for me

willydouhard commented 8 months ago

Since chainlit 1.0.0 this api changed slightly. Check this example