Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix encoding error #8381

Merged
merged 3 commits into from
May 27, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions gradio/blocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -997,12 +997,12 @@ def __init__(
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
Expand Down
2 changes: 1 addition & 1 deletion gradio/cli/commands/components/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion gradio/cli/commands/components/create.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
10 changes: 5 additions & 5 deletions gradio/cli/commands/components/docs.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"])
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand All @@ -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
)
Expand Down
6 changes: 3 additions & 3 deletions gradio/cli/commands/deploy_space.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand All @@ -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(
Expand Down
6 changes: 3 additions & 3 deletions gradio/flagging.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,11 +104,11 @@ def flag(
)
)

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

Expand Down Expand Up @@ -393,7 +393,7 @@ def _save_as_csv(data_file: Path, headers: list[str], row: list[Any]) -> int:
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

Expand Down
2 changes: 1 addition & 1 deletion gradio/themes/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ def load(cls, path: str) -> 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
Expand Down
6 changes: 3 additions & 3 deletions gradio/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down