snowby666 / poe-api-wrapper

👾 A Python API wrapper for Poe.com. With this, you will have free access to GPT-4, Claude, Llama, Gemini, Mistral and more! 🚀
https://pypi.org/project/poe-api-wrapper/
GNU General Public License v3.0
959 stars 114 forks source link

Can not upload file after update newest code #80

Closed phamxtien closed 1 year ago

phamxtien commented 1 year ago

Env: Python 3.11.4 - Ubuntu

It shows:

File....poe_api_wrapper/api.py", line 704, in send_message message_data = self.send_request(apiPath, 'SendMessageMutation', variables, file_form) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File ..../poe_api_wrapper/api.py", line 176, in send_request payload = MultipartEncoder( ^^^^^^^^^^^^^^^^^ File ....../requests_toolbelt/multipart/encoder.py", line 125, in init self._prepare_parts() File ...../requests_toolbelt/multipart/encoder.py", line 246, in _prepare_parts self.parts = [Part.from_field(f, enc) for f in self._iter_fields()] ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File ...../requests_toolbelt/multipart/encoder.py", line 246, in self.parts = [Part.from_field(f, enc) for f in self._iter_fields()] ^^^^^^^^^^^^^^^^^^^^^^^ File ...../requests_toolbelt/multipart/encoder.py", line 495, in from_field return cls(headers, body) ^^^^^^^^^^^^^^^^^^ File ...../requests_toolbelt/multipart/encoder.py", line 488, in init self.len = len(self.headers) + total_len(self.body) ^^^^^^^^^^^^^^^^^^^^ File ...../requests_toolbelt/multipart/encoder.py", line 432, in total_len if hasattr(o, 'len'): ^^^^^^^^^^^^^^^^^ File ...../requests_toolbelt/multipart/encoder.py", line 573, in len return total_len(self.fd) - self.fd.tell() ^^^^^^^^^^^^^^^^^^ File ...../requests_toolbelt/multipart/encoder.py", line 437, in total_len fileno = o.fileno() ^^^^^^^^^^ ValueError: I/O operation on closed file

Please help: How to fix it

snowby666 commented 1 year ago

can you specify what files you uploaded? and the code you used here?

phamxtien commented 1 year ago

File upload: info.txt Code:

import getDuckDuckRegion
from poe_api_wrapper import PoeApi

m_b = '....'

def search(engine, prompt, id=''):
    region = getDuckDuckRegion(prompt)
    if (prompt.strip()).startswith('#search:'):
        keywords = [prompt.replace('#search:', '')]
        act = keywords
    else:
        prompt = prompt.split('#search:')
        keywords = prompt[1].strip().split('\n')
        act = prompt[0]
    count = 15
    if len(keywords) > 1: count = 5
    ddg = ddgSearch(keywords=keywords, region=region, count=count)
    i = 1
    links = ''
    for url in ddg['urls']:
        links = links + f'[{i}] {url}\n'
        i = i + 1
    documents = ' '.join(ddg['documents'])

    temp_path = f'{getConfPath()}{os.sep}info.txt'
    if os.path.exists(temp_path): os.remove(temp_path)
    prompt = f'{prompt}#file::{temp_path}'

    return poeChat(prompt, engine, id)

def poeChat(prompt, engine='', id=''):    
    file_path = []
    if '#file::' in prompt:
        file_path = prompt.split('#file::')[1].strip()
        file_path = file_path.split('\n')
        prompt = prompt.split('#file::')[0]

    client = PoeApi(m_b)
    if id !='':
        id = id.split(' ')
        response = client.send_message('gpt3_5', prompt, chatCode=id[0], chatId=int(id[1]), file_path=file_path)
    else:
        response = client.send_message('gpt3_5', prompt, file_path=file_path)
    for chunk in response:
        pass

    chatCode = chunk["chatCode"]
    chatId = chunk["chatId"]

    return {'RESULT': True, 'ID': f'{chatCode} {chatId}', 'DATA': chunk['text']}

It run smooth if not attach file

jnikhilreddy commented 1 year ago

Yeah i am facing the same error too when i try to upload file

snowby666 commented 1 year ago

I've just resolved this. You can update the lib now.