soiz commited on
Commit
be673e5
1 Parent(s): f477a0b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +11 -18
app.py CHANGED
@@ -14,22 +14,13 @@ from PIL import Image
14
 
15
  app = Flask(__name__)
16
 
17
- def load_fn(models):
18
- global models_load
19
- models_load = {}
20
-
21
- for model in models:
22
- if model not in models_load.keys():
23
- try:
24
- m = gr.Interface.load(f'models/{model}')
25
- models_load[model] = m
26
- print(f"Loaded model: {model}")
27
- except Exception as error:
28
- print(f"Error loading model {model}: {error}")
29
- m = gr.Interface(lambda txt: None, ['text'], ['image'])
30
- models_load[model] = m
31
-
32
- load_fn(models)
33
 
34
  num_models = 6
35
  default_models = models[:num_models]
@@ -46,10 +37,12 @@ def gen_fn(model_str, prompt, negative_prompt=None):
46
  return None
47
  noise = str('') # Optional: str(randint(0, 99999999999))
48
  try:
 
 
49
  full_prompt = f'{prompt} {noise}'
50
  if negative_prompt:
51
  full_prompt += f' -{negative_prompt}'
52
- result = models_load[model_str](full_prompt)
53
  # Check if result is an image or a file path
54
  if isinstance(result, str): # Assuming result might be a file path
55
  if os.path.exists(result):
@@ -72,7 +65,7 @@ def home():
72
  model = request.args.get('model', default_models[0] if default_models else 'NA')
73
  negative_prompt = request.args.get('Nprompt', None)
74
 
75
- if not model or model not in models_load:
76
  return f'Invalid model: {model}', 400
77
 
78
  if prompt:
 
14
 
15
  app = Flask(__name__)
16
 
17
+ def load_model(model_name):
18
+ try:
19
+ # Load model within Gradio context
20
+ return gr.Interface.load(f'models/{model_name}')
21
+ except Exception as error:
22
+ print(f"Error loading model {model_name}: {error}")
23
+ return gr.Interface(lambda txt: None, ['text'], ['image'])
 
 
 
 
 
 
 
 
 
24
 
25
  num_models = 6
26
  default_models = models[:num_models]
 
37
  return None
38
  noise = str('') # Optional: str(randint(0, 99999999999))
39
  try:
40
+ # Load model within Gradio context
41
+ model = load_model(model_str)
42
  full_prompt = f'{prompt} {noise}'
43
  if negative_prompt:
44
  full_prompt += f' -{negative_prompt}'
45
+ result = model(full_prompt)
46
  # Check if result is an image or a file path
47
  if isinstance(result, str): # Assuming result might be a file path
48
  if os.path.exists(result):
 
65
  model = request.args.get('model', default_models[0] if default_models else 'NA')
66
  negative_prompt = request.args.get('Nprompt', None)
67
 
68
+ if not model:
69
  return f'Invalid model: {model}', 400
70
 
71
  if prompt: