Spaces:
Paused
Paused
Commit
·
38d816c
1
Parent(s):
9e3bd6c
Update app.py
Browse files
app.py
CHANGED
@@ -1,19 +1,25 @@
|
|
1 |
import gradio as gr
|
2 |
-
import requests
|
3 |
from PIL import Image
|
4 |
-
from io import BytesIO
|
5 |
import base64
|
|
|
6 |
import os
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
7 |
|
8 |
-
|
9 |
-
HF_API = "https://api-inference.huggingface.co/models/lllyasviel/controlnet-sdxl-1.0-canny"
|
10 |
-
API_KEY = os.getenv("HF_API_KEY") # Secure: fetch from secret
|
11 |
|
12 |
-
|
13 |
-
|
14 |
-
|
|
|
15 |
|
16 |
-
|
17 |
buffered = BytesIO()
|
18 |
image.save(buffered, format="JPEG")
|
19 |
img_bytes = buffered.getvalue()
|
@@ -27,23 +33,34 @@ def generate_image(prompt, image):
|
|
27 |
"options": {"wait_for_model": True}
|
28 |
}
|
29 |
|
|
|
30 |
response = requests.post(HF_API, headers=headers, json=payload)
|
31 |
-
|
32 |
if response.status_code == 200:
|
33 |
-
|
34 |
-
return img_out
|
35 |
else:
|
36 |
-
|
|
|
|
|
37 |
|
38 |
# Gradio UI
|
39 |
with gr.Blocks() as demo:
|
40 |
-
gr.Markdown("# 🧠 NewCrux AI Demo: Product → Lifestyle Image")
|
|
|
41 |
with gr.Row():
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
|
47 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
48 |
|
49 |
demo.launch()
|
|
|
|
1 |
import gradio as gr
|
|
|
2 |
from PIL import Image
|
|
|
3 |
import base64
|
4 |
+
import requests
|
5 |
import os
|
6 |
+
from io import BytesIO
|
7 |
+
|
8 |
+
from utils.planner import extract_scene_plan # 🧠 Brain Layer (LLM)
|
9 |
+
|
10 |
+
# Hugging Face ControlNet model
|
11 |
+
CONTROLNET_MODEL = "lllyasviel/controlnet-sdxl-1.0-canny"
|
12 |
+
HF_API = f"https://api-inference.huggingface.co/models/{CONTROLNET_MODEL}"
|
13 |
+
API_KEY = os.getenv("HF_API_KEY")
|
14 |
|
15 |
+
headers = {"Authorization": f"Bearer {API_KEY}"}
|
|
|
|
|
16 |
|
17 |
+
# 🧠 Generate image + extract Brain Layer JSON
|
18 |
+
def process_image(prompt, image):
|
19 |
+
# Step 1: Brain Layer – extract structured JSON
|
20 |
+
reasoning_json = extract_scene_plan(prompt)
|
21 |
|
22 |
+
# Step 2: Prepare image for HF API
|
23 |
buffered = BytesIO()
|
24 |
image.save(buffered, format="JPEG")
|
25 |
img_bytes = buffered.getvalue()
|
|
|
33 |
"options": {"wait_for_model": True}
|
34 |
}
|
35 |
|
36 |
+
# Step 3: Generate image
|
37 |
response = requests.post(HF_API, headers=headers, json=payload)
|
|
|
38 |
if response.status_code == 200:
|
39 |
+
result_image = Image.open(BytesIO(response.content))
|
|
|
40 |
else:
|
41 |
+
result_image = None
|
42 |
+
|
43 |
+
return result_image, reasoning_json
|
44 |
|
45 |
# Gradio UI
|
46 |
with gr.Blocks() as demo:
|
47 |
+
gr.Markdown("# 🧠 NewCrux AI Demo: Product → Lifestyle Image with Brain Layer")
|
48 |
+
|
49 |
with gr.Row():
|
50 |
+
with gr.Column():
|
51 |
+
prompt_input = gr.Textbox(label="Enter Prompt")
|
52 |
+
image_input = gr.Image(type="pil", label="Upload Product Image")
|
53 |
+
generate_btn = gr.Button("Generate")
|
54 |
|
55 |
+
with gr.Column():
|
56 |
+
output_image = gr.Image(label="Generated Image")
|
57 |
+
json_output = gr.JSON(label="Brain Layer Reasoning (Scene Plan)")
|
58 |
+
|
59 |
+
generate_btn.click(
|
60 |
+
fn=process_image,
|
61 |
+
inputs=[prompt_input, image_input],
|
62 |
+
outputs=[output_image, json_output]
|
63 |
+
)
|
64 |
|
65 |
demo.launch()
|
66 |
+
|