123Divyansh commited on
Commit
56ed549
·
verified ·
1 Parent(s): 80bd337

Update src/streamlit_app.py

Browse files
Files changed (1) hide show
  1. src/streamlit_app.py +91 -38
src/streamlit_app.py CHANGED
@@ -1,40 +1,93 @@
1
- import altair as alt
2
- import numpy as np
3
- import pandas as pd
4
  import streamlit as st
 
 
 
5
 
6
- """
7
- # Welcome to Streamlit!
8
-
9
- Edit `/streamlit_app.py` to customize this app to your heart's desire :heart:.
10
- If you have any questions, checkout our [documentation](https://docs.streamlit.io) and [community
11
- forums](https://discuss.streamlit.io).
12
-
13
- In the meantime, below is an example of what you can do with just a few lines of code:
14
- """
15
-
16
- num_points = st.slider("Number of points in spiral", 1, 10000, 1100)
17
- num_turns = st.slider("Number of turns in spiral", 1, 300, 31)
18
-
19
- indices = np.linspace(0, 1, num_points)
20
- theta = 2 * np.pi * num_turns * indices
21
- radius = indices
22
-
23
- x = radius * np.cos(theta)
24
- y = radius * np.sin(theta)
25
-
26
- df = pd.DataFrame({
27
- "x": x,
28
- "y": y,
29
- "idx": indices,
30
- "rand": np.random.randn(num_points),
31
- })
32
-
33
- st.altair_chart(alt.Chart(df, height=700, width=700)
34
- .mark_point(filled=True)
35
- .encode(
36
- x=alt.X("x", axis=None),
37
- y=alt.Y("y", axis=None),
38
- color=alt.Color("idx", legend=None, scale=alt.Scale()),
39
- size=alt.Size("rand", legend=None, scale=alt.Scale(range=[1, 150])),
40
- ))
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import streamlit as st
2
+ import base64
3
+ import requests
4
+ import json
5
 
6
+ st.set_page_config(page_title="Solar Rooftop Analyzer", layout="centered")
7
+ st.title("\U0001F31E Solar Rooftop Analysis")
8
+
9
+ st.markdown("Upload a rooftop image and provide your location and budget. The system will analyze the rooftop and estimate potential solar installation ROI.")
10
+
11
+ # Constants
12
+ OPENROUTER_API_KEY = "your_openrouter_api_key_here"
13
+ VISION_MODEL_NAME = "opengvlab/internvl3-14b:free"
14
+
15
+ # Helpers
16
+ def analyze_image_with_openrouter(image_bytes):
17
+ encoded_image = base64.b64encode(image_bytes).decode("utf-8")
18
+ prompt = (
19
+ "Analyze the rooftop in this image. Output JSON with: [Roof area (sqm), "
20
+ "Sunlight availability (%), Shading (Yes/No), Recommended solar panel type, "
21
+ "Estimated capacity (kW)]."
22
+ )
23
+ headers = {
24
+ "Authorization": f"Bearer {OPENROUTER_API_KEY}",
25
+ "Content-Type": "application/json"
26
+ }
27
+ payload = {
28
+ "model": VISION_MODEL_NAME,
29
+ "messages": [
30
+ {"role": "user", "content": [
31
+ {"type": "text", "text": prompt},
32
+ {"type": "image_url", "image_url": {"url": f"data:image/jpeg;base64,{encoded_image}"}}
33
+ ]}
34
+ ]
35
+ }
36
+ response = requests.post("https://openrouter.ai/api/v1/chat/completions", json=payload, headers=headers)
37
+ if response.status_code == 200:
38
+ return response.json()
39
+ return {"error": "Failed to analyze image."}
40
+
41
+
42
+ def estimate_roi(roof_area, capacity_kw, budget):
43
+ cost_per_kw = 65000 # INR/kW
44
+ estimated_cost = capacity_kw * cost_per_kw
45
+ incentives = estimated_cost * 0.30
46
+ net_cost = estimated_cost - incentives
47
+ annual_savings = capacity_kw * 1500 * 7
48
+ payback_years = round(net_cost / annual_savings, 2)
49
+ return {
50
+ "estimated_cost": estimated_cost,
51
+ "incentives": incentives,
52
+ "net_cost": net_cost,
53
+ "annual_savings": annual_savings,
54
+ "payback_years": payback_years,
55
+ "within_budget": budget >= net_cost
56
+ }
57
+
58
+ # UI
59
+ with st.form("solar_form"):
60
+ image = st.file_uploader("Upload Rooftop Image", type=["jpg", "jpeg", "png"])
61
+ location = st.text_input("Location")
62
+ budget = st.number_input("Budget (INR)", min_value=10000.0, step=1000.0)
63
+ submitted = st.form_submit_button("Analyze")
64
+
65
+ if submitted:
66
+ if image and location and budget:
67
+ with st.spinner("Analyzing rooftop image..."):
68
+ image_data = image.read()
69
+ ai_response = analyze_image_with_openrouter(image_data)
70
+
71
+ if "choices" in ai_response:
72
+ try:
73
+ content = ai_response["choices"][0]["message"]["content"]
74
+ content_json = json.loads(content)
75
+ st.success("Analysis complete!")
76
+ st.subheader("Rooftop Analysis")
77
+ st.json(content_json)
78
+
79
+ if "Roof area (sqm)" in content_json and "Estimated capacity (kW)" in content_json:
80
+ roi = estimate_roi(
81
+ roof_area=content_json["Roof area (sqm)"],
82
+ capacity_kw=content_json["Estimated capacity (kW)"],
83
+ budget=budget
84
+ )
85
+ st.subheader("ROI Estimation")
86
+ st.json(roi)
87
+ except Exception as e:
88
+ st.error(f"Error parsing analysis content: {e}")
89
+ st.json(ai_response)
90
+ else:
91
+ st.error("Failed to analyze the image. Please try again.")
92
+ else:
93
+ st.warning("Please upload an image and fill all fields.")