johnsmith253325 commited on
Commit
406ae44
1 Parent(s): d3f26cb

bugfix: 消除了Windows平台下报错'TZ' 不是内部或外部命令的问题

Browse files
Files changed (2) hide show
  1. ChuanhuChatbot.py +7 -6
  2. modules/repo.py +129 -42
ChuanhuChatbot.py CHANGED
@@ -1,4 +1,10 @@
1
  # -*- coding:utf-8 -*-
 
 
 
 
 
 
2
  from modules.models.models import get_model
3
  from modules.train_func import *
4
  from modules.repo import *
@@ -10,11 +16,6 @@ from modules.config import *
10
  from modules import config
11
  import gradio as gr
12
  import colorama
13
- import logging
14
- logging.basicConfig(
15
- level=logging.INFO,
16
- format="%(asctime)s [%(levelname)s] [%(filename)s:%(lineno)d] %(message)s",
17
- )
18
 
19
 
20
  logging.getLogger("httpx").setLevel(logging.WARNING)
@@ -433,7 +434,7 @@ with gr.Blocks(theme=small_and_beautiful_theme) as demo:
433
 
434
  with gr.Tab(label=i18n("准备数据集")):
435
  dataset_preview_json = gr.JSON(
436
- label=i18n("数据集预览"), readonly=True)
437
  dataset_selection = gr.Files(label=i18n("选择数据集"), file_types=[
438
  ".xlsx", ".jsonl"], file_count="single")
439
  upload_to_openai_btn = gr.Button(
 
1
  # -*- coding:utf-8 -*-
2
+ import logging
3
+ logging.basicConfig(
4
+ level=logging.INFO,
5
+ format="%(asctime)s [%(levelname)s] [%(filename)s:%(lineno)d] %(message)s",
6
+ )
7
+
8
  from modules.models.models import get_model
9
  from modules.train_func import *
10
  from modules.repo import *
 
16
  from modules import config
17
  import gradio as gr
18
  import colorama
 
 
 
 
 
19
 
20
 
21
  logging.getLogger("httpx").setLevel(logging.WARNING)
 
434
 
435
  with gr.Tab(label=i18n("准备数据集")):
436
  dataset_preview_json = gr.JSON(
437
+ label=i18n("数据集预览"))
438
  dataset_selection = gr.Files(label=i18n("选择数据集"), file_types=[
439
  ".xlsx", ".jsonl"], file_count="single")
440
  upload_to_openai_btn = gr.Button(
modules/repo.py CHANGED
@@ -6,30 +6,33 @@ from functools import lru_cache
6
  import logging
7
  import gradio as gr
8
  import datetime
 
9
 
10
  # This file is mainly used to describe repo version info, execute the git command, python pip command, shell command, etc.
11
  # Part of the code in this file is referenced from stable-diffusion-webui/modules/launch_utils.py
12
 
13
  python = sys.executable
14
- pip = os.environ.get('PIP', "pip")
15
- git = os.environ.get('GIT', "git")
16
 
17
  # Pypi index url
18
- index_url = os.environ.get('INDEX_URL', "")
19
 
20
  # Whether to default to printing command output
21
  default_command_live = True
22
 
23
 
24
- def run(command, desc=None, errdesc=None, custom_env=None, live: bool = default_command_live) -> str:
 
 
25
  if desc is not None:
26
  print(desc)
27
  run_kwargs = {
28
  "args": command,
29
  "shell": True,
30
  "env": os.environ if custom_env is None else custom_env,
31
- "encoding": 'utf8',
32
- "errors": 'ignore',
33
  }
34
 
35
  if not live:
@@ -48,29 +51,32 @@ def run(command, desc=None, errdesc=None, custom_env=None, live: bool = default_
48
  error_bits.append(f"stderr: {result.stderr}")
49
  raise RuntimeError("\n".join(error_bits))
50
 
51
- return (result.stdout or "")
52
 
53
 
54
  def run_pip(command, desc=None, pref=None, live=default_command_live):
55
  # if args.skip_install:
56
  # return
57
 
58
- index_url_line = f' --index-url {index_url}' if index_url != '' else ''
59
  return run(
60
- f'"{python}" -m pip {command} --prefer-binary{index_url_line}',
61
- desc=f"{pref} Installing {desc}...",
62
- errdesc=f"Couldn't install {desc}",
63
- live=live
64
  )
65
 
66
 
67
  @lru_cache()
68
  def commit_hash():
69
  try:
70
- return subprocess.check_output([git, "rev-parse", "HEAD"], shell=False, encoding='utf8').strip()
 
 
71
  except Exception:
72
  return "<none>"
73
 
 
74
  def commit_html():
75
  commit = commit_hash()
76
  if commit != "<none>":
@@ -80,6 +86,7 @@ def commit_html():
80
  commit_info = "unknown \U0001F615"
81
  return commit_info
82
 
 
83
  @lru_cache()
84
  def tag_html():
85
  try:
@@ -87,7 +94,7 @@ def tag_html():
87
  try:
88
  # tag = subprocess.check_output([git, "describe", "--tags", "--exact-match"], shell=False, encoding='utf8').strip()
89
  tag = run(f"{git} describe --tags --exact-match", live=False).strip()
90
- except Exception:
91
  tag = "<edited>"
92
  except Exception:
93
  tag = "<none>"
@@ -98,14 +105,16 @@ def tag_html():
98
  tag_info = f'<a style="text-decoration:none;color:inherit" href="https://github.com/GaiZhenbiao/ChuanhuChatGPT/releases/tag/{latest_tag}">{latest_tag}</a><span style="font-size:smaller">*</span>'
99
  else:
100
  tag_info = f'<a style="text-decoration:none;color:inherit" href="https://github.com/GaiZhenbiao/ChuanhuChatGPT/releases/tag/{tag}">{tag}</a>'
101
-
102
  return tag_info
103
 
 
104
  def repo_tag_html():
105
  commit_version = commit_html()
106
  tag_version = tag_html()
107
  return tag_version if tag_version != "unknown \U0001F615" else commit_version
108
 
 
109
  def versions_html():
110
  python_version = ".".join([str(x) for x in sys.version_info[0:3]])
111
  repo_version = repo_tag_html()
@@ -117,20 +126,44 @@ def versions_html():
117
  <a style="text-decoration:none;color:inherit" href="https://github.com/GaiZhenbiao/ChuanhuChatGPT">ChuanhuChat</a>: {repo_version}
118
  """
119
 
 
120
  def version_time():
 
 
 
121
  try:
122
- commit_time = subprocess.check_output(f"TZ=UTC {git} log -1 --format=%cd --date='format-local:%Y-%m-%dT%H:%M:%SZ'", shell=True, encoding='utf8').strip()
123
- # commit_time = run(f"TZ=UTC {git} log -1 --format=%cd --date='format-local:%Y-%m-%dT%H:%M:%SZ'").strip()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
124
  except Exception:
125
  commit_time = "unknown"
126
  return commit_time
127
 
128
 
129
-
130
  def get_current_branch():
131
  try:
132
  # branch = run(f"{git} rev-parse --abbrev-ref HEAD").strip()
133
- branch = subprocess.check_output([git, "rev-parse", "--abbrev-ref", "HEAD"], shell=False, encoding='utf8').strip()
 
 
134
  except Exception:
135
  branch = "<none>"
136
  return branch
@@ -139,7 +172,10 @@ def get_current_branch():
139
  def get_latest_release():
140
  try:
141
  import requests
142
- release = requests.get("https://api.github.com/repos/GaiZhenbiao/ChuanhuChatGPT/releases/latest").json()
 
 
 
143
  tag = release["tag_name"]
144
  release_note = release["body"]
145
  need_pip = release_note.find("requirements reinstall needed") != -1
@@ -149,21 +185,34 @@ def get_latest_release():
149
  need_pip = False
150
  return {"tag": tag, "release_note": release_note, "need_pip": need_pip}
151
 
 
152
  def get_tag_commit_hash(tag):
153
  try:
154
  import requests
155
- tags = requests.get("https://api.github.com/repos/GaiZhenbiao/ChuanhuChatGPT/tags").json()
 
 
 
156
  commit_hash = [x["commit"]["sha"] for x in tags if x["name"] == tag][0]
157
  except Exception:
158
  commit_hash = "<none>"
159
  return commit_hash
160
 
 
161
  def repo_need_stash():
162
  try:
163
- return subprocess.check_output([git, "diff-index", "--quiet", "HEAD", "--"], shell=False, encoding='utf8').strip() != ""
 
 
 
 
 
 
 
164
  except Exception:
165
  return True
166
 
 
167
  def background_update():
168
  # {git} fetch --all && ({git} pull https://github.com/GaiZhenbiao/ChuanhuChatGPT.git main -f || ({git} stash && {git} pull https://github.com/GaiZhenbiao/ChuanhuChatGPT.git main -f && {git} stash pop)) && {pip} install -r requirements.txt")
169
  try:
@@ -175,50 +224,83 @@ def background_update():
175
 
176
  timestamp = datetime.datetime.now().strftime("%Y%m%d%H%M%S")
177
  current_branch = get_current_branch()
178
- updater_branch = f'tmp_{timestamp}'
179
- backup_branch = f'backup_{timestamp}'
180
  track_repo = "https://github.com/GaiZhenbiao/ChuanhuChatGPT.git"
181
  try:
182
  try:
183
- run(f"{git} fetch {track_repo}", desc="[Updater] Fetching from github...", live=False)
 
 
 
 
184
  except Exception:
185
- logging.error(f"Update failed in fetching, check your network connection")
 
 
186
  return "failed"
187
-
188
- run(f'{git} stash push --include-untracked -m "updater-{timestamp}"',
189
- desc=f'[Updater] Restoring you local changes on stash updater-{timestamp}', live=False) if need_stash else None
190
-
 
 
 
191
  run(f"{git} checkout -b {backup_branch}", live=False)
192
  run(f"{git} checkout -b {updater_branch}", live=False)
193
  run(f"{git} reset --hard FETCH_HEAD", live=False)
194
- run(f"{git} reset --hard {latest_release_hash}", desc=f'[Updater] Checking out {latest_release_tag}...', live=False)
 
 
 
 
195
  run(f"{git} checkout {current_branch}", live=False)
196
-
197
  try:
198
- run(f"{git} merge --no-edit {updater_branch} -q", desc=f"[Updater] Trying to apply latest update on version {latest_release_tag}...")
 
 
 
199
  except Exception:
200
  logging.error(f"Update failed in merging")
201
  try:
202
- run(f"{git} merge --abort", desc="[Updater] Conflict detected, canceling update...")
 
 
 
203
  run(f"{git} reset --hard {backup_branch}", live=False)
204
  run(f"{git} branch -D -f {updater_branch}", live=False)
205
  run(f"{git} branch -D -f {backup_branch}", live=False)
206
  run(f"{git} stash pop", live=False) if need_stash else None
207
- logging.error(f"Update failed, but your file was safely reset to the state before the update.")
 
 
208
  return "failed"
209
  except Exception as e:
210
- logging.error(f"!!!Update failed in resetting, try to reset your files manually. {e}")
 
 
211
  return "failed"
212
 
213
  if need_stash:
214
  try:
215
- run(f"{git} stash apply", desc="[Updater] Trying to restore your local modifications...", live=False)
 
 
 
 
216
  except Exception:
217
- run(f"{git} reset --hard {backup_branch}", desc="[Updater] Conflict detected, canceling update...", live=False)
 
 
 
 
218
  run(f"{git} branch -D -f {updater_branch}", live=False)
219
  run(f"{git} branch -D -f {backup_branch}", live=False)
220
  run(f"{git} stash pop", live=False)
221
- logging.error(f"Update failed in applying your local changes, but your file was safely reset to the state before the update.")
 
 
222
  return "failed"
223
  run(f"{git} stash drop", live=False)
224
 
@@ -228,12 +310,17 @@ def background_update():
228
  logging.error(f"Update failed: {e}")
229
  return "failed"
230
  if need_pip:
231
- try:
232
- run_pip(f"install -r requirements.txt", pref="[Updater]", desc="requirements", live=False)
 
 
 
 
 
233
  except Exception:
234
  logging.error(f"Update failed in pip install")
235
  return "failed"
236
  return "success"
237
  except Exception as e:
238
  logging.error(f"Update failed: {e}")
239
- return "failed"
 
6
  import logging
7
  import gradio as gr
8
  import datetime
9
+ import platform
10
 
11
  # This file is mainly used to describe repo version info, execute the git command, python pip command, shell command, etc.
12
  # Part of the code in this file is referenced from stable-diffusion-webui/modules/launch_utils.py
13
 
14
  python = sys.executable
15
+ pip = os.environ.get("PIP", "pip")
16
+ git = os.environ.get("GIT", "git")
17
 
18
  # Pypi index url
19
+ index_url = os.environ.get("INDEX_URL", "")
20
 
21
  # Whether to default to printing command output
22
  default_command_live = True
23
 
24
 
25
+ def run(
26
+ command, desc=None, errdesc=None, custom_env=None, live: bool = default_command_live
27
+ ) -> str:
28
  if desc is not None:
29
  print(desc)
30
  run_kwargs = {
31
  "args": command,
32
  "shell": True,
33
  "env": os.environ if custom_env is None else custom_env,
34
+ "encoding": "utf8",
35
+ "errors": "ignore",
36
  }
37
 
38
  if not live:
 
51
  error_bits.append(f"stderr: {result.stderr}")
52
  raise RuntimeError("\n".join(error_bits))
53
 
54
+ return result.stdout or ""
55
 
56
 
57
  def run_pip(command, desc=None, pref=None, live=default_command_live):
58
  # if args.skip_install:
59
  # return
60
 
61
+ index_url_line = f" --index-url {index_url}" if index_url != "" else ""
62
  return run(
63
+ f'"{python}" -m pip {command} --prefer-binary{index_url_line}',
64
+ desc=f"{pref} Installing {desc}...",
65
+ errdesc=f"Couldn't install {desc}",
66
+ live=live,
67
  )
68
 
69
 
70
  @lru_cache()
71
  def commit_hash():
72
  try:
73
+ return subprocess.check_output(
74
+ [git, "rev-parse", "HEAD"], shell=False, encoding="utf8"
75
+ ).strip()
76
  except Exception:
77
  return "<none>"
78
 
79
+
80
  def commit_html():
81
  commit = commit_hash()
82
  if commit != "<none>":
 
86
  commit_info = "unknown \U0001F615"
87
  return commit_info
88
 
89
+
90
  @lru_cache()
91
  def tag_html():
92
  try:
 
94
  try:
95
  # tag = subprocess.check_output([git, "describe", "--tags", "--exact-match"], shell=False, encoding='utf8').strip()
96
  tag = run(f"{git} describe --tags --exact-match", live=False).strip()
97
+ except Exception:
98
  tag = "<edited>"
99
  except Exception:
100
  tag = "<none>"
 
105
  tag_info = f'<a style="text-decoration:none;color:inherit" href="https://github.com/GaiZhenbiao/ChuanhuChatGPT/releases/tag/{latest_tag}">{latest_tag}</a><span style="font-size:smaller">*</span>'
106
  else:
107
  tag_info = f'<a style="text-decoration:none;color:inherit" href="https://github.com/GaiZhenbiao/ChuanhuChatGPT/releases/tag/{tag}">{tag}</a>'
108
+
109
  return tag_info
110
 
111
+
112
  def repo_tag_html():
113
  commit_version = commit_html()
114
  tag_version = tag_html()
115
  return tag_version if tag_version != "unknown \U0001F615" else commit_version
116
 
117
+
118
  def versions_html():
119
  python_version = ".".join([str(x) for x in sys.version_info[0:3]])
120
  repo_version = repo_tag_html()
 
126
  <a style="text-decoration:none;color:inherit" href="https://github.com/GaiZhenbiao/ChuanhuChatGPT">ChuanhuChat</a>: {repo_version}
127
  """
128
 
129
+
130
  def version_time():
131
+ git = "git"
132
+ cmd = f"{git} log -1 --format=%cd --date=iso-strict"
133
+ commit_time = "unknown"
134
  try:
135
+ if platform.system() == "Windows":
136
+ # For Windows
137
+ env = dict(os.environ) # copy the current environment
138
+ env["TZ"] = "UTC" # set timezone to UTC
139
+ raw_commit_time = subprocess.check_output(
140
+ cmd, shell=True, encoding="utf8", env=env
141
+ ).strip()
142
+ else:
143
+ # For Unix systems
144
+ cmd = f"TZ=UTC {cmd}"
145
+ raw_commit_time = subprocess.check_output(
146
+ cmd, shell=True, encoding="utf8"
147
+ ).strip()
148
+
149
+ # Convert the date-time to the desired format
150
+ commit_datetime = datetime.datetime.strptime(
151
+ raw_commit_time, "%Y-%m-%dT%H:%M:%S%z"
152
+ )
153
+ commit_time = commit_datetime.strftime("%Y-%m-%dT%H:%M:%SZ")
154
+
155
+ logging.info(f"commit time: {commit_time}")
156
  except Exception:
157
  commit_time = "unknown"
158
  return commit_time
159
 
160
 
 
161
  def get_current_branch():
162
  try:
163
  # branch = run(f"{git} rev-parse --abbrev-ref HEAD").strip()
164
+ branch = subprocess.check_output(
165
+ [git, "rev-parse", "--abbrev-ref", "HEAD"], shell=False, encoding="utf8"
166
+ ).strip()
167
  except Exception:
168
  branch = "<none>"
169
  return branch
 
172
  def get_latest_release():
173
  try:
174
  import requests
175
+
176
+ release = requests.get(
177
+ "https://api.github.com/repos/GaiZhenbiao/ChuanhuChatGPT/releases/latest"
178
+ ).json()
179
  tag = release["tag_name"]
180
  release_note = release["body"]
181
  need_pip = release_note.find("requirements reinstall needed") != -1
 
185
  need_pip = False
186
  return {"tag": tag, "release_note": release_note, "need_pip": need_pip}
187
 
188
+
189
  def get_tag_commit_hash(tag):
190
  try:
191
  import requests
192
+
193
+ tags = requests.get(
194
+ "https://api.github.com/repos/GaiZhenbiao/ChuanhuChatGPT/tags"
195
+ ).json()
196
  commit_hash = [x["commit"]["sha"] for x in tags if x["name"] == tag][0]
197
  except Exception:
198
  commit_hash = "<none>"
199
  return commit_hash
200
 
201
+
202
  def repo_need_stash():
203
  try:
204
+ return (
205
+ subprocess.check_output(
206
+ [git, "diff-index", "--quiet", "HEAD", "--"],
207
+ shell=False,
208
+ encoding="utf8",
209
+ ).strip()
210
+ != ""
211
+ )
212
  except Exception:
213
  return True
214
 
215
+
216
  def background_update():
217
  # {git} fetch --all && ({git} pull https://github.com/GaiZhenbiao/ChuanhuChatGPT.git main -f || ({git} stash && {git} pull https://github.com/GaiZhenbiao/ChuanhuChatGPT.git main -f && {git} stash pop)) && {pip} install -r requirements.txt")
218
  try:
 
224
 
225
  timestamp = datetime.datetime.now().strftime("%Y%m%d%H%M%S")
226
  current_branch = get_current_branch()
227
+ updater_branch = f"tmp_{timestamp}"
228
+ backup_branch = f"backup_{timestamp}"
229
  track_repo = "https://github.com/GaiZhenbiao/ChuanhuChatGPT.git"
230
  try:
231
  try:
232
+ run(
233
+ f"{git} fetch {track_repo}",
234
+ desc="[Updater] Fetching from github...",
235
+ live=False,
236
+ )
237
  except Exception:
238
+ logging.error(
239
+ f"Update failed in fetching, check your network connection"
240
+ )
241
  return "failed"
242
+
243
+ run(
244
+ f'{git} stash push --include-untracked -m "updater-{timestamp}"',
245
+ desc=f"[Updater] Restoring you local changes on stash updater-{timestamp}",
246
+ live=False,
247
+ ) if need_stash else None
248
+
249
  run(f"{git} checkout -b {backup_branch}", live=False)
250
  run(f"{git} checkout -b {updater_branch}", live=False)
251
  run(f"{git} reset --hard FETCH_HEAD", live=False)
252
+ run(
253
+ f"{git} reset --hard {latest_release_hash}",
254
+ desc=f"[Updater] Checking out {latest_release_tag}...",
255
+ live=False,
256
+ )
257
  run(f"{git} checkout {current_branch}", live=False)
258
+
259
  try:
260
+ run(
261
+ f"{git} merge --no-edit {updater_branch} -q",
262
+ desc=f"[Updater] Trying to apply latest update on version {latest_release_tag}...",
263
+ )
264
  except Exception:
265
  logging.error(f"Update failed in merging")
266
  try:
267
+ run(
268
+ f"{git} merge --abort",
269
+ desc="[Updater] Conflict detected, canceling update...",
270
+ )
271
  run(f"{git} reset --hard {backup_branch}", live=False)
272
  run(f"{git} branch -D -f {updater_branch}", live=False)
273
  run(f"{git} branch -D -f {backup_branch}", live=False)
274
  run(f"{git} stash pop", live=False) if need_stash else None
275
+ logging.error(
276
+ f"Update failed, but your file was safely reset to the state before the update."
277
+ )
278
  return "failed"
279
  except Exception as e:
280
+ logging.error(
281
+ f"!!!Update failed in resetting, try to reset your files manually. {e}"
282
+ )
283
  return "failed"
284
 
285
  if need_stash:
286
  try:
287
+ run(
288
+ f"{git} stash apply",
289
+ desc="[Updater] Trying to restore your local modifications...",
290
+ live=False,
291
+ )
292
  except Exception:
293
+ run(
294
+ f"{git} reset --hard {backup_branch}",
295
+ desc="[Updater] Conflict detected, canceling update...",
296
+ live=False,
297
+ )
298
  run(f"{git} branch -D -f {updater_branch}", live=False)
299
  run(f"{git} branch -D -f {backup_branch}", live=False)
300
  run(f"{git} stash pop", live=False)
301
+ logging.error(
302
+ f"Update failed in applying your local changes, but your file was safely reset to the state before the update."
303
+ )
304
  return "failed"
305
  run(f"{git} stash drop", live=False)
306
 
 
310
  logging.error(f"Update failed: {e}")
311
  return "failed"
312
  if need_pip:
313
+ try:
314
+ run_pip(
315
+ f"install -r requirements.txt",
316
+ pref="[Updater]",
317
+ desc="requirements",
318
+ live=False,
319
+ )
320
  except Exception:
321
  logging.error(f"Update failed in pip install")
322
  return "failed"
323
  return "success"
324
  except Exception as e:
325
  logging.error(f"Update failed: {e}")
326
+ return "failed"