MervinPraison / PraisonAI

PraisonAI application combines AutoGen and CrewAI or similar frameworks into a low-code solution for building and managing multi-agent LLM systems, focusing on simplicity, customisation, and efficient human-agent collaboration.
https://docs.praison.ai
MIT License
1.26k stars 188 forks source link

UnicodeDecodeError: 'cp950' codec can't decode byte 0xe2 in position 2072: illegal multibyte sequence #8

Open Adamchanadam opened 3 months ago

Adamchanadam commented 3 months ago

Environment : Windows 11 (Conda) with Python 3.11.8 praisonAI 0.0.17

After installation ( : pip install praisonai

I ran the Initialise : praisonai --init create a movie script about dog in moon

it return error code as below , I think it's UTF-8 issue :

Traceback (most recent call last):
  File "<frozen runpy>", line 198, in _run_module_as_main
  File "<frozen runpy>", line 88, in _run_code
  File "C:\Users\adam\anaconda3\envs\adamlab2_env\Scripts\praisonai.exe\__main__.py", line 4, in <module>
  File "C:\Users\adam\anaconda3\envs\adamlab2_env\Lib\site-packages\praisonai\__init__.py", line 1, in <module>
    from .cli import PraisonAI
  File "C:\Users\adam\anaconda3\envs\adamlab2_env\Lib\site-packages\praisonai\cli.py", line 11, in <module>
    import gradio as gr
  File "C:\Users\adam\anaconda3\envs\adamlab2_env\Lib\site-packages\gradio\__init__.py", line 3, in <module>
    import gradio._simple_templates
  File "C:\Users\adam\anaconda3\envs\adamlab2_env\Lib\site-packages\gradio\_simple_templates\__init__.py", line 1, in <module>
    from .simpledropdown import SimpleDropdown
  File "C:\Users\adam\anaconda3\envs\adamlab2_env\Lib\site-packages\gradio\_simple_templates\simpledropdown.py", line 6, in <module>
    from gradio.components.base import FormComponent
  File "C:\Users\adam\anaconda3\envs\adamlab2_env\Lib\site-packages\gradio\components\__init__.py", line 40, in <module>
    from gradio.components.multimodal_textbox import MultimodalTextbox
  File "C:\Users\adam\anaconda3\envs\adamlab2_env\Lib\site-packages\gradio\components\multimodal_textbox.py", line 34, in <module>
    class MultimodalTextbox(FormComponent):
  File "C:\Users\adam\anaconda3\envs\adamlab2_env\Lib\site-packages\gradio\component_meta.py", line 198, in __new__
    create_or_modify_pyi(component_class, name, events)
  File "C:\Users\adam\anaconda3\envs\adamlab2_env\Lib\site-packages\gradio\component_meta.py", line 92, in create_or_modify_pyi
    source_code = source_file.read_text()
                  ^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\adam\anaconda3\envs\adamlab2_env\Lib\pathlib.py", line 1059, in read_text
    return f.read()
           ^^^^^^^^
UnicodeDecodeError: 'cp950' codec can't decode byte 0xe2 in position 2072: illegal multibyte sequence
Adamchanadam commented 3 months ago

The problem is solved by adding the encoding="utf-8" into the create_or_modify_pyi() function in .\gradio\component_meta.py.

Four modifications are needed:

1 x Change source_file.read_text() to source_file.read_text(encoding="utf-8") once. 3 x Change pyi_file.read_text() to pyi_file.read_text(encoding='utf-8') three times.

def create_or_modify_pyi(
    component_class: type, class_name: str, events: list[str | EventListener]
):
    source_file = Path(inspect.getfile(component_class))

    source_code = source_file.read_text(encoding="utf-8") # <---- add utf-8 here

    current_impl, lineno = extract_class_source_code(source_code, class_name)

    if not (current_impl and lineno):
        raise ValueError("Couldn't find class source code")

    new_interface = create_pyi(current_impl, events)

    pyi_file = source_file.with_suffix(".pyi")
    if not pyi_file.exists():
        last_empty_line_before_class = -1
        lines = source_code.splitlines()
        for i, line in enumerate(lines):
            if line in ["", " "]:
                last_empty_line_before_class = i
            if i >= lineno:
                break
        lines = (
            lines[:last_empty_line_before_class]
            + ["from gradio.events import Dependency"]
            + lines[last_empty_line_before_class:]
        )
        with no_raise_exception():
            pyi_file.write_text("\n".join(lines))
    current_interface, _ = extract_class_source_code(pyi_file.read_text(encoding='utf-8'), class_name) # <---- add utf-8 here
    if not current_interface:
        with no_raise_exception():
            with open(str(pyi_file), mode="a") as f:
                f.write(new_interface)
    else:
        contents = pyi_file.read_text(encoding='utf-8') # <---- add utf-8 here
        contents = contents.replace(current_interface, new_interface.strip())
        current_contents = pyi_file.read_text(encoding='utf-8') # <---- add utf-8 here
        if current_contents != contents:
            with no_raise_exception():
                pyi_file.write_text(contents)