Spaces:
No application file
No application file
Upload api\controllers\console\version.py with huggingface_hub
Browse files
api//controllers//console//version.py
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import json
|
| 2 |
+
import logging
|
| 3 |
+
|
| 4 |
+
import requests
|
| 5 |
+
from flask_restful import Resource, reqparse # type: ignore
|
| 6 |
+
from packaging import version
|
| 7 |
+
|
| 8 |
+
from configs import dify_config
|
| 9 |
+
|
| 10 |
+
from . import api
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
class VersionApi(Resource):
|
| 14 |
+
def get(self):
|
| 15 |
+
parser = reqparse.RequestParser()
|
| 16 |
+
parser.add_argument("current_version", type=str, required=True, location="args")
|
| 17 |
+
args = parser.parse_args()
|
| 18 |
+
check_update_url = dify_config.CHECK_UPDATE_URL
|
| 19 |
+
|
| 20 |
+
result = {
|
| 21 |
+
"version": dify_config.CURRENT_VERSION,
|
| 22 |
+
"release_date": "",
|
| 23 |
+
"release_notes": "",
|
| 24 |
+
"can_auto_update": False,
|
| 25 |
+
"features": {
|
| 26 |
+
"can_replace_logo": dify_config.CAN_REPLACE_LOGO,
|
| 27 |
+
"model_load_balancing_enabled": dify_config.MODEL_LB_ENABLED,
|
| 28 |
+
},
|
| 29 |
+
}
|
| 30 |
+
|
| 31 |
+
if not check_update_url:
|
| 32 |
+
return result
|
| 33 |
+
|
| 34 |
+
try:
|
| 35 |
+
response = requests.get(check_update_url, {"current_version": args.get("current_version")})
|
| 36 |
+
except Exception as error:
|
| 37 |
+
logging.warning("Check update version error: {}.".format(str(error)))
|
| 38 |
+
result["version"] = args.get("current_version")
|
| 39 |
+
return result
|
| 40 |
+
|
| 41 |
+
content = json.loads(response.content)
|
| 42 |
+
if _has_new_version(latest_version=content["version"], current_version=f"{args.get('current_version')}"):
|
| 43 |
+
result["version"] = content["version"]
|
| 44 |
+
result["release_date"] = content["releaseDate"]
|
| 45 |
+
result["release_notes"] = content["releaseNotes"]
|
| 46 |
+
result["can_auto_update"] = content["canAutoUpdate"]
|
| 47 |
+
return result
|
| 48 |
+
|
| 49 |
+
|
| 50 |
+
def _has_new_version(*, latest_version: str, current_version: str) -> bool:
|
| 51 |
+
try:
|
| 52 |
+
latest = version.parse(latest_version)
|
| 53 |
+
current = version.parse(current_version)
|
| 54 |
+
|
| 55 |
+
# Compare versions
|
| 56 |
+
return latest > current
|
| 57 |
+
except version.InvalidVersion:
|
| 58 |
+
logging.warning(f"Invalid version format: latest={latest_version}, current={current_version}")
|
| 59 |
+
return False
|
| 60 |
+
|
| 61 |
+
|
| 62 |
+
api.add_resource(VersionApi, "/version")
|