source_code = source_file.read_text()
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(), class_name)
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()
contents = contents.replace(current_interface, new_interface.strip())
current_contents = pyi_file.read_text()
if current_contents != contents:
with no_raise_exception():
pyi_file.write_text(contents)
修复后的代码
python
复制代码
from pathlib import Path
import inspect
# 修改:显式使用 UTF-8 编码读取文件
source_code = source_file.read_text(encoding="utf-8")
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():
# 修改:显式使用 UTF-8 编码写入文件
pyi_file.write_text("\n".join(lines), encoding="utf-8")
current_interface, _ = extract_class_source_code(pyi_file.read_text(encoding="utf-8"), class_name)
if not current_interface:
with no_raise_exception():
# 修改:显式使用 UTF-8 编码追加写入文件
with open(str(pyi_file), mode="a", encoding="utf-8") as f:
f.write(new_interface)
else:
# 修改:显式使用 UTF-8 编码读取文件
contents = pyi_file.read_text(encoding="utf-8")
contents = contents.replace(current_interface, new_interface.strip())
# 修改:显式使用 UTF-8 编码再次读取文件
current_contents = pyi_file.read_text(encoding="utf-8")
if current_contents != contents:
with no_raise_exception():
# 修改:显式使用 UTF-8 编码写入文件
pyi_file.write_text(contents, encoding="utf-8")
提交信息示例
你可以将以下信息提交给 GitHub 上的作者:
问题描述:
在尝试运行 D:\fastsdcpu-main\src\app.py 时遇到以下错误:
arduino
复制代码
Traceback (most recent call last):
File "D:\fastsdcpu-main\src\app.py", line 10, in
from frontend.webui.image_variations_ui import generate_image_variations
File "D:\fastsdcpu-main\src\frontend\webui\image_variations_ui.py", line 2, in
import gradio as gr
File "D:\fastsdcpu-main\env\Lib\site-packages\gradio__init.py", line 3, in
import gradio._simple_templates
File "D:\fastsdcpu-main\env\Lib\site-packages\gradio_simple_templates__init.py", line 1, in
from .simpledropdown import SimpleDropdown
File "D:\fastsdcpu-main\env\Lib\site-packages\gradio_simple_templates\simpledropdown.py", line 6, in
from gradio.components.base import FormComponent
File "D:\fastsdcpu-main\env\Lib\site-packages\gradio\components\init__.py", line 40, in
from gradio.components.multimodal_textbox import MultimodalTextbox
File "D:\fastsdcpu-main\env\Lib\site-packages\gradio\components\multimodal_textbox.py", line 34, in
class MultimodalTextbox(FormComponent):
File "D:\fastsdcpu-main\env\Lib\site-packages\gradio\component_meta.py", line 198, in new__
create_or_modify_pyi(component_class, name, events)
File "D:\fastsdcpu-main\env\Lib\site-packages\gradio\component_meta.py", line 92, in create_or_modify_pyi
source_code = source_file.read_text()
^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python311\Lib\pathlib.py", line 1059, in read_text
return f.read()
^^^^^^^^
UnicodeDecodeError: 'gbk' codec can't decode byte 0xb2 in position 2074: illegal multibyte sequence
问题解决:
source_code = source_file.read_text()
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(), class_name)
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()
contents = contents.replace(current_interface, new_interface.strip())
current_contents = pyi_file.read_text()
if current_contents != contents:
with no_raise_exception():
pyi_file.write_text(contents)
修复后的代码:
python
复制代码
from pathlib import Path
import inspect
当然,以下是你可以用来向 GitHub 作者提交问题的详细信息,包括原代码、修复后的代码、出现的文件和对应行数。
文件和对应行数 文件路径:D:\fastsdcpu-main\env\Lib\site-packages\gradio\component_meta.py 函数名:create_or_modify_pyi 问题所在行数:92行
原代码 python 复制代码 def create_or_modify_pyi( component_class: type, class_name: str, events: list[str | EventListener] ): source_file = Path(inspect.getfile(component_class))
修复后的代码 python 复制代码 from pathlib import Path import inspect
def create_or_modify_pyi( component_class: type, class_name: str, events: list[str | EventListener] ): source_file = Path(inspect.getfile(component_class))
提交信息示例 你可以将以下信息提交给 GitHub 上的作者:
问题描述:
在尝试运行 D:\fastsdcpu-main\src\app.py 时遇到以下错误:
arduino 复制代码 Traceback (most recent call last): File "D:\fastsdcpu-main\src\app.py", line 10, in
from frontend.webui.image_variations_ui import generate_image_variations
File "D:\fastsdcpu-main\src\frontend\webui\image_variations_ui.py", line 2, in
import gradio as gr
File "D:\fastsdcpu-main\env\Lib\site-packages\gradio__init.py", line 3, in
import gradio._simple_templates
File "D:\fastsdcpu-main\env\Lib\site-packages\gradio_simple_templates__init.py", line 1, in
from .simpledropdown import SimpleDropdown
File "D:\fastsdcpu-main\env\Lib\site-packages\gradio_simple_templates\simpledropdown.py", line 6, in
from gradio.components.base import FormComponent
File "D:\fastsdcpu-main\env\Lib\site-packages\gradio\components\ init__.py", line 40, in
from gradio.components.multimodal_textbox import MultimodalTextbox
File "D:\fastsdcpu-main\env\Lib\site-packages\gradio\components\multimodal_textbox.py", line 34, in
class MultimodalTextbox(FormComponent):
File "D:\fastsdcpu-main\env\Lib\site-packages\gradio\component_meta.py", line 198, in new__
create_or_modify_pyi(component_class, name, events)
File "D:\fastsdcpu-main\env\Lib\site-packages\gradio\component_meta.py", line 92, in create_or_modify_pyi
source_code = source_file.read_text()
^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python311\Lib\pathlib.py", line 1059, in read_text
return f.read()
^^^^^^^^
UnicodeDecodeError: 'gbk' codec can't decode byte 0xb2 in position 2074: illegal multibyte sequence
问题解决:
通过显式指定文件读取和写入编码为 UTF-8 可以解决此问题。以下是修复前后的代码:
原代码:
python 复制代码
在 D:\fastsdcpu-main\env\Lib\site-packages\gradio\component_meta.py 中的 create_or_modify_pyi 函数
def create_or_modify_pyi( component_class: type, class_name: str, events: list[str | EventListener] ): source_file = Path(inspect.getfile(component_class))
修复后的代码:
python 复制代码 from pathlib import Path import inspect
def create_or_modify_pyi( component_class: type, class_name: str, events: list[str | EventListener] ): source_file = Path(inspect.getfile(component_class))