Spaces:
Paused
Paused
Commit
Β·
fdd3761
1
Parent(s):
ed6c14c
Update app.py
Browse files
app.py
CHANGED
@@ -1,3 +1,5 @@
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
from PIL import Image
|
3 |
import base64
|
@@ -7,14 +9,15 @@ from io import BytesIO
|
|
7 |
|
8 |
from utils.planner import extract_scene_plan # π§ Brain Layer
|
9 |
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
|
|
|
16 |
def process_image(prompt, image, num_variations):
|
17 |
-
# Step 1:
|
18 |
reasoning_json = extract_scene_plan(prompt)
|
19 |
|
20 |
# Step 2: Encode image once
|
@@ -23,6 +26,7 @@ def process_image(prompt, image, num_variations):
|
|
23 |
img_bytes = buffered.getvalue()
|
24 |
encoded_image = base64.b64encode(img_bytes).decode("utf-8")
|
25 |
|
|
|
26 |
outputs = []
|
27 |
|
28 |
for i in range(num_variations):
|
@@ -36,31 +40,36 @@ def process_image(prompt, image, num_variations):
|
|
36 |
}
|
37 |
|
38 |
try:
|
39 |
-
response = requests.post(
|
40 |
if response.status_code == 200:
|
41 |
result_image = Image.open(BytesIO(response.content))
|
42 |
outputs.append(result_image)
|
43 |
else:
|
44 |
-
outputs.append(f"Error
|
45 |
except Exception as e:
|
46 |
outputs.append(f"Exception: {e}")
|
47 |
|
48 |
return outputs, reasoning_json
|
49 |
|
50 |
|
51 |
-
# Gradio UI
|
52 |
with gr.Blocks() as demo:
|
53 |
-
gr.Markdown("# π§ NewCrux AI Demo:
|
54 |
|
55 |
with gr.Row():
|
56 |
with gr.Column():
|
57 |
prompt_input = gr.Textbox(label="Enter Prompt")
|
58 |
image_input = gr.Image(type="pil", label="Upload Product Image")
|
59 |
-
variation_slider = gr.Slider(1, 4, step=1, label="Number of Variations"
|
60 |
generate_btn = gr.Button("Generate")
|
61 |
|
62 |
with gr.Column():
|
63 |
-
output_gallery = gr.Gallery(
|
|
|
|
|
|
|
|
|
|
|
64 |
json_output = gr.JSON(label="Brain Layer Reasoning (Scene Plan)")
|
65 |
|
66 |
generate_btn.click(
|
|
|
1 |
+
# app.py
|
2 |
+
|
3 |
import gradio as gr
|
4 |
from PIL import Image
|
5 |
import base64
|
|
|
9 |
|
10 |
from utils.planner import extract_scene_plan # π§ Brain Layer
|
11 |
|
12 |
+
# π Hugging Face + OpenAI keys (use Secrets)
|
13 |
+
HF_API_KEY = os.getenv("HF_API_KEY")
|
14 |
+
SDXL_MODEL_ID = "stabilityai/stable-diffusion-xl-base-1.0"
|
15 |
+
SDXL_API_URL = f"https://api-inference.huggingface.co/models/{SDXL_MODEL_ID}"
|
16 |
+
HEADERS = {"Authorization": f"Bearer {HF_API_KEY}"}
|
17 |
|
18 |
+
# π Image generation (no ControlNet)
|
19 |
def process_image(prompt, image, num_variations):
|
20 |
+
# Step 1: Extract planning JSON from Brain Layer
|
21 |
reasoning_json = extract_scene_plan(prompt)
|
22 |
|
23 |
# Step 2: Encode image once
|
|
|
26 |
img_bytes = buffered.getvalue()
|
27 |
encoded_image = base64.b64encode(img_bytes).decode("utf-8")
|
28 |
|
29 |
+
# Step 3: Generate multiple variations using base SDXL
|
30 |
outputs = []
|
31 |
|
32 |
for i in range(num_variations):
|
|
|
40 |
}
|
41 |
|
42 |
try:
|
43 |
+
response = requests.post(SDXL_API_URL, headers=HEADERS, json=payload)
|
44 |
if response.status_code == 200:
|
45 |
result_image = Image.open(BytesIO(response.content))
|
46 |
outputs.append(result_image)
|
47 |
else:
|
48 |
+
outputs.append(f"Error {response.status_code}: {response.text}")
|
49 |
except Exception as e:
|
50 |
outputs.append(f"Exception: {e}")
|
51 |
|
52 |
return outputs, reasoning_json
|
53 |
|
54 |
|
55 |
+
# π¨ Gradio UI
|
56 |
with gr.Blocks() as demo:
|
57 |
+
gr.Markdown("# π§ NewCrux AI Demo: Image-to-Image using Base SDXL + Brain Layer")
|
58 |
|
59 |
with gr.Row():
|
60 |
with gr.Column():
|
61 |
prompt_input = gr.Textbox(label="Enter Prompt")
|
62 |
image_input = gr.Image(type="pil", label="Upload Product Image")
|
63 |
+
variation_slider = gr.Slider(1, 4, step=1, value=1, label="Number of Variations")
|
64 |
generate_btn = gr.Button("Generate")
|
65 |
|
66 |
with gr.Column():
|
67 |
+
output_gallery = gr.Gallery(
|
68 |
+
label="Generated Image Variations",
|
69 |
+
columns=2,
|
70 |
+
rows=2,
|
71 |
+
height="auto"
|
72 |
+
)
|
73 |
json_output = gr.JSON(label="Brain Layer Reasoning (Scene Plan)")
|
74 |
|
75 |
generate_btn.click(
|