numpy
pandas
scikit-learn
gradio
import numpy as np
import gradio as gr
from sklearn.ensemble import RandomForestRegressor
from sklearn.model_selection import train_test_split
from sklearn.metrics import mean_squared_error
import pandas as pd
import json
# Sample architectural data (in reality, this would come from your canvas)
sample_data = {
"room_sizes": [20, 30, 15, 25, 40],
"window_count": [2, 3, 1, 2, 4],
"door_count": [1, 2, 1, 1, 2],
"natural_light": [0.7, 0.8, 0.5, 0.6, 0.9],
"energy_efficiency": [0.75, 0.85, 0.65, 0.7, 0.95]
}
df = pd.DataFrame(sample_data)
# Prepare data for ML model
X = df[["room_sizes", "window_count", "door_count"]]
y = df[["natural_light", "energy_efficiency"]]
# Split data
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
# Train model
model = RandomForestRegressor(n_estimators=100, random_state=42)
model.fit(X_train, y_train)
# Evaluate
y_pred = model.predict(X_test)
mse = mean_squared_error(y_test, y_pred)
print(f"Model MSE: {mse}")
def predict_design(room_size, window_count, door_count):
"""Predict natural light and energy efficiency for a room design"""
input_data = np.array([[room_size, window_count, door_count]])
prediction = model.predict(input_data)
natural_light = round(float(prediction[0][0]), 2)
energy_efficiency = round(float(prediction[0][1]), 2)
# Generate architectural advice based on predictions
advice = []
if natural_light < 0.5:
advice.append("Consider adding more windows or increasing window size for better natural light.")
elif natural_light > 0.8:
advice.append("Excellent natural light! Consider adding shading elements if needed.")
if energy_efficiency < 0.7:
advice.append("Recommend improving insulation or using energy-efficient materials.")
elif energy_efficiency > 0.85:
advice.append("Great energy efficiency! Consider adding renewable energy sources.")
return {
"Natural Light Score": natural_light,
"Energy Efficiency Score": energy_efficiency,
"Recommendations": advice
}
def analyze_design(design_data):
"""Analyze the current architectural design"""
try:
design = json.loads(design_data)
# In a real app, this would analyze the actual design elements
# For demo, we'll use sample values
total_area = sum([room['area'] for room in design.get('rooms', [{'area': 25}])])
window_count = design.get('window_count', 2)
door_count = design.get('door_count', 1)
result = predict_design(total_area, window_count, door_count)
# Format the response
response = f"""