File size: 17,018 Bytes
420d97e
 
bd28c5b
3377508
bd28c5b
420d97e
 
3377508
420d97e
 
 
 
 
 
 
 
 
 
 
 
3377508
 
420d97e
3377508
 
 
 
420d97e
 
3377508
 
 
 
420d97e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3377508
420d97e
3377508
 
420d97e
3377508
420d97e
 
 
 
 
 
 
 
 
bd28c5b
de1976d
 
 
 
 
 
3377508
bd28c5b
3377508
420d97e
 
 
 
 
 
 
 
bd28c5b
 
420d97e
 
0744752
b7ee240
0744752
b7ee240
 
 
 
0744752
 
 
 
6222e95
0744752
b7ee240
 
0744752
 
 
b7ee240
0744752
 
 
 
420d97e
 
 
 
 
 
 
3377508
 
 
 
 
 
 
 
420d97e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3377508
420d97e
fa4d408
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
420d97e
 
bd28c5b
420d97e
 
 
 
 
 
 
 
 
 
d32ad80
 
 
 
 
 
 
11f6ebb
 
6222e95
11f6ebb
420d97e
11f6ebb
 
fa4d408
11f6ebb
6222e95
a6aa67c
8af57aa
fa4d408
 
8af57aa
11f6ebb
a6aa67c
11f6ebb
a6aa67c
11f6ebb
a6aa67c
 
 
 
 
 
11f6ebb
8af57aa
 
11f6ebb
8af57aa
11f6ebb
420d97e
 
6222e95
11f6ebb
 
3377508
420d97e
 
 
3377508
397dd4b
420d97e
397dd4b
420d97e
80c28af
420d97e
3377508
 
420d97e
 
 
 
 
 
 
 
 
 
 
3377508
 
420d97e
 
 
 
 
 
 
 
 
 
 
bd28c5b
420d97e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bd28c5b
420d97e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bd28c5b
420d97e
 
 
 
 
 
 
3377508
 
420d97e
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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
import gradio as gr
import pandas as pd
from PIL import Image
import torch
import torchvision.transforms as T
import os
import json
import sentence_transformers
from huggingface_hub import hf_hub_download
import pickle
import timm
import google.generativeai as genai

# ============================================
# 1. LOAD IMAGE CLASSIFICATION MODEL
# ============================================
print("Loading image classification model...")
REPO_ID = "keerthikoganti/architecture-design-stages-compact-cnn"

pkl_path = hf_hub_download(repo_id=REPO_ID, filename="model_bundle.pkl")
with open(pkl_path, "rb") as f:
    bundle = pickle.load(f)

architecture = bundle["architecture"]
num_classes = bundle["num_classes"]
class_names = bundle["class_names"]
state_dict = bundle["state_dict"]

device = "cuda" if torch.cuda.is_available() else "cpu"
model = timm.create_model(architecture, pretrained=False, num_classes=num_classes)
model.load_state_dict(state_dict)
model.eval().to(device)

TFM = T.Compose([
    T.Resize(224), 
    T.CenterCrop(224), 
    T.ToTensor(), 
    T.Normalize([0.485,0.456,0.406],[0.229,0.224,0.225])
])

print("βœ“ Image classification model loaded successfully!")

# ============================================
# 2. LOAD TEXT CLASSIFICATION MODEL
# ============================================
print("Loading text classification model...")
from autogluon.tabular import TabularPredictor
import shutil

text_repo_id = "kaitongg/my-autogluon-model"
download_dir = "downloaded_predictor"

if os.path.exists(download_dir):
    shutil.rmtree(download_dir)

os.makedirs(download_dir, exist_ok=True)

from huggingface_hub import snapshot_download
downloaded_path = snapshot_download(
    repo_id=text_repo_id,
    repo_type="model",
    local_dir=download_dir,
    local_dir_use_symlinks=False,
)

predictor_path = os.path.join(downloaded_path, "autogluon_predictor")
# Bypass Python version check (model trained on 3.12, running on 3.10)
loaded_predictor_from_hub = TabularPredictor.load(
    predictor_path, 
    require_py_version_match=False,
    require_version_match=False
)

embedding_model = sentence_transformers.SentenceTransformer("sentence-transformers/all-MiniLM-L6-v2")

print("βœ“ Text classification model loaded successfully!")

# ============================================
# 3. INITIALIZE GEMINI API
# ============================================
print("Initializing Gemini API...")

# Get API key from environment variable (set in Hugging Face Spaces secrets)
GEMINI_API_KEY = os.environ.get("GEMINI_API_KEY")

if GEMINI_API_KEY:
    genai.configure(api_key=GEMINI_API_KEY)
    
    # Try models in order of preference (using full model paths)
    model_names_to_try = [
        'models/gemini-2.5-flash',           # Latest stable flash model
        'models/gemini-flash-latest',        # Auto-updates to latest
        'models/gemini-2.0-flash',           # Fallback to 2.0
        'models/gemini-pro-latest',          # Pro version
    ]
    
    gemini_model = None
    for model_name in model_names_to_try:
        try:
            gemini_model = genai.GenerativeModel(model_name)
            # Test with a simple query
            test_response = gemini_model.generate_content("Test")
            print(f"βœ“ Gemini API initialized successfully with {model_name}!")
            break
        except Exception as e:
            print(f"Failed to load {model_name}: {str(e)[:100]}")
            continue
    
    if gemini_model is None:
        print("⚠️ Warning: Could not initialize any Gemini model")
else:
    gemini_model = None
    print("⚠️ Warning: GEMINI_API_KEY not found in environment variables")

# ============================================
# 4. LLM ATTITUDE MAPPING
# ============================================
llm_attitude_mapping = {
    "brainstorm": "creative and encouraging",
    "design_iteration": "constructive and detailed, focusing on improvements",
    "design_optimization": "critical and focused on efficiency and refinement",
    "final_review": "thorough and critical, evaluating completeness and adherence to requirements",
    "random": "neutral and informative, perhaps suggesting a relevant stage",
}

# ============================================
# 5. TEXT CLASSIFICATION FUNCTION
# ============================================
def perform_text_classification_and_format(text: str) -> tuple:
    text_classification_formatted = "No text provided"
    text_classification_probabilities = {"No High Concept": 0.0, "High Concept": 0.0}
    predicted_text_label = "0"

    if text and loaded_predictor_from_hub is not None and embedding_model is not None:
        try:
            embeddings = embedding_model.encode(
                [text],
                batch_size=1,
                show_progress_bar=False,
                convert_to_numpy=True,
                normalize_embeddings=False,
            )

            n, d = embeddings.shape
            text_df_processed = pd.DataFrame(embeddings, columns=[f"e{i}" for i in range(d)])

            text_proba_df = loaded_predictor_from_hub.predict_proba(text_df_processed)

            text_classification_probabilities = {
                "No High Concept": float(text_proba_df.iloc[0].get("0", 0.0)),
                "High Concept": float(text_proba_df.iloc[0].get("1", 0.0)),
            }

            predicted_text_label = str(loaded_predictor_from_hub.predict(text_df_processed).iloc[0])

            if predicted_text_label == "1":
                has_high_concept = "Yes"
                confidence = text_classification_probabilities["High Concept"]
            else:
                has_high_concept = "No"
                confidence = text_classification_probabilities["No High Concept"]

            text_classification_formatted = f"High Concept: {has_high_concept} (Confidence: {confidence:.2f})"

        except Exception as e:
            print(f"Error processing text: {e}")
            text_classification_formatted = f"Text classification failed: {e}"

    return text_classification_formatted, text_classification_probabilities, predicted_text_label

# ============================================
# 6. COMBINED CLASSIFICATION FUNCTION
# ============================================
def perform_classification_and_format(image: Image.Image, text: str) -> tuple:
    image_classification_results = {"error": "No image provided"}
    design_stage = "unknown"

    if image is not None and model is not None:
        try:
            img_tensor = TFM(image).unsqueeze(0).to(device)

            with torch.no_grad():
                img_output = model(img_tensor)

            img_probabilities = torch.softmax(img_output, dim=1)[0]
            predicted_class_index = torch.argmax(img_probabilities).item()
            design_stage = class_names[predicted_class_index]

            image_classification_results = {
                class_names[i]: float(img_probabilities[i]) 
                for i in range(len(class_names))
            }

            print(f"βœ“ Image classified as: {design_stage}")

        except Exception as e:
            print(f"❌ Error processing image: {e}")
            image_classification_results = {"error": f"Image classification failed: {e}"}

    text_classification_formatted, text_classification_probabilities, predicted_text_label = perform_text_classification_and_format(text)

    return image_classification_results, text_classification_probabilities, text_classification_formatted, predicted_text_label

# ============================================
# 7. PROMPT GENERATION FUNCTION
# ============================================
def generate_prompt_only(image_classification_results: dict, 
                         text_classification_probabilities: dict, 
                         predicted_text_label: str, 
                         text: str) -> str:
    design_stage = "unknown"
    if image_classification_results and "error" not in image_classification_results:
        try:
            design_stage = max(image_classification_results, key=image_classification_results.get)
        except Exception:
            design_stage = "unknown"

    has_high_concept = "Unable to determine"
    confidence = 0.0
    if text_classification_probabilities and "error" not in text_classification_probabilities:
        try:
            if predicted_text_label == "1":
                has_high_concept = "Yes"
                confidence = text_classification_probabilities.get("High Concept", 0.0)
            else:
                has_high_concept = "No"
                confidence = text_classification_probabilities.get("No High Concept", 0.0)
        except Exception:
            has_high_concept = "Unable to determine"
            confidence = 0.0

    llm_attitude = llm_attitude_mapping.get(design_stage, llm_attitude_mapping["random"])

    prompt = f"""You are an architecture education assistant helping a student understand architectural concepts.

Context:
- Design stage: {design_stage}
- Your feedback style should be: {llm_attitude}
- Abstract concepts detected: {has_high_concept} (confidence: {confidence:.2f})

Student's input: "{text}"

Instructions:
Please provide educational feedback in 250-350 words that:
1. Uses simple, everyday examples and analogies
2. Explains any abstract architectural concepts in accessible language
3. Provides specific, actionable suggestions for improvement
4. Maintains an encouraging yet constructive tone
5. Ends with a complete sentence

Focus on being helpful and educational rather than critical.
"""
    return prompt

# ============================================
# 8. GEMINI FEEDBACK GENERATION
# ============================================
def generate_feedback_from_prompt(prompt_input: str) -> str:
    if gemini_model is None:
        return "⚠️ Gemini API not configured. Please set GEMINI_API_KEY in Hugging Face Spaces secrets."
    
    try:
        print("Generating feedback with Gemini...")
        
        # Extract just the user's input text
        user_text = prompt_input
        if "Student's input:" in prompt_input:
            parts = prompt_input.split("Student's input:")
            if len(parts) > 1:
                user_text = parts[1].strip().strip('"')
        
        # Ultra-simplified prompt - just the core request
        simple_prompt = f"Provide brief educational feedback on this architectural description: {user_text}"
        
        print(f"Sending prompt ({len(simple_prompt)} chars)")
        
        # Minimal configuration - only what's absolutely necessary
        response = gemini_model.generate_content(simple_prompt)
        
        print(f"Response received")
        
        # Extract text
        llm_response_text = None
        
        try:
            llm_response_text = response.text
            print(f"βœ“ Got text ({len(llm_response_text)} chars)")
        except Exception as e:
            print(f"Failed to get text: {str(e)[:100]}")
            
            # Try alternative extraction
            if response.candidates and response.candidates[0].content:
                candidate = response.candidates[0]
                if candidate.content.parts:
                    texts = [part.text for part in candidate.content.parts if hasattr(part, 'text')]
                    if texts:
                        llm_response_text = "".join(texts)
                        print(f"βœ“ Got text from parts ({len(llm_response_text)} chars)")
        
        if not llm_response_text:
            return "⚠️ No response generated. This may be an API limitation. Try:\n- Shorter, simpler descriptions\n- Removing technical terms\n- Testing with basic input like 'large windows'"
        
        return llm_response_text.strip()
        
    except Exception as e:
        error_msg = str(e)
        print(f"Error: {error_msg[:200]}")
        return f"Error: {error_msg}"

# ============================================
# 9. GRADIO INTERFACE
# ============================================
examples = [
    ["https://huggingface.co/datasets/kaitongg/image/resolve/main/5e848c2d622e7abe1ad48504_5e01ce9f0d272014d0353cd1_Things-You-Need-to-Organize-a-3D-Rendering-Architectural-Project-EASY-RENDER.jpeg", 
     "Exploring spatial relationships and material palettes."],
    ["https://huggingface.co/datasets/kaitongg/image/resolve/main/EXISTING-FIRST-FLOOR-PRES-scaled-e1635965923983.jpg", 
     "The window size is too small."],
    ["https://huggingface.co/datasets/kaitongg/image/resolve/main/bilbao_sketch.png", 
     "The facade expresses the building's relationship with the urban context."],
]

with gr.Blocks(css="""
    .left-column, .middle-column, .right-column {min-width: 300px !important;}
    .textbox-container textarea {min-height: 150px !important;}
""") as demo:
    
    gr.Markdown("# πŸ›οΈ Architecture Feedback Generator (Powered by Gemini)")
    gr.Markdown("""
    Upload an architectural image and provide a text description or question. 
    The system will classify the design stage, analyze the text for high-level concepts, 
    generate a customized prompt, and provide AI-powered feedback using Google's Gemini.
    """)

    with gr.Row():
        # LEFT COLUMN - Input Section
        with gr.Column(scale=1, elem_classes="left-column"):
            gr.Markdown("### πŸ“₯ Input")
            image_input = gr.Image(type="pil", label="Upload Architectural Image", height=300)
            text_input = gr.Textbox(
                label="Enter Text Description or Question", 
                placeholder="Describe your architectural design, ask questions, or provide context...",
                lines=6,
                elem_classes="textbox-container"
            )
            classify_button = gr.Button("πŸ” Classify & Generate Prompt", variant="primary", size="lg")
        
        # MIDDLE COLUMN - Classification & Prompt Section
        with gr.Column(scale=1, elem_classes="middle-column"):
            gr.Markdown("### πŸ“Š Classification Results & Prompt")
            image_output_label = gr.Label(
                num_top_classes=len(class_names), 
                label="Image Classification (Design Stage)"
            )
            text_output_textbox = gr.Textbox(
                label="Text Classification (High Concept Detection)", 
                lines=2,
                elem_classes="textbox-container"
            )
            prompt_output_textbox = gr.Textbox(
                label="Generated Prompt (Editable)", 
                lines=10,
                interactive=True,
                elem_classes="textbox-container"
            )
            generate_feedback_button = gr.Button("✨ Generate AI Feedback", variant="primary", size="lg")
        
        # RIGHT COLUMN - Gemini Output Section
        with gr.Column(scale=1, elem_classes="right-column"):
            gr.Markdown("### πŸ€– AI-Generated Feedback")
            llm_output_text = gr.Textbox(
                label="Gemini Response", 
                lines=20,
                elem_classes="textbox-container",
                show_copy_button=True
            )

    # Hidden state variables
    text_classification_probabilities_state = gr.State()
    predicted_text_label_state = gr.State()

    # Step 1: Classification
    classification_outputs = classify_button.click(
        fn=perform_classification_and_format,
        inputs=[image_input, text_input],
        outputs=[
            image_output_label, 
            text_classification_probabilities_state, 
            text_output_textbox,
            predicted_text_label_state
        ]
    )

    # Step 2: Generate Prompt
    def generate_prompt_wrapper(img_res, txt_prob, predicted_label, txt):
        return generate_prompt_only(img_res, txt_prob, predicted_label, txt)

    classification_outputs.then(
        fn=generate_prompt_wrapper,
        inputs=[
            image_output_label, 
            text_classification_probabilities_state, 
            predicted_text_label_state,
            text_input
        ],
        outputs=prompt_output_textbox
    )

    # Step 3: Gemini Feedback
    generate_feedback_button.click(
        fn=generate_feedback_from_prompt,
        inputs=[prompt_output_textbox],
        outputs=llm_output_text
    )

    # Examples Section
    gr.Markdown("---")
    gr.Markdown("### πŸ’‘ Example Inputs")
    
    def generate_full_chain_output(img, txt):
        img_res, txt_prob, txt_fmt, predicted_label = perform_classification_and_format(img, txt)
        prompt = generate_prompt_only(img_res, txt_prob, predicted_label, txt)
        llm_res = generate_feedback_from_prompt(prompt)
        return img_res, txt_fmt, prompt, llm_res

    gr.Examples(
        examples=examples,
        inputs=[image_input, text_input],
        outputs=[image_output_label, text_output_textbox, prompt_output_textbox, llm_output_text],
        fn=generate_full_chain_output,
        cache_examples=False
    )

if __name__ == "__main__":
    demo.launch()