melih-unsal/DemoGPT

UnicodeEncodeError when writing to temporary file on Windows with Python 3.10 or 3.11

juanma1331 opened this issue · 6 comments

Describe the bug
When running DemoGPT on a Windows machine with a Python version 3.11, a UnicodeEncodeError is thrown. The error occurs when the application attempts to write to a temporary file using the tmp.write(code) function in run_code and run_python methods. The problem seems to stem from the code containing Unicode characters which are not supported by the default encoding of the temporary file.

To Reproduce
Steps to reproduce the behavior:
Install Python 3.10 or 3.11 (tried with both) on a Windows 10 machine.
Clone the DemoGPT repository.
Create a virtual environment and install the required libraries as indicated in the project README.
Execute the DemoGPT application with streamlit run src/prompt_based/app.py.
See error.

Expected behavior
The DemoGPT application should be able to handle Unicode characters in the code without throwing a UnicodeEncodeError.

Screenshots
The trace appeared on the app after failing, but could not make it appear again.

Hey @juanma1331 ,

Thank you for your detailed report and for bringing this issue to our attention. We sincerely apologize for any inconvenience caused. It appears that the issue was related to the lack of handling Unicode during temporary file writing in the Windows operating system, as you rightly pointed out.

I'm glad to inform you that we've pushed a new update to the repository that should resolve the issue. You should now be able to run the Streamlit app without encountering this problem.

Please update your local copy of the repository and retry running the Streamlit app. If you encounter any further issues, don't hesitate to report them. Your feedback is highly appreciated and helps us improve the overall user experience.

Thank you for your support and contribution to DemoGPT. 🙏

Best,
Melih

I cloned again and tried the example but it keeps erroring. return codecs.charmap_encode(input,self.errors,encoding_table)[0]. I checked committed changes and it should work with the new changes, but dont know why it's still there.

Hey @juanma1331 ,thank you again for reporting. 🙏
I'm also surprised but I worked on it and it should be okay now.
I hope this new update resolves the issue.

Best,
Melih

Ok I made it work with this changes.

if "import sys" not in code:
        code = "import sys\nsys.stdout.reconfigure(encoding='utf-8')\n" + code

        tmp = tempfile.NamedTemporaryFile(
            "w", suffix=".py", delete=False, encoding="utf-8"
        )```
.Thanks will check!!

I liked the way you used!

I hope the new update also solves the issue, otherwise, you can also make a PR.

It didn't work, unfortunately. I'll try the PR when I have some free time (I have kids, so it can get a little hectic, hehe). Although it will be my first PR, so it's going to be a learning experience!