Spaces:
Running on Zero
Running on Zero
Add Jeveration workflow bindings
Browse filesBind generate, edit and dense functions to gr.Workflow and mount the canvas at /workflow
app.py
CHANGED
|
@@ -1,7 +1,7 @@
|
|
| 1 |
-
"""Jeveration Studio —
|
| 2 |
|
| 3 |
-
One loaded pipeline: text-to-image, image-to-image and dense prediction,
|
| 4 |
-
gr.Server API
|
| 5 |
|
| 6 |
python app.py # weights from the Hugging Face Hub, studio on port 7860
|
| 7 |
"""
|
|
@@ -201,6 +201,24 @@ def _open_rgb(value):
|
|
| 201 |
return ImageOps.exif_transpose(Image.open(_local_path(value))).convert("RGB")
|
| 202 |
|
| 203 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 204 |
def _run(fn, *args):
|
| 205 |
"""One GPU call with the studio's error surface: messages, not stack traces."""
|
| 206 |
try:
|
|
@@ -224,6 +242,31 @@ def _dense(image):
|
|
| 224 |
return _dense_run(image)
|
| 225 |
|
| 226 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 227 |
# ------------------------------------------------------------------ studio server
|
| 228 |
|
| 229 |
server = gr.Server(title="Jeveration Studio")
|
|
@@ -304,6 +347,19 @@ try:
|
|
| 304 |
except Exception:
|
| 305 |
pass
|
| 306 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 307 |
if __name__ == "__main__":
|
| 308 |
import uvicorn
|
| 309 |
uvicorn.run(server, host="0.0.0.0", port=int(os.environ.get("GRADIO_SERVER_PORT", "7860")))
|
|
|
|
| 1 |
+
"""Jeveration Studio — project-page-inspired gr.Server studio + gr.Workflow canvas.
|
| 2 |
|
| 3 |
+
One loaded pipeline: text-to-image, image-to-image and dense prediction, available through
|
| 4 |
+
gr.Server API endpoints at / and a chainable gr.Workflow canvas at /workflow.
|
| 5 |
|
| 6 |
python app.py # weights from the Hugging Face Hub, studio on port 7860
|
| 7 |
"""
|
|
|
|
| 201 |
return ImageOps.exif_transpose(Image.open(_local_path(value))).convert("RGB")
|
| 202 |
|
| 203 |
|
| 204 |
+
def _publish(pixels) -> str:
|
| 205 |
+
"""Write a result into Gradio's upload folder so the workflow canvas can display and chain it."""
|
| 206 |
+
folder = get_upload_folder()
|
| 207 |
+
os.makedirs(folder, exist_ok=True)
|
| 208 |
+
path = os.path.join(folder, f"jeveration_{os.urandom(8).hex()}.png")
|
| 209 |
+
Image.fromarray(np.asarray(pixels, dtype=np.uint8)).save(path, format="PNG")
|
| 210 |
+
return path
|
| 211 |
+
|
| 212 |
+
|
| 213 |
+
def _as_media(path: str) -> dict:
|
| 214 |
+
"""The media shape used by gr.Workflow image inputs and outputs."""
|
| 215 |
+
return {
|
| 216 |
+
"path": path,
|
| 217 |
+
"url": f"/gradio_api/file={client_utils.encode_file_path(path)}",
|
| 218 |
+
"is_file": True,
|
| 219 |
+
}
|
| 220 |
+
|
| 221 |
+
|
| 222 |
def _run(fn, *args):
|
| 223 |
"""One GPU call with the studio's error surface: messages, not stack traces."""
|
| 224 |
try:
|
|
|
|
| 242 |
return _dense_run(image)
|
| 243 |
|
| 244 |
|
| 245 |
+
# ------------------------------------------------------------------ workflow fns
|
| 246 |
+
|
| 247 |
+
|
| 248 |
+
def jeveration_generate(prompt, seed: str = "42") -> dict:
|
| 249 |
+
"""Generate one 512 x 512 image from text."""
|
| 250 |
+
seed_value = min(max(int(float(seed)), 0), MAX_SEED)
|
| 251 |
+
image, _ = _run(_generate, prompt, seed_value)
|
| 252 |
+
return _as_media(_publish(image))
|
| 253 |
+
|
| 254 |
+
|
| 255 |
+
def jeveration_edit(image, task: str = "anime", caption: str = "", seed: str = "42") -> dict:
|
| 256 |
+
"""Apply structure-to-image, colorization or style transfer to an image."""
|
| 257 |
+
_check_choice(task, I2I_TASKS, "Task")
|
| 258 |
+
source = _open_rgb(image)
|
| 259 |
+
seed_value = min(max(int(float(seed)), 0), MAX_SEED)
|
| 260 |
+
_, result, _ = _run(_edit, source, task, caption, False, seed_value)
|
| 261 |
+
return _as_media(_publish(result))
|
| 262 |
+
|
| 263 |
+
|
| 264 |
+
def jeveration_dense(image) -> tuple[dict, dict, dict]:
|
| 265 |
+
"""Return segmentation, depth and surface normals for an image."""
|
| 266 |
+
seg, depth, normal, _ = _run(_dense, _open_rgb(image))
|
| 267 |
+
return tuple(_as_media(_publish(result)) for result in (seg, depth, normal))
|
| 268 |
+
|
| 269 |
+
|
| 270 |
# ------------------------------------------------------------------ studio server
|
| 271 |
|
| 272 |
server = gr.Server(title="Jeveration Studio")
|
|
|
|
| 347 |
except Exception:
|
| 348 |
pass
|
| 349 |
|
| 350 |
+
# Mount only after the registration launch above, so the workflow queue stays on
|
| 351 |
+
# the public server's event loop. The starter graph chains Generate -> Edit -> Dense.
|
| 352 |
+
workflow_demo = gr.Workflow(
|
| 353 |
+
graph="workflow.json",
|
| 354 |
+
bind={
|
| 355 |
+
"jeveration_generate": jeveration_generate,
|
| 356 |
+
"jeveration_edit": jeveration_edit,
|
| 357 |
+
"jeveration_dense": jeveration_dense,
|
| 358 |
+
},
|
| 359 |
+
)
|
| 360 |
+
workflow_demo.title = "Jeveration Workflow"
|
| 361 |
+
server = gr.mount_gradio_app(server, workflow_demo, path="/workflow", ssr_mode=False)
|
| 362 |
+
|
| 363 |
if __name__ == "__main__":
|
| 364 |
import uvicorn
|
| 365 |
uvicorn.run(server, host="0.0.0.0", port=int(os.environ.get("GRADIO_SERVER_PORT", "7860")))
|