ParahumanSkitter commited on
Commit
75bf966
·
verified ·
1 Parent(s): 02e94b2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +31 -21
app.py CHANGED
@@ -2,26 +2,28 @@ import gradio as gr
2
  from random import randint
3
  from all_models import models
4
  from datetime import datetime
 
 
 
 
5
 
6
  def get_current_time():
7
  now = datetime.now()
8
- now2 = now
9
- current_time = now2.strftime("%Y-%m-%d %H:%M:%S")
10
- ki = f'{current_time}'
11
- return ki
12
 
13
- def load_fn(models):
14
- global models_load
15
- models_load = {}
16
- for model in models:
17
- if model not in models_load.keys():
18
- try:
19
- m = gr.load(f'models/{model}')
20
- except Exception as error:
21
- m = gr.Interface(lambda txt: None, ['text'], ['image'])
22
- models_load.update({model: m})
23
 
24
- load_fn(models)
25
 
26
  num_models = len(models)
27
  default_models = models[:num_models]
@@ -41,13 +43,21 @@ def gen_fn(model_str, prompt, max_retries=10):
41
  while retries < max_retries:
42
  try:
43
  noise = str(randint(0, 9999999))
44
- result = models_load[model_str](f'{prompt} {noise}')
 
 
45
  return result
 
 
 
 
46
  except Exception as e:
47
- print(f"Error generating image: {e}")
48
- retries += 1
49
- if retries >= max_retries:
50
- raise Exception(f"Failed to generate image after {max_retries} retries.")
 
 
51
 
52
  return None
53
 
@@ -237,5 +247,5 @@ body {
237
  with gr.Blocks(css=custom_css) as demo:
238
  make_me()
239
 
240
- demo.queue(concurrency_count=500)
241
  demo.launch()
 
2
  from random import randint
3
  from all_models import models
4
  from datetime import datetime
5
+ import logging
6
+
7
+ # Set up logging
8
+ logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
9
 
10
  def get_current_time():
11
  now = datetime.now()
12
+ current_time = now.strftime("%Y-%m-%d %H:%M:%S")
13
+ return current_time
 
 
14
 
15
+ def load_model(model_str):
16
+ if model_str not in models_load:
17
+ try:
18
+ m = gr.load(f'models/{model_str}')
19
+ logging.info(f"Model {model_str} loaded successfully.")
20
+ except Exception as error:
21
+ logging.error(f"Failed to load model {model_str}: {error}")
22
+ m = gr.Interface(lambda txt: None, ['text'], ['image'])
23
+ models_load[model_str] = m
24
+ return models_load[model_str]
25
 
26
+ models_load = {}
27
 
28
  num_models = len(models)
29
  default_models = models[:num_models]
 
43
  while retries < max_retries:
44
  try:
45
  noise = str(randint(0, 9999999))
46
+ model = load_model(model_str)
47
+ result = model(f'{prompt} {noise}')
48
+ logging.info(f"Image generated successfully with model {model_str} and prompt '{prompt}'.")
49
  return result
50
+ except TimeoutError as te:
51
+ logging.error(f"Timeout error with model {model_str} and prompt '{prompt}': {te}")
52
+ except ConnectionError as ce:
53
+ logging.error(f"Connection error with model {model_str} and prompt '{prompt}': {ce}")
54
  except Exception as e:
55
+ logging.error(f"General error with model {model_str} and prompt '{prompt}': {e}")
56
+
57
+ retries += 1
58
+ if retries >= max_retries:
59
+ logging.error(f"Failed to generate image after {max_retries} retries with model {model_str} and prompt '{prompt}'.")
60
+ raise Exception(f"Failed to generate image after {max_retries} retries.")
61
 
62
  return None
63
 
 
247
  with gr.Blocks(css=custom_css) as demo:
248
  make_me()
249
 
250
+ demo.queue(concurrency_count=29) # Adjust this value based on your resources
251
  demo.launch()