shaheerawan3 commited on
Commit
2835313
·
verified ·
1 Parent(s): 016f43f

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +96 -0
app.py ADDED
@@ -0,0 +1,96 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import numpy as np
3
+ from PIL import Image
4
+ import io
5
+ import base64
6
+ import requests
7
+ from huggingface_hub import InferenceClient
8
+
9
+ # Initialize clients
10
+ client = InferenceClient()
11
+
12
+ def segment_and_edit(image, prompt):
13
+ """
14
+ Fast interior design editor using Hugging Face Inference API
15
+ """
16
+ try:
17
+ # Convert PIL to bytes
18
+ img_byte_arr = io.BytesIO()
19
+ image.save(img_byte_arr, format='PNG')
20
+ img_byte_arr = img_byte_arr.getvalue()
21
+
22
+ # Use ControlNet Canny for guided editing (faster than segmentation)
23
+ # This maintains room structure while applying edits
24
+ edited_image = client.image_to_image(
25
+ image=img_byte_arr,
26
+ prompt=f"interior design photo, {prompt}, professional photography, 4k, detailed",
27
+ negative_prompt="blurry, distorted, low quality, watermark",
28
+ model="stabilityai/stable-diffusion-2-1",
29
+ strength=0.75, # Balance between original and edit
30
+ )
31
+
32
+ return edited_image
33
+
34
+ except Exception as e:
35
+ return None, f"Error: {str(e)}"
36
+
37
+ def process_room(image, edit_prompt):
38
+ """Main processing function"""
39
+ if image is None:
40
+ return None, "Please upload an image"
41
+
42
+ if not edit_prompt.strip():
43
+ return None, "Please provide editing instructions"
44
+
45
+ # Process the image
46
+ result = segment_and_edit(image, edit_prompt)
47
+
48
+ if isinstance(result, tuple):
49
+ return result
50
+
51
+ return result, "✅ Edit complete!"
52
+
53
+ # Gradio Interface
54
+ with gr.Blocks(theme=gr.themes.Soft()) as demo:
55
+ gr.Markdown("""
56
+ # 🏠 AI Interior Designer Assistant
57
+ Upload a room photo and describe your desired changes. Uses AI to redesign your space.
58
+
59
+ **Example prompts:**
60
+ - "Change the sofa to mid-century modern, deep blue velvet"
61
+ - "Replace with white minimalist furniture and add plants"
62
+ - "Make it Scandinavian style with light wood"
63
+ """)
64
+
65
+ with gr.Row():
66
+ with gr.Column():
67
+ input_image = gr.Image(type="pil", label="Upload Room Photo")
68
+ edit_prompt = gr.Textbox(
69
+ label="Describe Your Changes",
70
+ placeholder="E.g., change the sofa to modern gray fabric and add a floor lamp",
71
+ lines=3
72
+ )
73
+ submit_btn = gr.Button("Transform Room 🎨", variant="primary")
74
+
75
+ with gr.Column():
76
+ output_image = gr.Image(label="Redesigned Room")
77
+ status_text = gr.Textbox(label="Status", interactive=False)
78
+
79
+ # Examples
80
+ gr.Examples(
81
+ examples=[
82
+ ["Change to modern minimalist with white furniture", None],
83
+ ["Replace with bohemian style, add colorful cushions and plants", None],
84
+ ["Make it industrial loft style with exposed brick aesthetic", None],
85
+ ],
86
+ inputs=[edit_prompt, input_image],
87
+ )
88
+
89
+ submit_btn.click(
90
+ fn=process_room,
91
+ inputs=[input_image, edit_prompt],
92
+ outputs=[output_image, status_text]
93
+ )
94
+
95
+ if __name__ == "__main__":
96
+ demo.launch()