Spaces:
Sleeping
Sleeping
Update src/streamlit_app.py
Browse files- 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 |
-
|
| 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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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.")
|