File size: 13,360 Bytes
16c9f24
 
 
 
 
 
55d216b
16c9f24
 
 
 
4f2c446
55d216b
4f2c446
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
55d216b
3a0ce30
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
55d216b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cc9dc12
10d3044
 
 
 
 
 
 
 
55d216b
 
 
 
 
 
 
 
 
 
 
 
 
cc9dc12
10d3044
 
 
55d216b
 
 
 
 
 
 
 
 
10d3044
7a8c797
 
 
 
 
 
55d216b
7a8c797
55d216b
7a8c797
 
 
 
 
55d216b
7a8c797
 
3a0ce30
10d3044
16c9f24
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5138cff
 
 
 
 
 
 
 
 
 
 
55d216b
 
 
5138cff
 
 
55d216b
 
 
5138cff
 
 
18e7773
55d216b
 
 
18e7773
 
 
55d216b
 
 
18e7773
 
 
16c9f24
55d216b
 
 
16c9f24
55d216b
16c9f24
 
 
55d216b
 
 
16c9f24
5138cff
55d216b
5138cff
16c9f24
5138cff
16c9f24
5138cff
 
 
 
 
16c9f24
f86e620
55d216b
5138cff
3a0ce30
 
 
55d216b
3a0ce30
 
5138cff
 
16c9f24
55d216b
5138cff
55d216b
 
 
5138cff
 
cc9dc12
490c2b2
55d216b
5138cff
 
 
55d216b
cc9dc12
5138cff
55d216b
cc9dc12
5138cff
55d216b
cc9dc12
55d216b
 
 
 
 
 
 
 
 
18e7773
 
cc9dc12
490c2b2
55d216b
 
 
 
 
 
 
 
 
 
18e7773
 
 
55d216b
cc9dc12
18e7773
55d216b
cc9dc12
18e7773
55d216b
cc9dc12
18e7773
55d216b
 
 
 
 
 
 
 
16c9f24
5138cff
cc9dc12
490c2b2
cc9dc12
55d216b
5138cff
 
 
55d216b
cc9dc12
5138cff
55d216b
cc9dc12
5138cff
55d216b
cc9dc12
16c9f24
55d216b
 
 
 
78646c1
55d216b
 
 
252a234
 
ec7cfbf
1
2
3
4
5
6
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
import gradio as gr
import pandas as pd
import numpy as np
import torch
from transformers import CLIPProcessor, CLIPModel
from datasets import load_dataset
from diffusers import StableDiffusionPipeline
from sklearn.metrics.pairwise import cosine_similarity
import warnings
warnings.filterwarnings('ignore')

custom_css = """

body {
    background-image: url('https://huggingface.co/spaces/matanzig/Interior-Design-GenAI/resolve/main/viss.jpeg');
    background-size: 130% 130%; 
    background-position: 0% 0%;
    background-attachment: fixed;
    animation: panBackground 40s ease-in-out infinite alternate; 
}

@keyframes panBackground {
    0% { background-position: 0% 0%; }
    100% { background-position: 100% 100%; }
}

.gradio-container {
    background: rgba(20, 20, 20, 0.75) !important; 
    border-radius: 20px;
    box-shadow: 0 8px 32px 0 rgba(0, 0, 0, 0.5);
    backdrop-filter: blur(5px); 
    border: 1px solid rgba(255, 255, 255, 0.1);
}


.visionary-title {
    font-size: 3.5rem;
    font-weight: 900;
    text-align: center;
    letter-spacing: 4px;
    margin-bottom: 5px;
    margin-top: 15px;
    background: linear-gradient(270deg, #b8860b, #ffd700, #fff8dc, #b8860b);
    background-size: 200% 200%;
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
    animation: goldShine 4s ease infinite;
    transition: transform 0.3s ease, text-shadow 0.3s ease;
    cursor: pointer;
}

.visionary-title:hover {
    transform: scale(1.03);
    text-shadow: 0px 0px 20px rgba(255, 215, 0, 0.4);
}

@keyframes goldShine {
    0% { background-position: 0% 50%; }
    50% { background-position: 100% 50%; }
    100% { background-position: 0% 50%; }
}

.visionary-subtitle {
    text-align: center;
    color: rgba(255, 255, 255, 0.7);
    font-size: 1.2rem;
    letter-spacing: 2px;
    margin-bottom: 30px;
}

.custom-card {
    background: rgba(15, 15, 15, 0.6) !important; 
    border-radius: 20px !important;
    border: 1px solid rgba(255, 215, 0, 0.15) !important; 
    transition: all 0.3s ease-in-out !important; 
    overflow: hidden !important;
    box-shadow: 0 4px 15px rgba(0,0,0,0.4) !important;
}

.custom-card:hover {
    transform: translateY(-5px) scale(1.01) !important; 
    border: 1px solid rgba(255, 215, 0, 0.6) !important; 
    box-shadow: 0 10px 30px rgba(255, 215, 0, 0.15) !important; 
}

.custom-card img {
    transition: transform 0.4s ease !important; 
}
.custom-card:hover img {
    transform: scale(1.05) !important; 
}

.custom-score {
    background: rgba(10, 10, 10, 0.8) !important;
    border-radius: 12px !important;
    border: 1px solid rgba(255, 215, 0, 0.1) !important;
    transition: all 0.3s ease !important;
}

.custom-score:hover {
    border: 1px solid rgba(255, 215, 0, 0.4) !important;
    box-shadow: 0 0 15px rgba(255, 215, 0, 0.1) !important;
}
.custom-score:hover textarea {
    color: #ffd700 !important; 
}

button.primary {
    background: linear-gradient(90deg, #151515, #252525) !important;
    border: 1px solid rgba(255, 215, 0, 0.4) !important;
    color: #ffd700 !important;
    text-transform: uppercase;
    letter-spacing: 1px;
    font-weight: bold !important;
    transition: all 0.3s ease !important;
}

button.primary:hover {
    background: linear-gradient(90deg, #252525, #353535) !important;
    border: 1px solid rgba(255, 215, 0, 0.8) !important;
    box-shadow: 0 0 15px rgba(255, 215, 0, 0.2) !important;
    transform: translateY(-2px) !important;
}
"""

# --- 1. Load Dataset & Embeddings ---
print("Loading Dataset and Embeddings...")
full_ds = load_dataset("tonijhanel/my_interior_design_dataset", split="train")
sample_dataset = full_ds.shuffle(seed=42).select(range(5000))

df_saved = pd.read_parquet("interior_embeddings.parquet")
dataset_matrix = np.array(df_saved['embedding'].tolist())

# --- 2. Load Deep Learning Models ---
device = "cuda" if torch.cuda.is_available() else "cpu"

print("Loading CLIP Model...")
clip_id = "openai/clip-vit-base-patch32"
processor = CLIPProcessor.from_pretrained(clip_id)
clip_model = CLIPModel.from_pretrained(clip_id).to(device)

print("Loading Stable Diffusion Model...")
pipe = StableDiffusionPipeline.from_pretrained("CompVis/stable-diffusion-v1-4", torch_dtype=torch.float32)
pipe = pipe.to(device)
pipe.enable_attention_slicing()

# --- 3. Core Engine Logic ---

def get_recommendations_from_vector(user_vector):
    similarities = cosine_similarity(user_vector, dataset_matrix)[0]
    top_indices = np.argsort(similarities)[-3:][::-1]
    top_scores = similarities[top_indices]
    
    recs = [sample_dataset[int(i)]['image'] for i in top_indices]
    scores = [f"Match: {score*100:.1f}%" for score in top_scores]
    return recs[0], scores[0], recs[1], scores[1], recs[2], scores[2]

def search_by_image(user_image):
    if user_image is None:
        return None, "", None, "", None, ""
        
    inputs = processor(images=user_image, return_tensors="pt").to(device)
    with torch.no_grad():
        features = clip_model.get_image_features(**inputs)
        if not isinstance(features, torch.Tensor):
            features = features.pooler_output if hasattr(features, 'pooler_output') else features[0]
            
    user_vector = features.cpu().numpy().flatten().reshape(1, -1)
    return get_recommendations_from_vector(user_vector)

def search_by_text_only(prompt):
    if not prompt:
        return None, "", None, "", None, ""
        
    inputs = processor(text=[prompt], return_tensors="pt", padding=True).to(device)
    with torch.no_grad():
        features = clip_model.get_text_features(**inputs)
        if not isinstance(features, torch.Tensor):
            features = features.pooler_output if hasattr(features, 'pooler_output') else features[0]
            
    user_vector = features.cpu().numpy().flatten().reshape(1, -1)
    return get_recommendations_from_vector(user_vector)

def generate_and_recommend(prompt):
    if not prompt:
        return None, None, "", None, "", None, ""
        
    generated_image = pipe(prompt, num_inference_steps=15).images[0]
    
    inputs = processor(images=generated_image, return_tensors="pt").to(device)
    with torch.no_grad():
        features = clip_model.get_image_features(**inputs)
        if not isinstance(features, torch.Tensor):
            features = features.pooler_output if hasattr(features, 'pooler_output') else features[0]
            
    user_vector = features.cpu().numpy().flatten().reshape(1, -1)
    rec1, score1, rec2, score2, rec3, score3 = get_recommendations_from_vector(user_vector)
    
    return generated_image, rec1, score1, rec2, score2, rec3, score3

# --- 4. Gradio User Interface (Premium UI) ---

custom_theme = gr.themes.Monochrome(
    primary_hue="neutral",
    secondary_hue="neutral",
    font=[gr.themes.GoogleFont("Montserrat"), "ui-sans-serif", "system-ui", "sans-serif"]
)


with gr.Blocks(title="Visionary | AI Interior Design", css=custom_css, theme=custom_theme) as demo:
    gr.HTML("""
    <div style="display: flex; flex-direction: column; align-items: center; justify-content: center; margin-top: 20px;">
        <img src="https://huggingface.co/spaces/matanzig/Interior-Design-GenAI/resolve/main/viss1.jpeg" 
             style="height: 110px; border-radius: 15px; box-shadow: 0 4px 15px rgba(0,0,0,0.8); margin-bottom: 10px;">
        
        <h1 class="visionary-title">VISIONARY</h1>
        <div class="visionary-subtitle">AI-Powered Interior Design Engine</div>
    </div>
    """)
    
    with gr.Tabs():
        
        # --- TAB 1: Classic Search by Image ---
        with gr.TabItem("๐Ÿ–ผ๏ธ Search by Image"):
            gr.Markdown("Upload an inspiration photo to instantly discover visually and stylistically similar rooms from our curated catalog.")
            with gr.Row():
                with gr.Column(scale=1):
                    image_input = gr.Image(label="Upload Inspiration", type="pil", elem_classes="custom-card")
                    img_submit_btn = gr.Button("Find Matches (Instant)", variant="primary")
                    
                with gr.Column(scale=2):
                    with gr.Row():
                        with gr.Column():
                            img_rec1 = gr.Image(label="Top Match", elem_classes="custom-card")
                            img_score1 = gr.Textbox(label="Confidence", interactive=False, elem_classes="custom-score")
                        with gr.Column():
                            img_rec2 = gr.Image(label="2nd Match", elem_classes="custom-card")
                            img_score2 = gr.Textbox(label="Confidence", interactive=False, elem_classes="custom-score")
                        with gr.Column():
                            img_rec3 = gr.Image(label="3rd Match", elem_classes="custom-card")
                            img_score3 = gr.Textbox(label="Confidence", interactive=False, elem_classes="custom-score")
                            
            img_submit_btn.click(
                fn=search_by_image, inputs=[image_input],
                outputs=[img_rec1, img_score1, img_rec2, img_score2, img_rec3, img_score3]
            )

        # --- TAB 2: Fast Text Search (CLIP Multi-modal) ---
        with gr.TabItem("๐Ÿ” Fast Text Search"):
            gr.Markdown("Describe a room in text. Our multi-modal vision engine will instantly search the catalog for matching designs.")
            with gr.Row():
                with gr.Column(scale=1):
                    fast_text_input = gr.Textbox(label="Search Query", placeholder="e.g., A minimalist industrial bedroom...", lines=3)
                    fast_txt_submit_btn = gr.Button("Search Catalog (Instant)", variant="primary")
                    
                    gr.Examples(
                        examples=[
                            "A minimalist industrial bedroom with concrete walls",
                            "Luxury modern bathroom with marble and warm lights",
                            "Bohemian living room with lots of plants and wood"
                        ],
                        inputs=fast_text_input
                    )
                    
                with gr.Column(scale=2):
                    with gr.Row():
                        with gr.Column():
                            ft_rec1 = gr.Image(label="Top Match", elem_classes="custom-card")
                            ft_score1 = gr.Textbox(label="Confidence", interactive=False, elem_classes="custom-score")
                        with gr.Column():
                            ft_rec2 = gr.Image(label="2nd Match", elem_classes="custom-card")
                            ft_score2 = gr.Textbox(label="Confidence", interactive=False, elem_classes="custom-score")
                        with gr.Column():
                            ft_rec3 = gr.Image(label="3rd Match", elem_classes="custom-card")
                            ft_score3 = gr.Textbox(label="Confidence", interactive=False, elem_classes="custom-score")

            fast_txt_submit_btn.click(
                fn=search_by_text_only, inputs=[fast_text_input],
                outputs=[ft_rec1, ft_score1, ft_rec2, ft_score2, ft_rec3, ft_score3]
            )

        # --- TAB 3: Advanced GenAI Search ---
        with gr.TabItem("โœจ AI Design Studio"):
            gr.Markdown("Describe your ideal space. Our Generative AI will draft a concept from scratch, and then find the closest real-world equivalents.")
            with gr.Row():
                with gr.Column(scale=1):
                    text_input = gr.Textbox(label="Concept Description", placeholder="e.g., A cozy modern living room...", lines=3)
                    txt_submit_btn = gr.Button("Generate Concept & Match", variant="primary")
                    gen_output = gr.Image(label="AI Drafted Concept", type="pil", elem_classes="custom-card")
                    
                with gr.Column(scale=2):
                    with gr.Row():
                        with gr.Column():
                            txt_rec1 = gr.Image(label="Top Match", elem_classes="custom-card")
                            txt_score1 = gr.Textbox(label="Confidence", interactive=False, elem_classes="custom-score")
                        with gr.Column():
                            txt_rec2 = gr.Image(label="2nd Match", elem_classes="custom-card")
                            txt_score2 = gr.Textbox(label="Confidence", interactive=False, elem_classes="custom-score")
                        with gr.Column():
                            txt_rec3 = gr.Image(label="3rd Match", elem_classes="custom-card")
                            txt_score3 = gr.Textbox(label="Confidence", interactive=False, elem_classes="custom-score")

            txt_submit_btn.click(
                fn=generate_and_recommend, inputs=[text_input],
                outputs=[gen_output, txt_rec1, txt_score1, txt_rec2, txt_score2, txt_rec3, txt_score3]
            )

        # --- TAB 4: Presentation Video ---
        with gr.TabItem("๐ŸŽฅ Presentation Video"):
            gr.Markdown("### ๐ŸŽ“ Project Presentation & Walkthrough \nWatch the video below to see a full walkthrough of the dataset, EDA, model pipeline, and the live application.")
            gr.Video(value="https://huggingface.co/spaces/matanzig/Interior-Design-GenAI/resolve/main/A3.presentation.mp4", interactive=False)

demo.launch()