feat(api): improved model install confirmation page styling & messaging

This commit is contained in:
psychedelicious 2024-06-17 10:51:08 +10:00
parent f002bca2fa
commit cd70937b7f

View File

@ -518,46 +518,89 @@ async def install_hugging_face_model(
) -> HTMLResponse: ) -> HTMLResponse:
"""Install a Hugging Face model using a string identifier.""" """Install a Hugging Face model using a string identifier."""
def generate_html(message: str) -> str: def generate_html(title: str, heading: str, repo_id: str, is_error: bool, message: str | None = "") -> str:
if message:
message = f"<p>{message}</p>"
title_class = "error" if is_error else "success"
return f""" return f"""
<html> <html>
<head> <head>
<title>{title}</title>
<style> <style>
body {{ body {{
text-align: center; text-align: center;
margin-top: 50px; background-color: hsl(220 12% 10% / 1);
font-family: Helvetica, sans-serif;
color: hsl(220 12% 86% / 1);
}} }}
.repo-id {{
color: hsl(220 12% 68% / 1);
}}
.error {{
color: hsl(0 42% 68% / 1)
}}
.message-box {{ .message-box {{
display: inline-block; display: inline-block;
padding: 20px;
border: 1px solid #ccc;
border-radius: 5px; border-radius: 5px;
background-color: #f9f9f9; background-color: hsl(220 12% 20% / 1);
box-shadow: 0px 0px 10px rgba(0, 0, 0, 0.1); padding-inline-end: 30px;
padding: 20px;
padding-inline-start: 30px;
padding-inline-end: 30px;
}}
.container {{
display: flex;
width: 100%;
height: 100%;
align-items: center;
justify-content: center;
}}
a {{
color: inherit
}}
a:visited {{
color: inherit
}}
a:active {{
color: inherit
}} }}
</style> </style>
</head> </head>
<body>
<body style="background-color: hsl(220 12% 10% / 1);">
<div class="container">
<div class="message-box"> <div class="message-box">
<p>{message}</p> <h2 class="{title_class}">{heading}</h2>
{message}
<p class="repo-id">Repo ID: {repo_id}</p>
</div>
</div> </div>
</body> </body>
</html> </html>
""" """
try: try:
metadata = HuggingFaceMetadataFetch().from_id(source) metadata = HuggingFaceMetadataFetch().from_id(source)
assert isinstance(metadata, ModelMetadataWithFiles) assert isinstance(metadata, ModelMetadataWithFiles)
message = "Your Hugging Face model is installing now. You can close this tab and check the Model Manager for installation progress."
except UnknownMetadataException: except UnknownMetadataException:
message = "No HuggingFace repository found with that repo id." title = "Unable to Install Model"
return HTMLResponse(content=generate_html(message), status_code=400) heading = "No HuggingFace repository found with that repo ID."
message = "Ensure the repo ID is correct and try again."
return HTMLResponse(content=generate_html(title, heading, source, True, message), status_code=400)
logger = ApiDependencies.invoker.services.logger logger = ApiDependencies.invoker.services.logger
try: try:
installer = ApiDependencies.invoker.services.model_manager.install installer = ApiDependencies.invoker.services.model_manager.install
if metadata.is_diffusers: if metadata.is_diffusers:
installer.heuristic_import( installer.heuristic_import(
source=source, source=source,
@ -569,12 +612,21 @@ async def install_hugging_face_model(
inplace=False, inplace=False,
) )
else: else:
message = "This HuggingFace repo has multiple models. Please use the Model Manager to install this." title = "Unable to Install Model"
heading = "This HuggingFace repo has multiple models."
message = "Please use the Model Manager to install this model."
return HTMLResponse(content=generate_html(title, heading, source, True, message), status_code=200)
title = "Model Install Started"
heading = "Your HuggingFace model is installing now."
message = "You can close this tab and check the Model Manager for installation progress."
return HTMLResponse(content=generate_html(title, heading, source, False, message), status_code=201)
except Exception as e: except Exception as e:
logger.error(str(e)) logger.error(str(e))
message = "There was an error with installing this model. Please use the Model Manager to install this." title = "Unable to Install Model"
heading = "There was an problem installing this model."
return HTMLResponse(content=generate_html(message), status_code=201) message = 'Please use the Model Manager directly to install this model. If the issue persists, ask for help on <a href="https://discord.gg/ZmtBAhwWhy">discord</a>.'
return HTMLResponse(content=generate_html(title, heading, source, True, message), status_code=500)
@model_manager_router.get( @model_manager_router.get(