add inbrowser

This commit is contained in:
Qing 2024-02-08 17:35:55 +08:00
parent f52dbc1091
commit 45d29a4249
4 changed files with 19 additions and 1 deletions

View File

@ -1,3 +1,5 @@
import webbrowser
from contextlib import asynccontextmanager
from pathlib import Path
from typing import Dict, Optional
@ -97,6 +99,7 @@ def run(
def start(
host: str = Option("127.0.0.1"),
port: int = Option(8080),
inbrowser: bool = Option(False, help=INBROWSER_HELP),
model: str = Option(
DEFAULT_MODEL,
help=f"Erase models: [{', '.join(AVAILABLE_MODELS)}].\n"
@ -165,10 +168,18 @@ def start(
from iopaint.api import Api
from iopaint.schema import ApiConfig
app = FastAPI()
@asynccontextmanager
async def lifespan(app: FastAPI):
if inbrowser:
webbrowser.open(f"http://localhost:{port}", new=0, autoraise=True)
yield
app = FastAPI(lifespan=lifespan)
api_config = ApiConfig(
host=host,
port=port,
inbrowser=inbrowser,
model=model,
no_half=no_half,
low_mem=low_mem,

View File

@ -116,3 +116,5 @@ REALESRGAN_HELP = "Enable realesrgan super resolution"
GFPGAN_HELP = "Enable GFPGAN face restore. To also enhance background, use with --enable-realesrgan"
RESTOREFORMER_HELP = "Enable RestoreFormer face restore. To also enhance background, use with --enable-realesrgan"
GIF_HELP = "Enable GIF plugin. Make GIF to compare original and cleaned image"
INBROWSER_HELP = "Automatically launch IOPaint in a new tab on the default browser"

View File

@ -216,6 +216,7 @@ class PowerPaintTask(str, Enum):
class ApiConfig(BaseModel):
host: str
port: int
inbrowser: bool
model: str
no_half: bool
low_mem: bool

View File

@ -28,6 +28,7 @@ _config_file: Path = None
default_configs = dict(
host="127.0.0.1",
port=8080,
inbrowser=True,
model=DEFAULT_MODEL,
model_dir=DEFAULT_MODEL_DIR,
no_half=False,
@ -100,6 +101,7 @@ def save_config(
gfpgan_device,
enable_restoreformer,
restoreformer_device,
inbrowser,
):
config = WebConfig(**locals())
if str(config.input) == ".":
@ -146,6 +148,7 @@ def main(config_file: Path):
with gr.Row():
host = gr.Textbox(init_config.host, label="Host")
port = gr.Number(init_config.port, label="Port", precision=0)
inbrowser = gr.Checkbox(init_config.inbrowser, label=INBROWSER_HELP)
with gr.Column():
model = gr.Textbox(
@ -297,6 +300,7 @@ def main(config_file: Path):
gfpgan_device,
enable_restoreformer,
restoreformer_device,
inbrowser,
],
message,
)