Manireddy1508 commited on
Commit
4e7c237
·
1 Parent(s): 7503185

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +32 -25
app.py CHANGED
@@ -5,61 +5,68 @@ 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()
 
 
 
 
 
 
 
 
 
 
 
 
 
26
 
27
- payload = {
28
- "inputs": {
29
- "prompt": prompt,
30
- "image": base64.b64encode(img_bytes).decode("utf-8"),
31
- "negative_prompt": "blurry, deformed, cropped"
32
- },
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()
 
5
  import os
6
  from io import BytesIO
7
 
8
+ from utils.planner import extract_scene_plan # 🧠 Brain Layer
9
 
 
10
  CONTROLNET_MODEL = "lllyasviel/controlnet-sdxl-1.0-canny"
11
  HF_API = f"https://api-inference.huggingface.co/models/{CONTROLNET_MODEL}"
12
  API_KEY = os.getenv("HF_API_KEY")
 
13
  headers = {"Authorization": f"Bearer {API_KEY}"}
14
 
15
+
16
+ def process_image(prompt, image, num_variations):
17
  # Step 1: Brain Layer – extract structured JSON
18
  reasoning_json = extract_scene_plan(prompt)
19
 
20
+ # Step 2: Encode image once
21
  buffered = BytesIO()
22
  image.save(buffered, format="JPEG")
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):
29
+ payload = {
30
+ "inputs": {
31
+ "prompt": prompt,
32
+ "image": encoded_image,
33
+ "negative_prompt": "blurry, deformed, cropped"
34
+ },
35
+ "options": {"wait_for_model": True}
36
+ }
37
 
38
+ try:
39
+ response = requests.post(HF_API, headers=headers, json=payload)
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: {response.status_code} - {response.text}")
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: Product → Lifestyle Image Generator")
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", value=1)
60
  generate_btn = gr.Button("Generate")
61
 
62
  with gr.Column():
63
+ output_gallery = gr.Gallery(label="Generated Images").style(grid=2, height="auto")
64
  json_output = gr.JSON(label="Brain Layer Reasoning (Scene Plan)")
65
 
66
  generate_btn.click(
67
  fn=process_image,
68
+ inputs=[prompt_input, image_input, variation_slider],
69
+ outputs=[output_gallery, json_output]
70
  )
71
 
72
  demo.launch()