xtekky / gpt4free

The official gpt4free repository | various collection of powerful language models
https://g4f.ai
GNU General Public License v3.0
62.22k stars 13.39k forks source link

UnboundLocalError: cannot access local variable 'logs' where it is not associated with a value #2383

Closed poggersbutnot closed 6 days ago

poggersbutnot commented 6 days ago

On the GUI (Web UI), this error occurs (UnboundLocalError), due to this extract from the code in the file gui/server/api.py

def _create_response_stream(self, kwargs: dict, conversation_id: str, provider: str) -> Iterator:
        if debug.logging:
            logs = []
            print_callback = debug.log_handler
            def log_handler(text: str):
                logs.append(text)
                print_callback(text)
            debug.log_handler = log_handler
        try:
            result = ChatCompletion.create(**kwargs)
            first = True
            if isinstance(result, ImageResponse):
                if first:
                    first = False
                    yield self._format_json("provider", get_last_provider(True))
                yield self._format_json("content", str(result))
            else:
                for chunk in result:
                    if first:
                        first = False
                        yield self._format_json("provider", get_last_provider(True))
                    if isinstance(chunk, BaseConversation):
                        if provider not in conversations:
                            conversations[provider] = {}
                        conversations[provider][conversation_id] = chunk
                        yield self._format_json("conversation", conversation_id)
                    elif isinstance(chunk, Exception):
                        logger.exception(chunk)
                        yield self._format_json("message", get_error_message(chunk))
                    elif isinstance(chunk, ImagePreview):
                        yield self._format_json("preview", chunk.to_string())
                    elif isinstance(chunk, ImageResponse):
                        images = asyncio.run(self._copy_images(chunk.get_list(), chunk.options.get("cookies")))
                        yield self._format_json("content", str(ImageResponse(images, chunk.alt)))
                    elif not isinstance(chunk, FinishReason):
                        yield self._format_json("content", str(chunk))
                    if logs:
                        for log in logs:
                            yield self._format_json("log", str(log))
                        logs = []
        except Exception as e:
            logger.exception(e)
            yield self._format_json('error', get_error_message(e))

To fix this, simply change

if logs:
    for log in logs:
        yield self._format_json("log", str(log))
    logs = []

to

if debug.logging and logs:
    for log in logs:
        yield self._format_json("log", str(log))
    logs = []

Or just define logs = [] for users who don't have debug.logging enabled as well, but imo that'd be unnecessary.

Bug description Throws a UnboundLocalError due to the variable 'logs' not existing for users who don't have debug.logging enabled.

ex. Type a message in the Web UI and send it.

Environment Not necessary.

Just want to also say that this module is awesome. Keep it up!

hlohaus commented 6 days ago

I already fixed it, no worries.

poggersbutnot commented 6 days ago

Alright, thanks!