Spaces:
Build error

File size: 4,732 Bytes
aa54aed
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
import cv2
import numpy as np
import plotly.graph_objects as go
import pandas as pd
from math import ceil

# Solar-themed CSS
CSS = """
.gradio-container { background: linear-gradient(to right, #f5f7fa, #e4e8f0); }
.header { color: #ff7e36; text-align: center; }
.panel { border-radius: 10px; box-shadow: 0 4px 8px rgba(0,0,0,0.1); }
"""

def analyze_roof(image, roof_length, roof_width, roof_angle, panel_type):
    # Image processing for roof detection
    if image is not None:
        gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
        edges = cv2.Canny(gray, 100, 200)
        contours, _ = cv2.findContours(edges, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
        cv2.drawContours(image, contours, -1, (0, 255, 0), 2)
    
    # Solar panel calculations
    panel_sizes = {
        "Standard (1.6m x 1m)": (1.6, 1.0),
        "Large (2m x 1.2m)": (2.0, 1.2),
        "Commercial (2.5m x 1.3m)": (2.5, 1.3)
    }
    panel_length, panel_width = panel_sizes[panel_type]
    
    # Calculate maximum panels that can fit
    panels_length = ceil(roof_length / panel_length)
    panels_width = ceil(roof_width / panel_width)
    total_panels = panels_length * panels_width
    
    # 3D Visualization
    fig = create_3d_model(roof_length, roof_width, roof_angle, panel_length, panel_width)
    
    # Generate report
    report = generate_report(total_panels, roof_length, roof_width, panel_type)
    
    return image, fig, report

def create_3d_model(roof_l, roof_w, angle, panel_l, panel_w):
    # Create I-beam structure
    beam_thickness = 0.2
    beams = []
    
    # Main beams along length
    for i in np.linspace(-roof_w/2, roof_w/2, 5):
        beams.append(go.Scatter3d(
            x=[-roof_l/2, roof_l/2],
            y=[i, i],
            z=[0, 0],
            mode='lines',
            line=dict(width=beam_thickness*10, color='#555555'),
            name=f'Beam {i}'
        ))
    
    # Solar panels
    panels = []
    for x in np.arange(-roof_l/2, roof_l/2, panel_l):
        for y in np.arange(-roof_w/2, roof_w/2, panel_w):
            panels.append(go.Mesh3d(
                x=[x, x+panel_l, x+panel_l, x],
                y=[y, y, y+panel_w, y+panel_w],
                z=[0, 0, 0, 0],
                color='#1e88e5',
                opacity=0.8
            ))
    
    fig = go.Figure(data=beams + panels)
    fig.update_layout(
        scene=dict(
            xaxis_title='Length (m)',
            yaxis_title='Width (m)',
            zaxis_title='Height',
            aspectmode='manual',
            aspectratio=dict(x=1, y=roof_w/roof_l, z=0.2)
        ),
        margin=dict(l=0, r=0, b=0, t=0),
        height=500
    )
    return fig

def generate_report(panels, length, width, panel_type):
    energy_per_panel = 300  # watts
    total_energy = panels * energy_per_panel / 1000  # kW
    
    report = f"""
    ## Solar Installation Report
    - **Roof Dimensions**: {length}m x {width}m
    - **Panel Type**: {panel_type}
    - **Total Panels**: {panels}
    - **Estimated Capacity**: {total_energy:.1f} kW
    - **Annual Production**: {total_energy * 1500:.0f} kWh
    - **I-Beams Required**: {ceil(length/2) * 2}
    - **Mounting Hardware**: {panels * 4} units
    """
    return report

with gr.Blocks(css=CSS) as demo:
    gr.Markdown("""
    # ☀️ Solar Panel Design Assistant
    *Optimize your rooftop solar installation with AI*
    """, elem_classes="header")
    
    with gr.Row():
        with gr.Column():
            input_image = gr.Image(label="Upload Roof Image", type="numpy")
            roof_length = gr.Slider(1, 50, value=10, label="Roof Length (meters)")
            roof_width = gr.Slider(1, 30, value=5, label="Roof Width (meters)")
            roof_angle = gr.Slider(0, 60, value=30, label="Roof Angle (degrees)")
            panel_type = gr.Dropdown(
                choices=["Standard (1.6m x 1m)", "Large (2m x 1.2m)", "Commercial (2.5m x 1.3m)"],
                value="Standard (1.6m x 1m)",
                label="Solar Panel Type"
            )
            submit_btn = gr.Button("Design Solar System", variant="primary")
        
        with gr.Column():
            output_image = gr.Image(label="Roof Analysis", interactive=False)
            plot_output = gr.Plot(label="3D Solar Panel Layout")
            report_output = gr.Markdown()
    
    submit_btn.click(
        fn=analyze_roof,
        inputs=[input_image, roof_length, roof_width, roof_angle, panel_type],
        outputs=[output_image, plot_output, report_output]
    )

    gr.Examples(
        examples=[["sample_roof.jpg", 12, 6, 25, "Standard (1.6m x 1m)"]],
        inputs=[input_image, roof_length, roof_width, roof_angle, panel_type]
    )

demo.launch()