Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import os
- import time
- import torch
- import torch.nn as nn
- import torch.optim as optim
- import torch.nn.utils.prune as prune
- from modules import timer
- from modules import initialize_util
- from modules import initialize
- startup_timer = timer.startup_timer
- startup_timer.record("launcher")
- initialize.imports()
- initialize.check_versions()
- def optimize_model(model):
- # Prune unnecessary connections from the model
- prune.l1_unstructured(model.fc1, name='weight', amount=0.5)
- # Split the model into smaller modules
- model_chunk1 = nn.Sequential(model.fc1, model.relu)
- model_chunk2 = model.fc2
- # Share parameters between layers of the model
- model.fc2.weight = nn.Parameter(model.fc1.weight.clone())
- model.fc2.bias = nn.Parameter(model.fc1.bias.clone())
- def create_model(input_dim, hidden_dim, output_dim):
- # Define a simple neural network architecture
- model = SimpleNet(input_dim, hidden_dim, output_dim)
- # Optimize the model for memory usage
- optimize_model(model)
- return model
- def process_prompt(prompt):
- # Process the prompt here
- pass
- def webui():
- from modules.shared_cmd_options import cmd_opts
- launch_api = cmd_opts.api
- initialize.initialize()
- from modules import shared, ui_tempdir, script_callbacks, ui, progress, ui_extra_networks
- if shared.opts.clean_temp_dir_at_start:
- ui_tempdir.cleanup_tmpdr()
- startup_timer.record("cleanup temp dir")
- script_callbacks.before_ui_callback()
- startup_timer.record("scripts before_ui_callback")
- if not cmd_opts.no_gradio_queue:
- shared.demo.queue(64)
- gradio_auth_creds = list(initialize_util.get_gradio_auth_creds()) or None
- auto_launch_browser = False
- if os.getenv('SD_WEBUI_RESTARTING') != '1':
- if shared.opts.auto_launch_browser == "Remote" or cmd_opts.autolaunch:
- auto_launch_browser = True
- elif shared.opts.auto_launch_browser == "Local":
- auto_launch_browser = not cmd_opts.webui_is_non_local
- app, local_url, share_url = shared.demo.launch(
- share=cmd_opts.share,
- server_name=initialize_util.gradio_server_name(),
- server_port=cmd_opts.port,
- ssl_keyfile=cmd_opts.tls_keyfile,
- ssl_certfile=cmd_opts.tls_certfile,
- ssl_verify=cmd_opts.disable_tls_verify,
- debug=cmd_opts.gradio_debug,
- auth=gradio_auth_creds,
- inbrowser=auto_launch_browser,
- prevent_thread_lock=True,
- allowed_paths=cmd_opts.gradio_allowed_path,
- app_kwargs={
- "docs_url": "/docs",
- "redoc_url": "/redoc",
- },
- root_path=f"/{cmd_opts.subpath}" if cmd_opts.subpath else "",
- )
- startup_timer.record("gradio launch")
- # Remove the CORS middleware to enhance security
- app.user_middleware = [x for x in app.user_middleware if x.cls.__name__ != 'CORSMiddleware']
- initialize_util.setup_middleware(app)
- progress.setup_progress_api(app)
- ui.setup_ui_api(app)
- if launch_api:
- create_api(app)
- ui_extra_networks.add_pages_to_demo(app)
- startup_timer.record("add APIs")
- with startup_timer.subcategory("app_started_callback"):
- script_callbacks.app_started_callback(shared.demo, app)
- timer.startup_record = startup_timer.dump()
- print(f"Startup time: {startup_timer.summary()}.")
- try:
- while True:
- server_command = shared.state.wait_for_server_command(timeout=5)
- if server_command:
- if server_command in ("stop", "restart"):
- break
- else:
- print(f"Unknown server command: {server_command}")
- # Process prompt
- prompt = # Get the prompt data
- process_prompt(prompt)
- except KeyboardInterrupt:
- print('Caught KeyboardInterrupt, stopping...')
- server_command = "stop"
- if server_command == "stop":
- print("Stopping server...")
- # If we catch a keyboard interrupt, we want to stop the server and exit.
- shared.demo.close()
- # disable auto launch webui in browser for subsequent UI Reload
- os.environ.setdefault('SD_WEBUI_RESTARTING', '1')
- print('Restarting UI...')
- shared.demo.close()
- time.sleep(0.5)
- startup_timer.reset()
- script_callbacks.app_reload_callback()
- startup_timer.record("app reload callback")
- script_callbacks.script_unloaded_callback()
- startup_timer.record("scripts unloaded callback")
- initialize.initialize_rest(reload_script_modules=True)
- if __name__ == "__main__":
- from modules.shared_cmd_options import cmd_opts
- if cmd_opts.nowebui:
- api_only()
- else:
- webui()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement