summaryrefslogtreecommitdiffstats
path: root/g4f/version.py
diff options
context:
space:
mode:
authorH Lohaus <hlohaus@users.noreply.github.com>2024-01-02 01:10:31 +0100
committerGitHub <noreply@github.com>2024-01-02 01:10:31 +0100
commitb1b8ed40a4e8c7c3490b1c6b7cf6b55d0776f366 (patch)
tree6cd09fb2eb4c144e28a82759a2a9a2fa7f30d311 /g4f/version.py
parentMerge pull request #1414 from hlohaus/lia (diff)
parentFix markdown replace (diff)
downloadgpt4free-b1b8ed40a4e8c7c3490b1c6b7cf6b55d0776f366.tar
gpt4free-b1b8ed40a4e8c7c3490b1c6b7cf6b55d0776f366.tar.gz
gpt4free-b1b8ed40a4e8c7c3490b1c6b7cf6b55d0776f366.tar.bz2
gpt4free-b1b8ed40a4e8c7c3490b1c6b7cf6b55d0776f366.tar.lz
gpt4free-b1b8ed40a4e8c7c3490b1c6b7cf6b55d0776f366.tar.xz
gpt4free-b1b8ed40a4e8c7c3490b1c6b7cf6b55d0776f366.tar.zst
gpt4free-b1b8ed40a4e8c7c3490b1c6b7cf6b55d0776f366.zip
Diffstat (limited to '')
-rw-r--r--g4f/version.py47
1 files changed, 47 insertions, 0 deletions
diff --git a/g4f/version.py b/g4f/version.py
new file mode 100644
index 00000000..9e572bbb
--- /dev/null
+++ b/g4f/version.py
@@ -0,0 +1,47 @@
+from os import environ
+import requests
+from functools import cached_property
+from importlib.metadata import version as get_package_version, PackageNotFoundError
+from subprocess import check_output, CalledProcessError, PIPE
+from .errors import VersionNotFoundError
+
+
+class VersionUtils():
+ @cached_property
+ def current_version(self) -> str:
+ # Read from package manager
+ try:
+ return get_package_version("g4f")
+ except PackageNotFoundError:
+ pass
+ # Read from docker environment
+ version = environ.get("G4F_VERSION")
+ if version:
+ return version
+ # Read from git repository
+ try:
+ command = ["git", "describe", "--tags", "--abbrev=0"]
+ return check_output(command, text=True, stderr=PIPE).strip()
+ except CalledProcessError:
+ pass
+ raise VersionNotFoundError("Version not found")
+
+ @cached_property
+ def latest_version(self) -> str:
+ try:
+ get_package_version("g4f")
+ response = requests.get("https://pypi.org/pypi/g4f/json").json()
+ return response["info"]["version"]
+ except PackageNotFoundError:
+ url = "https://api.github.com/repos/xtekky/gpt4free/releases/latest"
+ response = requests.get(url).json()
+ return response["tag_name"]
+
+ def check_pypi_version(self) -> None:
+ try:
+ if self.current_version != self.latest_version:
+ print(f'New pypi version: {self.latest_version} (current: {self.version}) | pip install -U g4f')
+ except Exception as e:
+ print(f'Failed to check g4f pypi version: {e}')
+
+utils = VersionUtils() \ No newline at end of file