Spaces:
Sleeping
Sleeping
File size: 5,251 Bytes
25fdc62 2f5363d 25fdc62 2f5363d 25fdc62 2f5363d 25fdc62 2f5363d 25fdc62 2f5363d 25fdc62 2f5363d 9d02dac 97562cd 2f5363d de84137 6bd9d57 0d58bef 6bd9d57 2f5363d 25fdc62 2f5363d 25fdc62 2f5363d 6bd9d57 2f5363d 25fdc62 6bd9d57 2f5363d 6bd9d57 2f5363d 6bd9d57 2f5363d 25fdc62 2f5363d 25fdc62 2f5363d 25fdc62 2f5363d 6bd9d57 2f5363d 6bd9d57 2f5363d 6bd9d57 2f5363d 25fdc62 2f5363d 25fdc62 2f5363d 25fdc62 2f5363d 6bd9d57 25fdc62 2f5363d 9d02dac |
1 2 3 4 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 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 |
# === Chosen SDK === #
import gradio as gr
# === requirements.txt === #
from PIL import Image
import numpy as np
import torch
import cv2
# === Python built-in === #
from io import BytesIO
import base64
import sys
import os
# =========================================================== #
# Q: Why use gr.Blocks instead of gr.Interface? #
# A: Only Blocks supports Layouts (and I'm more used to this) #
# =========================================================== #
with gr.Blocks() as demo:
# ======================================== #
# Markdown is pointless in API usage... XD #
# ======================================== #
gr.Markdown("""<h1 align="center">Hello World</h1>""")
gr.Markdown("""<p align="center">For <b>learning</b> purposes only...</p>""")
# ======================================= #
# function name is overridden by api_name #
# ======================================= #
def whatever() -> str:
info: list[str] = []
info.append(f"OS: {os.name}")
info.append(f"Python: {sys.version.split(' ', 1)[0]}")
info.append(f"CUDA: {torch.cuda.is_available()}")
info.append(f"Open-CV: {cv2.__version__}")
info.append(f"torch: {getattr(torch, 'float16', False)}")
info.append(f"np: {getattr(np, 'float16', False)}")
return "\n".join(info)
# ===================================================== #
# parameter name is also shown as parameter name in API #
# ===================================================== #
def proc_image(image: str, width: int, height: int) -> list[str, Image.Image]:
assert isinstance(image, str)
# ==================================== #
# This works with base64 image via API #
# ==================================== #
image = Image.open(BytesIO(base64.b64decode(image)))
if isinstance(width, float):
width = int(width)
if isinstance(height, float):
height = int(height)
input_w, input_h = image.size
log = f"Resize a {input_w}x{input_h} image to {width}x{height}..."
image = image.convert("RGB").resize((width, height))
with BytesIO() as buffer:
image.save(buffer, format="PNG")
b64_image = base64.b64encode(buffer.getvalue()).decode()
return [log, b64_image]
with gr.Row():
with gr.Column():
# ==================================== #
# variable name does not matter in API #
# ==================================== #
foo1 = gr.Text(
value="",
label="Input Image",
show_label=True,
info="Image in base64",
visible=True,
interactive=True,
lines=1,
max_lines=1,
)
# =============================================== #
# Only value matters (shown as the Default value) #
# =============================================== #
with gr.Row():
foo2 = gr.Slider(
minimum=1,
maximum=1024,
step=1,
value=512,
label="width",
show_label=True,
interactive=True,
)
foo3 = gr.Slider(
minimum=1,
maximum=1024,
step=1,
value=512,
label="width",
show_label=True,
interactive=True,
)
bar = gr.Text(
value="",
label="Output Image",
show_label=True,
info="Image in base64",
visible=True,
interactive=False,
lines=1,
max_lines=1,
)
yeet1 = gr.Button("Resize")
with gr.Column():
# ============================== #
# Button does not show up in API #
# ============================== #
yeet2 = gr.Button("Print Info")
# ==================================== #
# label is shown in the returns in API #
# ==================================== #
console_logs = gr.Text(
value="...",
label="Logs",
show_label=True,
lines=1,
max_lines=8,
interactive=False,
)
# =================================== #
# api_name is used as endpoint in API #
# =================================== #
yeet2.click(
fn=whatever,
inputs=None,
outputs=console_logs,
show_progress="hidden",
show_api=True,
api_name="systemInfo",
)
# =============================================== #
# API order is based on event declaration in code #
# =============================================== #
yeet1.click(
fn=proc_image,
inputs=[foo1, foo2, foo3],
outputs=[console_logs, bar],
show_progress="hidden",
show_api=True,
api_name="resizeImage",
)
demo.launch(show_error=True)
|