1212import traceback
1313import platform
1414import subprocess as sp
15- from functools import reduce
15+ from functools import partial , reduce
1616
1717import numpy as np
1818import torch
@@ -261,6 +261,19 @@ def f(*args, extra_outputs_array=extra_outputs, **kwargs):
261261 return f
262262
263263
264+ def calc_time_left (progress , threshold , label , force_display ):
265+ if progress == 0 :
266+ return ""
267+ else :
268+ time_since_start = time .time () - shared .state .time_start
269+ eta = (time_since_start / progress )
270+ eta_relative = eta - time_since_start
271+ if (eta_relative > threshold and progress > 0.02 ) or force_display :
272+ return label + time .strftime ('%H:%M:%S' , time .gmtime (eta_relative ))
273+ else :
274+ return ""
275+
276+
264277def check_progress_call (id_part ):
265278 if shared .state .job_count == 0 :
266279 return "" , gr_show (False ), gr_show (False ), gr_show (False )
@@ -272,11 +285,15 @@ def check_progress_call(id_part):
272285 if shared .state .sampling_steps > 0 :
273286 progress += 1 / shared .state .job_count * shared .state .sampling_step / shared .state .sampling_steps
274287
288+ time_left = calc_time_left ( progress , 60 , " ETA:" , shared .state .time_left_force_display )
289+ if time_left != "" :
290+ shared .state .time_left_force_display = True
291+
275292 progress = min (progress , 1 )
276293
277294 progressbar = ""
278295 if opts .show_progressbar :
279- progressbar = f"""{ progress * 100 } %">{ str (int (progress * 100 ))+ "%" if progress > 0.01 else "" }
""" 296+ progressbar = f"""overflow:hidden;width:{ progress * 100 } %">{ str (int (progress * 100 ))+ "%" + time_left if progress > 0.01 else "" }
""" 280297
281298 image = gr_show (False )
282299 preview_visibility = gr_show (False )
@@ -308,6 +325,8 @@ def check_progress_call_initial(id_part):
308325 shared .state .current_latent = None
309326 shared .state .current_image = None
310327 shared .state .textinfo = None
328+ shared .state .time_start = time .time ()
329+ shared .state .time_left_force_display = False
311330
312331 return check_progress_call (id_part )
313332
@@ -1543,6 +1562,7 @@ def run_settings_single(value, key):
15431562
15441563 def reload_scripts ():
15451564 modules .scripts .reload_script_body_only ()
1565+ reload_javascript () # need to refresh the html page
15461566
15471567 reload_script_bodies .click (
15481568 fn = reload_scripts ,
@@ -1801,26 +1821,30 @@ def apply_field(obj, field, condition=None, init_field=None):
18011821 return demo
18021822
18031823
1804- with open (os .path .join (script_path , "script.js" ), "r" , encoding = "utf8" ) as jsfile :
1805- javascript = f''
1824+ def load_javascript (raw_response ):
1825+ with open (os .path .join (script_path , "script.js" ), "r" , encoding = "utf8" ) as jsfile :
1826+ javascript = f''
18061827
1807- jsdir = os .path .join (script_path , "javascript" )
1808- for filename in sorted (os .listdir (jsdir )):
1809- with open (os .path .join (jsdir , filename ), "r" , encoding = "utf8" ) as jsfile :
1810- javascript += f"\n "
1828+ jsdir = os .path .join (script_path , "javascript" )
1829+ for filename in sorted (os .listdir (jsdir )):
1830+ with open (os .path .join (jsdir , filename ), "r" , encoding = "utf8" ) as jsfile :
1831+ javascript += f"\n "
18111832
1812- if cmd_opts .theme is not None :
1813- javascript += f"\n \n "
1833+ if cmd_opts .theme is not None :
1834+ javascript += f"\n \n "
18141835
1815- javascript += f"\n "
1836+ javascript += f"\n "
18161837
1817- if 'gradio_routes_templates_response' not in globals ():
18181838 def template_response (* args , ** kwargs ):
1819- res = gradio_routes_templates_response (* args , ** kwargs )
1820- res .body = res .body .replace (b'' , f'{ javascript } ' .encode ("utf8" ))
1839+ res = raw_response (* args , ** kwargs )
1840+ res .body = res .body .replace (
1841+ b'' , f'{ javascript } ' .encode ("utf8" ))
18211842 res .init_headers ()
18221843 return res
18231844
1824- gradio_routes_templates_response = gradio .routes .templates .TemplateResponse
18251845 gradio .routes .templates .TemplateResponse = template_response
18261846
1847+
1848+ reload_javascript = partial (load_javascript ,
1849+ gradio .routes .templates .TemplateResponse )
1850+ reload_javascript ()
0 commit comments