phenomenon1981 commited on
Commit
54174b0
1 Parent(s): 6e84704

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +89 -28
app.py CHANGED
@@ -3,43 +3,104 @@ import os
3
  import sys
4
  import random
5
  from pathlib import Path
 
6
 
7
- # Load all models at once
8
- proc1 = gr.Interface.load("models/dreamlike-art/dreamlike-photoreal-2.0")
9
- proc2 = gr.Interface.load("models/dreamlike-art/dreamlike-diffusion-1.0")
 
 
 
 
 
 
 
10
 
11
- # Load text generation model
12
- text_gen = gr.Interface.load("spaces/Omnibus/MagicPrompt-Stable-Diffusion")
 
 
 
 
 
 
13
 
14
- def generate_images(prompt_text):
15
- # generate prompts
16
- prompts = text_gen(prompt_text)
17
-
18
- # run both models on all prompts
19
- output1 = proc1(prompts)
20
- output2 = proc2(prompts)
21
-
22
- return output1, output2
23
 
24
- with gr.Blocks(title="DreamlikeArt Generator") as myface:
25
- with gr.Row(title="Instructions", padding=20):
26
- gr.Label("Enter a short prompt below and click 'Magic Prompt' to generate prettified prompts.")
27
- gr.Label("Then, select the models to run and click 'Launch' to generate images.")
28
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
29
  with gr.Row():
30
  input_text = gr.Textbox(label="Short Prompt")
31
  see_prompts = gr.Button("Magic Prompt")
32
-
33
  with gr.Row():
34
- prompt = gr.Textbox(label="Prettified Prompt", output=True)
35
  run = gr.Button("Launch")
36
-
37
- with gr.Row():
38
- output1 = gr.Image(label="DreamlikeArt PhotoReal", output=True)
39
- output2 = gr.Image(label="DreamlikeArt Diffusion", output=True)
40
-
41
  with gr.Row():
42
- model_select = gr.Checkbox(["DreamlikeArt PhotoReal", "DreamlikeArt Diffusion"])
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
43
 
44
- see_prompts.click(text_gen, inputs=[input_text], outputs=[prompt])
45
- run.click(generate_images, inputs=[prompt], outputs=[output1, output2], control_inputs=[model_select], control_outputs=[output1, output2])
 
3
  import sys
4
  import random
5
  from pathlib import Path
6
+ from concurrent.futures import ThreadPoolExecutor
7
 
8
+ #Load the models and the text generation model
9
+ text_gen=gr.Interface.load("spaces/Omnibus/MagicPrompt-Stable-Diffusion")
10
+ proc1=gr.Interface.load("models/dreamlike-art/dreamlike-photoreal-2.0")
11
+ proc2=gr.Interface.load("models/dreamlike-art/dreamlike-photoreal-2.0")
12
+ proc3=gr.Interface.load("models/dreamlike-art/dreamlike-photoreal-2.0")
13
+ proc4=gr.Interface.load("models/dreamlike-art/dreamlike-photoreal-2.0")
14
+ proc5=gr.Interface.load("models/dreamlike-art/dreamlike-diffusion-1.0")
15
+ proc6=gr.Interface.load("models/dreamlike-art/dreamlike-diffusion-1.0")
16
+ proc7=gr.Interface.load("models/dreamlike-art/dreamlike-diffusion-1.0")
17
+ proc8=gr.Interface.load("models/dreamlike-art/dreamlike-diffusion-1.0")
18
 
19
+ #Create a function to handle the errors
20
+ def handle_errors(func):
21
+ def wrapper(*args, **kwargs):
22
+ try:
23
+ return func(*args, **kwargs)
24
+ except Exception as e:
25
+ return "An error occurred: " + str(e)
26
+ return wrapper
27
 
 
 
 
 
 
 
 
 
 
28
 
29
+ # Define the functions for each model
30
+ @handle_errors
31
+ def send_it1(inputs):
32
+ return proc1(inputs)
33
 
34
+ @handle_errors
35
+ def send_it2(inputs):
36
+ return proc2(inputs)
37
+
38
+ @handle_errors
39
+ def send_it3(inputs):
40
+ return proc3(inputs)
41
+
42
+ @handle_errors
43
+ def send_it4(inputs):
44
+ return proc4(inputs)
45
+
46
+ @handle_errors
47
+ def send_it5(inputs):
48
+ return proc5(inputs)
49
+
50
+ @handle_errors
51
+ def send_it6(inputs):
52
+ return proc6(inputs)
53
+
54
+ @handle_errors
55
+ def send_it7(inputs):
56
+ return proc7(inputs)
57
+
58
+ @handle_errors
59
+ def send_it8(inputs):
60
+ return proc8(inputs)
61
+
62
+
63
+ # Create a function to generate the prompts
64
+ @handle_errors
65
+ def get_prompts(prompt_text):
66
+ return text_gen(prompt_text)
67
+
68
+ with gr.Blocks() as myface:
69
  with gr.Row():
70
  input_text = gr.Textbox(label="Short Prompt")
71
  see_prompts = gr.Button("Magic Prompt")
72
+
73
  with gr.Row():
74
+ prompt = gr.Textbox(label="Prettified Prompt")
75
  run = gr.Button("Launch")
76
+
 
 
 
 
77
  with gr.Row():
78
+ output1 = gr.Image(label="DreamlikeArt PhotoReal 1")
79
+ output2 = gr.Image(label="DreamlikeArt PhotoReal 2")
80
+ output3 = gr.Image(label="DreamlikeArt PhotoReal 3")
81
+ output4 = gr.Image(label="DreamlikeArt PhotoReal 4")
82
+
83
+ with gr.Row():
84
+ output5 = gr.Image(label="DreamlikeArt Diffusion 1")
85
+ output6 = gr.Image(label="DreamlikeArt Diffusion 2")
86
+ output7 = gr.Image(label="DreamlikeArt Diffusion 3")
87
+ output8 = gr.Image(label="DreamlikeArt Diffusion 4")
88
+
89
+ # Handle button clicks
90
+ see_prompts.click(get_prompts, inputs=[input_text], outputs=[prompt])
91
+
92
+ # Use a thread pool to run the models in parallel
93
+ executor = ThreadPoolExecutor()
94
+ run.click(lambda : [executor.submit(send_it1, prompt),
95
+ executor.submit(send_it2, prompt),
96
+ executor.submit(send_it3, prompt),
97
+ executor.submit(send_it4, prompt),
98
+ executor.submit(send_it5, prompt),
99
+ executor.submit(send_it6, prompt),
100
+ executor.submit(send_it7, prompt),
101
+ executor.submit(send_it8, prompt)
102
+ ],
103
+ inputs=[prompt],
104
+ outputs=[output1, output2, output3, output4, output5, output6, output7, output8])
105
+
106