Skip to content

Instantly share code, notes, and snippets.

@w-e-w
Last active November 24, 2023 18:31
Show Gist options
  • Save w-e-w/b491f5a67494dd242d5526e421b7365a to your computer and use it in GitHub Desktop.
Save w-e-w/b491f5a67494dd242d5526e421b7365a to your computer and use it in GitHub Desktop.
stable-diffusion-webui version info
from pkg_resources import parse_version
from importlib.util import find_spec
from subprocess import check_output
from pathlib import Path
from os import environ
import re
def get_version_from_changelog():
"""
get webui version from CHANGELOG.md
works for webui version >= 1.1.0, anything before will return fallback value of version 1
return: (changelog_version, suffix)
changelog_version: version from CHANGELOG.md
changelog_suffix: suffix after changelog_version like -alpha -beta -pre
currently suffix are not in used in web UI, implemented for future-proofing
"""
try:
changelog_md = Path(find_spec('webui').origin).parent.joinpath('CHANGELOG.md')
with open(changelog_md, 'r', encoding='utf-8') as file:
version_pattern = re.compile(r'(\d+(?:\.\d)+)(.+)?')
found = next(found for line in file if (found := version_pattern.search(line)))
return parse_version(found.group(1)), found.group(2)
except Exception:
return parse_version('1'), None
def get_version_from_git():
"""
get webui version info using git
return: (tag_version, tag_suffix, additional_commits, commit_hash)
tag_version: version form git tag
tag_suffix: suffix after tag_version like -alpha -beta -pre
currently suffix are not in used in web UI, implemented for future-proofing
additional_commits: number of additional commits on top of the version tag
commit_hash: webui git commit hash
this detailed provides specific git commit, useful when detailed version information is needed
works for versions before the introduction of change logs, but will not work if webui is not a git repo
"""
tag_version = tag_suffix = additional_commits = commit_hash = None
try:
webui_root = Path(find_spec('webui').origin).parent
git_describe = check_output(
[environ.get('GIT', 'git'), '-C', webui_root, 'describe', '--tags', '--long', '--always'],
encoding='utf8').strip()
if parse := re.search(r'(\d+(?:\.\d)+)(.+?)?-(\d+)-g(.+)', git_describe):
tag_version = parse_version(parse.group(1))
tag_suffix = parse.group(2)
additional_commits = parse.group(3)
commit_hash = parse.group(4)
else: # if no tag is found then info will be commit hash
commit_hash = git_describe
except Exception:
pass
return tag_version or parse_version('1'), tag_suffix, additional_commits, commit_hash
def get_webui_version_info():
"""
get detailed webui version info, combination of the two above
return: (webui_version, changelog_version, changelog_suffix, tag_version, tag_suffix, additional_commits, commit_hash)
webui_version: webui version
returns the larger version number CHANGELOG.md and git
fallback version if none is found: 1
changelog_version: version from CHANGELOG.md
changelog_suffix: suffix after changelog_version
tag_version: version form git tag
tag_suffix: suffix after tag_version
additional_commits: number of additional commits on top of the version tag
commit_hash: webui git commit hash
note: tag_version may not match the version stated in the CHANGELOG.md
"""
changelog_version, changelog_suffix = get_version_from_changelog()
tag_version, tag_suffix, additional_commits, commit_hash = get_version_from_git()
# use the larger version number as the webui_version
webui_version = changelog_version if changelog_version > tag_version else tag_version
return webui_version, changelog_version, changelog_suffix, tag_version, tag_suffix, additional_commits, commit_hash
if __name__ == '__main__':
"""
A reliable method of testing the version of Stable Diffusion web UI
The following example is on 2023-11-25 using
https://github.com/AUTOMATIC1111/stable-diffusion-webui/tree/8aa51f682c17d85f4585b9471860224568d25e95
"""
version_changelog, *_ = changelog_info = get_version_from_changelog()
print(changelog_info) # (<Version('1.6.1')>, None)
print(version_changelog == parse_version('1.6.1')) # True
version_tag, *_ = git_info = get_version_from_git()
print(git_info) # (<Version('1.6.0')>, '372', '8aa51f68')
print(version_tag == parse_version('1.6.1')) # False
version, *_ = detailed_version_info = get_webui_version_info()
print(detailed_version_info) # (<Version('1.6.1')>, <Version('1.6.1')>, None, <Version('1.6.0')>, None, '372', '8aa51f68')
print(version >= parse_version('1.5.1')) # True
print(version == parse_version('1.6')) # False
print(version > parse_version('2')) # False
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment