Spaces:
Runtime error
Runtime error
Commit
·
110b988
1
Parent(s):
2a28f67
Update app.py
Browse files
app.py
CHANGED
@@ -1,162 +1,73 @@
|
|
1 |
-
|
2 |
-
|
3 |
-
import
|
4 |
-
import
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
45 |
)
|
46 |
-
|
47 |
-
|
48 |
-
def webui():
|
49 |
-
from modules.shared_cmd_options import cmd_opts
|
50 |
-
|
51 |
-
launch_api = cmd_opts.api
|
52 |
-
initialize.initialize()
|
53 |
-
|
54 |
-
from modules import shared, ui_tempdir, script_callbacks, ui, progress, ui_extra_networks
|
55 |
-
|
56 |
-
while 1:
|
57 |
-
if shared.opts.clean_temp_dir_at_start:
|
58 |
-
ui_tempdir.cleanup_tmpdr()
|
59 |
-
startup_timer.record("cleanup temp dir")
|
60 |
-
|
61 |
-
script_callbacks.before_ui_callback()
|
62 |
-
startup_timer.record("scripts before_ui_callback")
|
63 |
-
|
64 |
-
shared.demo = ui.create_ui()
|
65 |
-
startup_timer.record("create ui")
|
66 |
-
|
67 |
-
if not cmd_opts.no_gradio_queue:
|
68 |
-
shared.demo.queue(64)
|
69 |
-
|
70 |
-
gradio_auth_creds = list(initialize_util.get_gradio_auth_creds()) or None
|
71 |
-
|
72 |
-
auto_launch_browser = False
|
73 |
-
if os.getenv('SD_WEBUI_RESTARTING') != '1':
|
74 |
-
if shared.opts.auto_launch_browser == "Remote" or cmd_opts.autolaunch:
|
75 |
-
auto_launch_browser = True
|
76 |
-
elif shared.opts.auto_launch_browser == "Local":
|
77 |
-
auto_launch_browser = not any([cmd_opts.listen, cmd_opts.share, cmd_opts.ngrok, cmd_opts.server_name])
|
78 |
-
|
79 |
-
app, local_url, share_url = shared.demo.launch(
|
80 |
-
share=cmd_opts.share,
|
81 |
-
server_name=initialize_util.gradio_server_name(),
|
82 |
-
server_port=cmd_opts.port,
|
83 |
-
ssl_keyfile=cmd_opts.tls_keyfile,
|
84 |
-
ssl_certfile=cmd_opts.tls_certfile,
|
85 |
-
ssl_verify=cmd_opts.disable_tls_verify,
|
86 |
-
debug=cmd_opts.gradio_debug,
|
87 |
-
auth=gradio_auth_creds,
|
88 |
-
inbrowser=auto_launch_browser,
|
89 |
-
prevent_thread_lock=True,
|
90 |
-
allowed_paths=cmd_opts.gradio_allowed_path,
|
91 |
-
app_kwargs={
|
92 |
-
"docs_url": "/docs",
|
93 |
-
"redoc_url": "/redoc",
|
94 |
-
},
|
95 |
-
root_path=f"/{cmd_opts.subpath}" if cmd_opts.subpath else "",
|
96 |
-
)
|
97 |
-
|
98 |
-
startup_timer.record("gradio launch")
|
99 |
-
|
100 |
-
# gradio uses a very open CORS policy via app.user_middleware, which makes it possible for
|
101 |
-
# an attacker to trick the user into opening a malicious HTML page, which makes a request to the
|
102 |
-
# running web ui and do whatever the attacker wants, including installing an extension and
|
103 |
-
# running its code. We disable this here. Suggested by RyotaK.
|
104 |
-
app.user_middleware = [x for x in app.user_middleware if x.cls.__name__ != 'CORSMiddleware']
|
105 |
-
|
106 |
-
initialize_util.setup_middleware(app)
|
107 |
-
|
108 |
-
progress.setup_progress_api(app)
|
109 |
-
ui.setup_ui_api(app)
|
110 |
-
|
111 |
-
if launch_api:
|
112 |
-
create_api(app)
|
113 |
-
|
114 |
-
ui_extra_networks.add_pages_to_demo(app)
|
115 |
-
|
116 |
-
startup_timer.record("add APIs")
|
117 |
-
|
118 |
-
with startup_timer.subcategory("app_started_callback"):
|
119 |
-
script_callbacks.app_started_callback(shared.demo, app)
|
120 |
-
|
121 |
-
timer.startup_record = startup_timer.dump()
|
122 |
-
print(f"Startup time: {startup_timer.summary()}.")
|
123 |
-
|
124 |
-
try:
|
125 |
-
while True:
|
126 |
-
server_command = shared.state.wait_for_server_command(timeout=5)
|
127 |
-
if server_command:
|
128 |
-
if server_command in ("stop", "restart"):
|
129 |
-
break
|
130 |
-
else:
|
131 |
-
print(f"Unknown server command: {server_command}")
|
132 |
-
except KeyboardInterrupt:
|
133 |
-
print('Caught KeyboardInterrupt, stopping...')
|
134 |
-
server_command = "stop"
|
135 |
-
|
136 |
-
if server_command == "stop":
|
137 |
-
print("Stopping server...")
|
138 |
-
# If we catch a keyboard interrupt, we want to stop the server and exit.
|
139 |
-
shared.demo.close()
|
140 |
-
break
|
141 |
-
|
142 |
-
# disable auto launch webui in browser for subsequent UI Reload
|
143 |
-
os.environ.setdefault('SD_WEBUI_RESTARTING', '1')
|
144 |
-
|
145 |
-
print('Restarting UI...')
|
146 |
-
shared.demo.close()
|
147 |
-
time.sleep(0.5)
|
148 |
-
startup_timer.reset()
|
149 |
-
script_callbacks.app_reload_callback()
|
150 |
-
startup_timer.record("app reload callback")
|
151 |
-
script_callbacks.script_unloaded_callback()
|
152 |
-
startup_timer.record("scripts unloaded callback")
|
153 |
-
initialize.initialize_rest(reload_script_modules=True)
|
154 |
-
|
155 |
|
156 |
if __name__ == "__main__":
|
157 |
-
|
158 |
-
|
159 |
-
if cmd_opts.nowebui:
|
160 |
-
api_only()
|
161 |
-
else:
|
162 |
-
webui()
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import requests
|
3 |
+
import base64
|
4 |
+
from PIL import Image, ImageDraw
|
5 |
+
import io
|
6 |
+
import random
|
7 |
+
|
8 |
+
url = "http://73.255.78.150:7909/sdapi/v1/img2img"
|
9 |
+
session_seed, last_session_seed = None, None
|
10 |
+
|
11 |
+
def generate_image(prompt, steps, reset_seed, increment_seed, image_file=None):
|
12 |
+
global session_seed, last_session_seed
|
13 |
+
|
14 |
+
if reset_seed:
|
15 |
+
last_session_seed, session_seed = session_seed, -1 if session_seed != -1 else None
|
16 |
+
elif not reset_seed and last_session_seed is not None:
|
17 |
+
session_seed = last_session_seed
|
18 |
+
elif session_seed is None:
|
19 |
+
session_seed = random.randint(0, 10000)
|
20 |
+
|
21 |
+
if increment_seed and session_seed != -1:
|
22 |
+
session_seed += 1
|
23 |
+
|
24 |
+
init_image_value = ""
|
25 |
+
if image_file:
|
26 |
+
image_data = image_file.read()
|
27 |
+
encoded_image = base64.b64encode(image_data).decode("utf-8")
|
28 |
+
init_image_value = "data:image/jpeg;base64," + encoded_image
|
29 |
+
|
30 |
+
payload = {
|
31 |
+
"init_image": init_image_value,
|
32 |
+
"prompt": prompt,
|
33 |
+
"steps": steps,
|
34 |
+
# ... rest of the predefined parameters ...
|
35 |
+
}
|
36 |
+
|
37 |
+
response = requests.post(url, json=payload)
|
38 |
+
response_data = response.json()
|
39 |
+
|
40 |
+
try:
|
41 |
+
image = Image.open(io.BytesIO(base64.b64decode(response_data['images'][0])))
|
42 |
+
return image
|
43 |
+
except KeyError:
|
44 |
+
error_message = response_data.get('error', 'Unknown error')
|
45 |
+
error_image = Image.new('RGB', (512, 512), color=(73, 109, 137))
|
46 |
+
d = ImageDraw.Draw(error_image)
|
47 |
+
d.text((10, 10), f"Error: {error_message}", fill=(255, 255, 0))
|
48 |
+
return error_image
|
49 |
+
|
50 |
+
def main_interface():
|
51 |
+
with gr.Blocks() as main_blocks:
|
52 |
+
with gr.Row():
|
53 |
+
img_output = gr.Image(label="Generated Image")
|
54 |
+
with gr.Row():
|
55 |
+
with gr.Column():
|
56 |
+
prompt_input = gr.Textbox(lines=2, placeholder="Imagine something...", label="Prompt")
|
57 |
+
steps_input = gr.Slider(minimum=15, maximum=50, step=15, default=5, label="Steps")
|
58 |
+
image_input = gr.File(label="Upload Image")
|
59 |
+
with gr.Column():
|
60 |
+
reset_seed_input = gr.Checkbox(label="Randomize Composition")
|
61 |
+
increment_seed_input = gr.Checkbox(label="New Composition")
|
62 |
+
|
63 |
+
iface = gr.Interface(
|
64 |
+
fn=generate_image,
|
65 |
+
inputs=[prompt_input, steps_input, reset_seed_input, increment_seed_input, image_input],
|
66 |
+
outputs=[img_output],
|
67 |
+
live=False,
|
68 |
+
layout=main_blocks
|
69 |
)
|
70 |
+
iface.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
71 |
|
72 |
if __name__ == "__main__":
|
73 |
+
main_interface()
|
|
|
|
|
|
|
|
|
|