From 24ab22d26179ee3d1e5f4957067ebf8722ef2e6f Mon Sep 17 00:00:00 2001 From: Xu Song Date: Mon, 27 May 2024 16:50:47 +0800 Subject: [PATCH] Fix encoding error (#8381) --- .changeset/common-zoos-retire.md | 5 +++++ gradio/blocks.py | 4 ++-- gradio/cli/commands/components/build.py | 2 +- gradio/cli/commands/components/create.py | 2 +- gradio/cli/commands/components/docs.py | 10 +++++----- gradio/cli/commands/deploy_space.py | 6 +++--- gradio/flagging.py | 6 +++--- gradio/themes/base.py | 2 +- gradio/utils.py | 6 +++--- 9 files changed, 24 insertions(+), 19 deletions(-) create mode 100644 .changeset/common-zoos-retire.md diff --git a/.changeset/common-zoos-retire.md b/.changeset/common-zoos-retire.md new file mode 100644 index 0000000000..f67c3b5b30 --- /dev/null +++ b/.changeset/common-zoos-retire.md @@ -0,0 +1,5 @@ +--- +"gradio": patch +--- + +fix:Fix encoding error diff --git a/gradio/blocks.py b/gradio/blocks.py index 29c8778bfc..df807f43cb 100644 --- a/gradio/blocks.py +++ b/gradio/blocks.py @@ -997,12 +997,12 @@ class Blocks(BlockContext, BlocksEvents, metaclass=BlocksMeta): self.fill_height = fill_height self.delete_cache = delete_cache if css is not None and os.path.exists(css): - with open(css) as css_file: + with open(css, encoding="utf-8") as css_file: self.css = css_file.read() else: self.css = css if js is not None and os.path.exists(js): - with open(js) as js_file: + with open(js, encoding="utf-8") as js_file: self.js = js_file.read() else: self.js = js diff --git a/gradio/cli/commands/components/build.py b/gradio/cli/commands/components/build.py index 159777c2aa..d642817235 100644 --- a/gradio/cli/commands/components/build.py +++ b/gradio/cli/commands/components/build.py @@ -88,7 +88,7 @@ def _build( "Set [bold][magenta]--no-bump-version[/][/] to use the version in pyproject.toml file." ) pyproject_toml["project"]["version"] = str(version) # type: ignore - with open(path / "pyproject.toml", "w") as f: + with open(path / "pyproject.toml", "w", encoding="utf-8") as f: dump(pyproject_toml, f) else: version = pyproject_toml["project"]["version"] # type: ignore diff --git a/gradio/cli/commands/components/create.py b/gradio/cli/commands/components/create.py index 20b9bf8238..59453e9173 100644 --- a/gradio/cli/commands/components/create.py +++ b/gradio/cli/commands/components/create.py @@ -177,7 +177,7 @@ def _create( break current_keywords = pyproject_toml["project"].get("keywords", []) # type: ignore pyproject_toml["project"]["keywords"] = current_keywords + keywords # type: ignore - with open(directory / "pyproject.toml", "w") as f: + with open(directory / "pyproject.toml", "w", encoding="utf-8") as f: dump(pyproject_toml, f) (directory / "demo" / "requirements.txt").write_text(package_name) diff --git a/gradio/cli/commands/components/docs.py b/gradio/cli/commands/components/docs.py index e14d4edb04..62e0c068c1 100644 --- a/gradio/cli/commands/components/docs.py +++ b/gradio/cli/commands/components/docs.py @@ -83,7 +83,7 @@ def _docs( f"Cannot find pyproject.toml file in [orange3]{_component_dir}[/]" ) - with open(_component_dir / "pyproject.toml") as f: + with open(_component_dir / "pyproject.toml", encoding="utf-8") as f: data = toml.loads(f.read()) name = get_deep(data, ["project", "name"]) @@ -122,7 +122,7 @@ def run_command( _component_dir: Path, simple: bool = False, ): - with open(_demo_path) as f: + with open(_demo_path, encoding="utf-8") as f: demo = f.read() pypi_exists = requests.get(f"https://pypi.org/pypi/{name}/json").status_code @@ -163,13 +163,13 @@ def run_command( suppress_demo_check=suppress_demo_check, ) - with open(_demo_dir / "space.py", "w") as f: + with open(_demo_dir / "space.py", "w", encoding="utf-8") as f: f.write(source) if not simple: live.update( f":white_check_mark: Space created in [orange3]{_demo_dir}/space.py[/]\n" ) - with open(_demo_dir / "css.css", "w") as f: + with open(_demo_dir / "css.css", "w", encoding="utf-8") as f: f.write(css) if generate_readme: @@ -181,7 +181,7 @@ def run_command( readme_content = Path(_readme_path).read_text() - with open(_readme_path, "w") as f: + with open(_readme_path, "w", encoding="utf-8") as f: yaml_regex = re.search( "(?:^|[\r\n])---[\n\r]+([\\S\\s]*?)[\n\r]+---([\n\r]|$)", readme_content ) diff --git a/gradio/cli/commands/deploy_space.py b/gradio/cli/commands/deploy_space.py index c825644563..ad082d5817 100644 --- a/gradio/cli/commands/deploy_space.py +++ b/gradio/cli/commands/deploy_space.py @@ -82,7 +82,7 @@ def add_configuration_to_readme( requirement = input("Enter a dependency (leave blank to end): ") if not requirement: break - with open(requirements_file, "a") as f: + with open(requirements_file, "a", encoding="utf-8") as f: f.write(requirement + "\n") if ( @@ -96,10 +96,10 @@ def add_configuration_to_readme( repo_directory, ".github/workflows/update_space.yml" ) os.makedirs(os.path.dirname(github_action_file), exist_ok=True) - with open(github_action_template) as f: + with open(github_action_template, encoding="utf-8") as f: github_action_content = f.read() github_action_content = github_action_content.replace("$branch", track_branch) - with open(github_action_file, "w") as f: + with open(github_action_file, "w", encoding="utf-8") as f: f.write(github_action_content) print( diff --git a/gradio/flagging.py b/gradio/flagging.py index f8f7171253..c012835ca6 100644 --- a/gradio/flagging.py +++ b/gradio/flagging.py @@ -104,11 +104,11 @@ class SimpleCSVLogger(FlaggingCallback): ) ) - with open(log_filepath, "a", newline="") as csvfile: + with open(log_filepath, "a", encoding="utf-8", newline="") as csvfile: writer = csv.writer(csvfile) writer.writerow(utils.sanitize_list_for_csv(csv_data)) - with open(log_filepath) as csvfile: + with open(log_filepath, encoding="utf-8") as csvfile: line_count = len(list(csv.reader(csvfile))) - 1 return line_count @@ -393,7 +393,7 @@ class HuggingFaceDatasetSaver(FlaggingCallback): def _save_as_jsonl(data_file: Path, headers: list[str], row: list[Any]) -> str: """Save data as JSONL and return the sample name (uuid).""" Path.mkdir(data_file.parent, parents=True, exist_ok=True) - with open(data_file, "w") as f: + with open(data_file, "w", encoding="utf-8") as f: json.dump(dict(zip(headers, row)), f) return data_file.parent.name diff --git a/gradio/themes/base.py b/gradio/themes/base.py index a28453bdb8..93c53f52a7 100644 --- a/gradio/themes/base.py +++ b/gradio/themes/base.py @@ -136,7 +136,7 @@ class ThemeClass: Parameters: path: The filepath to read. """ - with open(path) as fp: + with open(path, encoding="utf-8") as fp: return cls.from_dict(json.load(fp, object_hook=fonts.as_font)) @classmethod diff --git a/gradio/utils.py b/gradio/utils.py index 6ce60ace24..7fd42e19a2 100644 --- a/gradio/utils.py +++ b/gradio/utils.py @@ -414,15 +414,15 @@ def launch_counter() -> None: try: if not os.path.exists(JSON_PATH): launches = {"launches": 1} - with open(JSON_PATH, "w+") as j: + with open(JSON_PATH, "w+", encoding="utf-8") as j: json.dump(launches, j) else: - with open(JSON_PATH) as j: + with open(JSON_PATH, encoding="utf-8") as j: launches = json.load(j) launches["launches"] += 1 if launches["launches"] in [25, 50, 150, 500, 1000]: print(en["BETA_INVITE"]) - with open(JSON_PATH, "w") as j: + with open(JSON_PATH, "w", encoding="utf-8") as j: j.write(json.dumps(launches)) except Exception: pass