matanzig commited on
Commit
18e7773
·
verified ·
1 Parent(s): 5138cff

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +51 -14
app.py CHANGED
@@ -56,8 +56,23 @@ def search_by_image(user_image):
56
  user_vector = features.cpu().numpy().flatten().reshape(1, -1)
57
  return get_recommendations_from_vector(user_vector)
58
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
59
  def generate_and_recommend(prompt):
60
- """Tab 2: Generative AI Concept Search"""
61
  if not prompt:
62
  return None, None, "", None, "", None, ""
63
 
@@ -76,7 +91,6 @@ def generate_and_recommend(prompt):
76
 
77
  # --- 4. Gradio User Interface (Premium UI) ---
78
 
79
- # Using a sleek, monochrome theme for an architectural, high-end feel
80
  custom_theme = gr.themes.Monochrome(
81
  primary_hue="neutral",
82
  secondary_hue="neutral",
@@ -93,13 +107,13 @@ with gr.Blocks(theme=custom_theme) as demo:
93
 
94
  with gr.Tabs():
95
 
96
- # --- TAB 1: Classic Search (Safe for the assignment requirements) ---
97
- with gr.TabItem("🔍 Search by Image (Catalog Match)"):
98
  gr.Markdown("Upload an inspiration photo to instantly discover visually and stylistically similar rooms from our curated catalog.")
99
  with gr.Row():
100
  with gr.Column(scale=1):
101
  image_input = gr.Image(label="Upload Inspiration", type="pil")
102
- img_submit_btn = gr.Button("Find Matches", variant="primary")
103
 
104
  with gr.Column(scale=2):
105
  with gr.Row():
@@ -114,18 +128,42 @@ with gr.Blocks(theme=custom_theme) as demo:
114
  img_score3 = gr.Textbox(label="Confidence", interactive=False)
115
 
116
  img_submit_btn.click(
117
- fn=search_by_image,
118
- inputs=[image_input],
119
  outputs=[img_rec1, img_score1, img_rec2, img_score2, img_rec3, img_score3]
120
  )
121
 
122
- # --- TAB 2: Advanced GenAI Search (The Startup Pitch feature) ---
123
- with gr.TabItem(" AI Dream Room (Generative Concept)"):
124
- gr.Markdown("Describe your ideal space. Our Generative AI will draft a concept, and our vision engine will find the closest real-world equivalents.")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
125
  with gr.Row():
126
  with gr.Column(scale=1):
127
- text_input = gr.Textbox(label="Concept Description", placeholder="e.g., A minimalist industrial bedroom with exposed brick...", lines=3)
128
- txt_submit_btn = gr.Button("Generate & Match", variant="primary")
129
  gen_output = gr.Image(label="AI Drafted Concept", type="pil")
130
 
131
  with gr.Column(scale=2):
@@ -141,8 +179,7 @@ with gr.Blocks(theme=custom_theme) as demo:
141
  txt_score3 = gr.Textbox(label="Confidence", interactive=False)
142
 
143
  txt_submit_btn.click(
144
- fn=generate_and_recommend,
145
- inputs=[text_input],
146
  outputs=[gen_output, txt_rec1, txt_score1, txt_rec2, txt_score2, txt_rec3, txt_score3]
147
  )
148
 
 
56
  user_vector = features.cpu().numpy().flatten().reshape(1, -1)
57
  return get_recommendations_from_vector(user_vector)
58
 
59
+ def search_by_text_only(prompt):
60
+ """Tab 2: Fast Text-to-Image Retrieval (No Image Generation)"""
61
+ if not prompt:
62
+ return None, "", None, "", None, ""
63
+
64
+ # CLIP embeds the text directly into the same space as the images
65
+ inputs = processor(text=[prompt], return_tensors="pt", padding=True).to(device)
66
+ with torch.no_grad():
67
+ features = clip_model.get_text_features(**inputs)
68
+ if not isinstance(features, torch.Tensor):
69
+ features = features.pooler_output if hasattr(features, 'pooler_output') else features[0]
70
+
71
+ user_vector = features.cpu().numpy().flatten().reshape(1, -1)
72
+ return get_recommendations_from_vector(user_vector)
73
+
74
  def generate_and_recommend(prompt):
75
+ """Tab 3: Generative AI Concept Search"""
76
  if not prompt:
77
  return None, None, "", None, "", None, ""
78
 
 
91
 
92
  # --- 4. Gradio User Interface (Premium UI) ---
93
 
 
94
  custom_theme = gr.themes.Monochrome(
95
  primary_hue="neutral",
96
  secondary_hue="neutral",
 
107
 
108
  with gr.Tabs():
109
 
110
+ # --- TAB 1: Classic Search by Image ---
111
+ with gr.TabItem("🖼️ Search by Image"):
112
  gr.Markdown("Upload an inspiration photo to instantly discover visually and stylistically similar rooms from our curated catalog.")
113
  with gr.Row():
114
  with gr.Column(scale=1):
115
  image_input = gr.Image(label="Upload Inspiration", type="pil")
116
+ img_submit_btn = gr.Button("Find Matches (Instant)", variant="primary")
117
 
118
  with gr.Column(scale=2):
119
  with gr.Row():
 
128
  img_score3 = gr.Textbox(label="Confidence", interactive=False)
129
 
130
  img_submit_btn.click(
131
+ fn=search_by_image, inputs=[image_input],
 
132
  outputs=[img_rec1, img_score1, img_rec2, img_score2, img_rec3, img_score3]
133
  )
134
 
135
+ # --- TAB 2: Fast Text Search (CLIP Multi-modal) ---
136
+ with gr.TabItem("🔍 Fast Text Search"):
137
+ gr.Markdown("Describe a room in text. Our multi-modal vision engine will instantly search the catalog for matching designs.")
138
+ with gr.Row():
139
+ with gr.Column(scale=1):
140
+ fast_text_input = gr.Textbox(label="Search Query", placeholder="e.g., A minimalist industrial bedroom with exposed brick...", lines=3)
141
+ fast_txt_submit_btn = gr.Button("Search Catalog (Instant)", variant="primary")
142
+
143
+ with gr.Column(scale=2):
144
+ with gr.Row():
145
+ with gr.Column():
146
+ ft_rec1 = gr.Image(label="Top Match")
147
+ ft_score1 = gr.Textbox(label="Confidence", interactive=False)
148
+ with gr.Column():
149
+ ft_rec2 = gr.Image(label="2nd Match")
150
+ ft_score2 = gr.Textbox(label="Confidence", interactive=False)
151
+ with gr.Column():
152
+ ft_rec3 = gr.Image(label="3rd Match")
153
+ ft_score3 = gr.Textbox(label="Confidence", interactive=False)
154
+
155
+ fast_txt_submit_btn.click(
156
+ fn=search_by_text_only, inputs=[fast_text_input],
157
+ outputs=[ft_rec1, ft_score1, ft_rec2, ft_score2, ft_rec3, ft_score3]
158
+ )
159
+
160
+ # --- TAB 3: Advanced GenAI Search ---
161
+ with gr.TabItem("✨ AI Design Studio"):
162
+ gr.Markdown("Describe your ideal space. Our Generative AI will draft a concept from scratch, and then find the closest real-world equivalents. <br> ⏳ *Note: Image generation takes ~3-5 minutes on free servers.*")
163
  with gr.Row():
164
  with gr.Column(scale=1):
165
+ text_input = gr.Textbox(label="Concept Description", placeholder="e.g., A cozy modern living room with large windows...", lines=3)
166
+ txt_submit_btn = gr.Button("Generate Concept & Match", variant="primary")
167
  gen_output = gr.Image(label="AI Drafted Concept", type="pil")
168
 
169
  with gr.Column(scale=2):
 
179
  txt_score3 = gr.Textbox(label="Confidence", interactive=False)
180
 
181
  txt_submit_btn.click(
182
+ fn=generate_and_recommend, inputs=[text_input],
 
183
  outputs=[gen_output, txt_rec1, txt_score1, txt_rec2, txt_score2, txt_rec3, txt_score3]
184
  )
185