Spaces:
Sleeping
Sleeping
Tanut
commited on
Commit
·
1fc8d06
1
Parent(s):
c83cd29
Checkconnect
Browse files
app.py
CHANGED
@@ -1,55 +1,108 @@
|
|
1 |
-
import gradio as gr
|
2 |
-
import torch
|
3 |
-
from diffusers import StableDiffusionControlNetPipeline, ControlNetModel
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
4 |
from PIL import Image
|
5 |
-
import
|
6 |
-
from
|
|
|
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 |
-
return
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
with gr.Row():
|
41 |
-
prompt = gr.Textbox(label="Prompt", value="A futuristic poster, high detail")
|
42 |
-
seed = gr.Number(label="Seed (0=random)", value=0)
|
43 |
-
with gr.Row():
|
44 |
-
control = gr.Image(type="pil", label="Control image (e.g., QR or edge map)")
|
45 |
-
steps = gr.Slider(10, 50, 30, step=1, label="Steps")
|
46 |
-
guidance = gr.Slider(1.0, 12.0, 7.5, step=0.1, label="Guidance scale")
|
47 |
-
out = gr.Image(label="Result")
|
48 |
-
|
49 |
-
btn = gr.Button("Generate")
|
50 |
-
btn.click(generate, [prompt, control, guidance, steps, seed], out)
|
51 |
-
|
52 |
-
# Enable simple API use
|
53 |
-
gr.Examples([], inputs=[prompt, control, guidance, steps, seed], outputs=out)
|
54 |
-
|
55 |
-
demo.launch()
|
|
|
1 |
+
# import gradio as gr
|
2 |
+
# import torch
|
3 |
+
# from diffusers import StableDiffusionControlNetPipeline, ControlNetModel
|
4 |
+
# from PIL import Image
|
5 |
+
# import base64
|
6 |
+
# from io import BytesIO
|
7 |
+
|
8 |
+
# # You can change these:
|
9 |
+
# BASE_MODEL = "runwayml/stable-diffusion-v1-5"
|
10 |
+
# CONTROLNET_ID = "lllyasviel/sd-controlnet-canny" # placeholder; change to a QR-focused ControlNet if you have one
|
11 |
+
|
12 |
+
# device = "cuda" if torch.cuda.is_available() else "cpu"
|
13 |
+
|
14 |
+
# controlnet = ControlNetModel.from_pretrained(
|
15 |
+
# CONTROLNET_ID, torch_dtype=torch.float16 if device=="cuda" else torch.float32
|
16 |
+
# )
|
17 |
+
|
18 |
+
# pipe = StableDiffusionControlNetPipeline.from_pretrained(
|
19 |
+
# BASE_MODEL,
|
20 |
+
# controlnet=controlnet,
|
21 |
+
# torch_dtype=torch.float16 if device=="cuda" else torch.float32,
|
22 |
+
# safety_checker=None
|
23 |
+
# )
|
24 |
+
# pipe.to(device)
|
25 |
+
|
26 |
+
# def generate(prompt, control_image, guidance_scale=7.5, steps=30, seed=0):
|
27 |
+
# print("API called:", type(control_image))
|
28 |
+
# generator = torch.Generator(device=device).manual_seed(int(seed)) if seed else None
|
29 |
+
# img = pipe(
|
30 |
+
# prompt=prompt,
|
31 |
+
# image=control_image,
|
32 |
+
# num_inference_steps=int(steps),
|
33 |
+
# guidance_scale=float(guidance_scale),
|
34 |
+
# generator=generator
|
35 |
+
# ).images[0]
|
36 |
+
# return img
|
37 |
+
|
38 |
+
# with gr.Blocks() as demo:
|
39 |
+
# gr.Markdown("# ControlNet Image Generator")
|
40 |
+
# with gr.Row():
|
41 |
+
# prompt = gr.Textbox(label="Prompt", value="A futuristic poster, high detail")
|
42 |
+
# seed = gr.Number(label="Seed (0=random)", value=0)
|
43 |
+
# with gr.Row():
|
44 |
+
# control = gr.Image(type="pil", label="Control image (e.g., QR or edge map)")
|
45 |
+
# steps = gr.Slider(10, 50, 30, step=1, label="Steps")
|
46 |
+
# guidance = gr.Slider(1.0, 12.0, 7.5, step=0.1, label="Guidance scale")
|
47 |
+
# out = gr.Image(label="Result")
|
48 |
+
|
49 |
+
# btn = gr.Button("Generate")
|
50 |
+
# btn.click(generate, [prompt, control, guidance, steps, seed], out)
|
51 |
+
|
52 |
+
# # Enable simple API use
|
53 |
+
# gr.Examples([], inputs=[prompt, control, guidance, steps, seed], outputs=out)
|
54 |
+
|
55 |
+
# demo.launch()
|
56 |
+
|
57 |
+
# app.py — minimal, API-stable echo server + Gradio UI
|
58 |
+
import base64, io
|
59 |
+
from typing import List, Any
|
60 |
from PIL import Image
|
61 |
+
import gradio as gr
|
62 |
+
from fastapi import FastAPI
|
63 |
+
from pydantic import BaseModel
|
64 |
|
65 |
+
# ---- helpers ----
|
66 |
+
def data_url_to_pil(data_url: str) -> Image.Image:
|
67 |
+
# expects "data:image/<ext>;base64,<...>"
|
68 |
+
b64 = data_url.split(",", 1)[1]
|
69 |
+
return Image.open(io.BytesIO(base64.b64decode(b64))).convert("RGB")
|
70 |
|
71 |
+
def pil_to_data_url(img: Image.Image) -> str:
|
72 |
+
buf = io.BytesIO()
|
73 |
+
img.save(buf, format="PNG")
|
74 |
+
return "data:image/png;base64," + base64.b64encode(buf.getvalue()).decode()
|
75 |
|
76 |
+
# ---- your model fn (for now just echo control image) ----
|
77 |
+
def generate(prompt: str, control_image: Image.Image, guidance: float, steps: int, seed: int):
|
78 |
+
# TODO: replace with ControlNet pipeline call
|
79 |
+
return control_image
|
80 |
|
81 |
+
# ---- Gradio UI (not required for API, but nice to see) ----
|
82 |
+
demo = gr.Interface(
|
83 |
+
fn=generate,
|
84 |
+
inputs=[
|
85 |
+
gr.Textbox(label="Prompt", value="High-quality artistic style, preserve structure"),
|
86 |
+
gr.Image(type="pil", label="Control Image (PNG/JPG)"),
|
87 |
+
gr.Slider(1, 12, 7.5, step=0.1, label="Guidance scale"),
|
88 |
+
gr.Slider(10, 50, 30, step=1, label="Steps"),
|
89 |
+
gr.Number(0, label="Seed"),
|
90 |
+
],
|
91 |
+
outputs=gr.Image(label="Result"),
|
92 |
)
|
93 |
+
|
94 |
+
# ---- FastAPI endpoint (stable path for Postman/Next.js) ----
|
95 |
+
app = FastAPI()
|
96 |
+
|
97 |
+
class PredictIn(BaseModel):
|
98 |
+
data: List[Any] # [prompt, controlImageBase64, guidance, steps, seed]
|
99 |
+
|
100 |
+
@app.post("/api/predict/0")
|
101 |
+
def predict(payload: PredictIn):
|
102 |
+
prompt, control_b64, guidance, steps, seed = payload.data
|
103 |
+
img = data_url_to_pil(control_b64) if isinstance(control_b64, str) else control_b64
|
104 |
+
out = generate(str(prompt), img, float(guidance), int(steps), int(seed))
|
105 |
+
return { "data": [ pil_to_data_url(out) ] }
|
106 |
+
|
107 |
+
# mount Gradio at "/"
|
108 |
+
app = gr.mount_gradio_app(app, demo, path="/")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|