Allow password query API access
Browse files
app.py
CHANGED
|
@@ -267,14 +267,20 @@ def gpu_duration(prompt, width, height, steps, p=None):
|
|
| 267 |
return min(120, max(10, int(work / 4) + 6))
|
| 268 |
|
| 269 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 270 |
@spaces.GPU(duration=gpu_duration)
|
| 271 |
-
def generate(prompt, width=1152, height=896, steps=16):
|
|
|
|
|
|
|
| 272 |
return generate_core(prompt, width, height, steps)
|
| 273 |
|
| 274 |
|
| 275 |
@spaces.GPU(duration=gpu_duration)
|
| 276 |
def api_generate(prompt, width=1152, height=896, steps=16, p=""):
|
| 277 |
-
if not
|
| 278 |
raise gr.Error("Invalid password")
|
| 279 |
return generate_core(prompt, width, height, steps)
|
| 280 |
|
|
@@ -282,7 +288,7 @@ def api_generate(prompt, width=1152, height=896, steps=16, p=""):
|
|
| 282 |
def queued_generate(prompt, width, height, steps):
|
| 283 |
from gradio_client import Client
|
| 284 |
|
| 285 |
-
client = Client(LOCAL_URL,
|
| 286 |
result = client.predict(prompt, width, height, steps, PASSWORD, api_name="/generate")
|
| 287 |
return Image.open(result).copy()
|
| 288 |
|
|
@@ -339,7 +345,7 @@ api.add_middleware(CORSMiddleware, allow_origins=["*"], allow_methods=["*"], all
|
|
| 339 |
|
| 340 |
|
| 341 |
def require_pass(p: str = Query(...)):
|
| 342 |
-
if not
|
| 343 |
raise HTTPException(401, "invalid password")
|
| 344 |
|
| 345 |
|
|
@@ -409,23 +415,37 @@ def direct(body: DirectRequest):
|
|
| 409 |
return FileResponse(path, media_type="image/png", filename=filename)
|
| 410 |
|
| 411 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 412 |
with gr.Blocks(title="Image API") as demo:
|
| 413 |
-
gr.
|
| 414 |
-
|
| 415 |
-
|
| 416 |
-
|
| 417 |
-
|
| 418 |
-
|
| 419 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 420 |
api_button = gr.Button(visible=False)
|
| 421 |
-
|
| 422 |
-
|
|
|
|
|
|
|
|
|
|
| 423 |
button.click(
|
| 424 |
-
generate, [prompt_input, width_input, height_input, steps_input], output,
|
| 425 |
api_visibility="private",
|
| 426 |
)
|
| 427 |
api_button.click(
|
| 428 |
-
api_generate, [prompt_input, width_input, height_input, steps_input,
|
| 429 |
api_name="generate",
|
| 430 |
)
|
| 431 |
|
|
@@ -440,7 +460,5 @@ if __name__ == "__main__":
|
|
| 440 |
server_port=PORT,
|
| 441 |
css_paths="style.css",
|
| 442 |
head='<link rel="icon" href="data:;base64,=">',
|
| 443 |
-
auth=("user", PASSWORD),
|
| 444 |
-
auth_message="Username: user",
|
| 445 |
_app=api,
|
| 446 |
)
|
|
|
|
| 267 |
return min(120, max(10, int(work / 4) + 6))
|
| 268 |
|
| 269 |
|
| 270 |
+
def valid_pass(p):
|
| 271 |
+
return secrets.compare_digest(str(p), PASSWORD)
|
| 272 |
+
|
| 273 |
+
|
| 274 |
@spaces.GPU(duration=gpu_duration)
|
| 275 |
+
def generate(prompt, width=1152, height=896, steps=16, p=""):
|
| 276 |
+
if not valid_pass(p):
|
| 277 |
+
raise gr.Error("Invalid password")
|
| 278 |
return generate_core(prompt, width, height, steps)
|
| 279 |
|
| 280 |
|
| 281 |
@spaces.GPU(duration=gpu_duration)
|
| 282 |
def api_generate(prompt, width=1152, height=896, steps=16, p=""):
|
| 283 |
+
if not valid_pass(p):
|
| 284 |
raise gr.Error("Invalid password")
|
| 285 |
return generate_core(prompt, width, height, steps)
|
| 286 |
|
|
|
|
| 288 |
def queued_generate(prompt, width, height, steps):
|
| 289 |
from gradio_client import Client
|
| 290 |
|
| 291 |
+
client = Client(LOCAL_URL, verbose=False)
|
| 292 |
result = client.predict(prompt, width, height, steps, PASSWORD, api_name="/generate")
|
| 293 |
return Image.open(result).copy()
|
| 294 |
|
|
|
|
| 345 |
|
| 346 |
|
| 347 |
def require_pass(p: str = Query(...)):
|
| 348 |
+
if not valid_pass(p):
|
| 349 |
raise HTTPException(401, "invalid password")
|
| 350 |
|
| 351 |
|
|
|
|
| 415 |
return FileResponse(path, media_type="image/png", filename=filename)
|
| 416 |
|
| 417 |
|
| 418 |
+
def login(p):
|
| 419 |
+
if not valid_pass(p):
|
| 420 |
+
raise gr.Error("Invalid password")
|
| 421 |
+
return gr.Column(visible=False), gr.Column(visible=True)
|
| 422 |
+
|
| 423 |
+
|
| 424 |
with gr.Blocks(title="Image API") as demo:
|
| 425 |
+
with gr.Column() as login_panel:
|
| 426 |
+
pass_input = gr.Textbox(label="Password", type="password")
|
| 427 |
+
login_button = gr.Button("Login", variant="primary")
|
| 428 |
+
with gr.Column(visible=False) as generate_panel:
|
| 429 |
+
gr.HTML("<model-note>ComfyUI-compatible API and direct image generation</model-note>")
|
| 430 |
+
prompt_input = gr.Textbox(label="Prompt", lines=5)
|
| 431 |
+
with gr.Row():
|
| 432 |
+
width_input = gr.Number(1152, label="Width", precision=0)
|
| 433 |
+
height_input = gr.Number(896, label="Height", precision=0)
|
| 434 |
+
steps_input = gr.Slider(1, 100, 16, step=1, label="Steps")
|
| 435 |
+
button = gr.Button("Generate", variant="primary")
|
| 436 |
+
output = gr.Image(label="Image", format="png")
|
| 437 |
api_button = gr.Button(visible=False)
|
| 438 |
+
api_pass_input = gr.Textbox(visible=False)
|
| 439 |
+
login_button.click(
|
| 440 |
+
login, pass_input, [login_panel, generate_panel], queue=False,
|
| 441 |
+
api_visibility="private",
|
| 442 |
+
)
|
| 443 |
button.click(
|
| 444 |
+
generate, [prompt_input, width_input, height_input, steps_input, pass_input], output,
|
| 445 |
api_visibility="private",
|
| 446 |
)
|
| 447 |
api_button.click(
|
| 448 |
+
api_generate, [prompt_input, width_input, height_input, steps_input, api_pass_input], output,
|
| 449 |
api_name="generate",
|
| 450 |
)
|
| 451 |
|
|
|
|
| 460 |
server_port=PORT,
|
| 461 |
css_paths="style.css",
|
| 462 |
head='<link rel="icon" href="data:;base64,=">',
|
|
|
|
|
|
|
| 463 |
_app=api,
|
| 464 |
)
|