Spaces:
Running
Running
Update src/streamlit_app.py
Browse files
- src/streamlit_app.py +15 -24
src/streamlit_app.py
CHANGED
@@ -2,6 +2,7 @@ import streamlit as st
|
|
2 |
import base64
|
3 |
import requests
|
4 |
import json
|
|
|
5 |
from PIL import Image
|
6 |
|
7 |
st.set_page_config(page_title="Solar Rooftop Analyzer", layout="centered")
|
@@ -9,19 +10,17 @@ st.title("\U0001F31E Solar Rooftop Analysis")
|
|
9 |
|
10 |
st.markdown("Upload a rooftop image and provide your location and budget. The system will analyze the rooftop and estimate potential solar installation ROI.")
|
11 |
|
12 |
-
# Constants
|
13 |
OPENROUTER_API_KEY = "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"
|
14 |
VISION_MODEL_NAME = "opengvlab/internvl3-14b:free"
|
15 |
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
img = Image.open(io.BytesIO(image_bytes)).convert("RGB")
|
20 |
buffer = io.BytesIO()
|
21 |
img.save(buffer, format="JPEG")
|
22 |
jpeg_bytes = buffer.getvalue()
|
23 |
-
|
24 |
-
encoded_image = base64.b64encode(jpeg_bytes).decode("utf-8")
|
25 |
prompt = (
|
26 |
"Analyze the rooftop in this image. Output JSON with: [Roof area (sqm), "
|
27 |
"Sunlight availability (%), Shading (Yes/No), Recommended solar panel type, "
|
@@ -38,17 +37,15 @@ def analyze_image_with_openrouter(image_bytes):
|
|
38 |
"role": "user",
|
39 |
"content": [
|
40 |
{"type": "text", "text": prompt},
|
41 |
-
{"type": "
|
42 |
]
|
43 |
}
|
44 |
]
|
45 |
}
|
46 |
response = requests.post("https://openrouter.ai/api/v1/chat/completions", json=payload, headers=headers)
|
47 |
-
st.write(f"API Response status: {response.status_code}")
|
48 |
-
st.write(f"API Response text: {response.text}")
|
49 |
if response.status_code == 200:
|
50 |
return response.json()
|
51 |
-
return {"error": "Failed to analyze image."}
|
52 |
|
53 |
def estimate_roi(roof_area, capacity_kw, budget):
|
54 |
cost_per_kw = 65000 # INR/kW
|
@@ -66,32 +63,26 @@ def estimate_roi(roof_area, capacity_kw, budget):
|
|
66 |
"within_budget": budget >= net_cost
|
67 |
}
|
68 |
|
69 |
-
# UI
|
70 |
with st.form("solar_form"):
|
71 |
-
|
72 |
location = st.text_input("Location")
|
73 |
budget = st.number_input("Budget (INR)", min_value=10000.0, step=1000.0)
|
74 |
submitted = st.form_submit_button("Analyze")
|
75 |
-
|
76 |
if submitted:
|
77 |
-
if
|
78 |
-
st.image(
|
79 |
with st.spinner("Analyzing rooftop image..."):
|
80 |
-
|
81 |
-
ai_response = analyze_image_with_openrouter(image_data)
|
82 |
-
|
83 |
if "choices" in ai_response:
|
84 |
try:
|
85 |
choice = ai_response["choices"][0]
|
86 |
-
|
87 |
-
|
88 |
-
else:
|
89 |
-
raise ValueError("Invalid response format: 'message' or 'content' missing.")
|
90 |
content_json = json.loads(content)
|
91 |
st.success("Analysis complete!")
|
92 |
st.subheader("Rooftop Analysis")
|
93 |
st.json(content_json)
|
94 |
-
|
95 |
if "Roof area (sqm)" in content_json and "Estimated capacity (kW)" in content_json:
|
96 |
roi = estimate_roi(
|
97 |
roof_area=content_json["Roof area (sqm)"],
|
|
|
2 |
import base64
|
3 |
import requests
|
4 |
import json
|
5 |
+
import io
|
6 |
from PIL import Image
|
7 |
|
8 |
st.set_page_config(page_title="Solar Rooftop Analyzer", layout="centered")
|
|
|
10 |
|
11 |
st.markdown("Upload a rooftop image and provide your location and budget. The system will analyze the rooftop and estimate potential solar installation ROI.")
|
12 |
|
|
|
13 |
OPENROUTER_API_KEY = "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"
|
14 |
VISION_MODEL_NAME = "opengvlab/internvl3-14b:free"
|
15 |
|
16 |
+
def analyze_image_with_openrouter(image_file):
|
17 |
+
# Read and convert image to JPEG bytes
|
18 |
+
img = Image.open(image_file).convert("RGB")
|
|
|
19 |
buffer = io.BytesIO()
|
20 |
img.save(buffer, format="JPEG")
|
21 |
jpeg_bytes = buffer.getvalue()
|
22 |
+
# Base64 encode with content-type prefix
|
23 |
+
encoded_image = "data:image/jpeg;base64," + base64.b64encode(jpeg_bytes).decode("utf-8")
|
24 |
prompt = (
|
25 |
"Analyze the rooftop in this image. Output JSON with: [Roof area (sqm), "
|
26 |
"Sunlight availability (%), Shading (Yes/No), Recommended solar panel type, "
|
|
|
37 |
"role": "user",
|
38 |
"content": [
|
39 |
{"type": "text", "text": prompt},
|
40 |
+
{"type": "image_url", "image_url": {"url": encoded_image}}
|
41 |
]
|
42 |
}
|
43 |
]
|
44 |
}
|
45 |
response = requests.post("https://openrouter.ai/api/v1/chat/completions", json=payload, headers=headers)
|
|
|
|
|
46 |
if response.status_code == 200:
|
47 |
return response.json()
|
48 |
+
return {"error": f"Failed to analyze image. Status code: {response.status_code}, Response: {response.text}"}
|
49 |
|
50 |
def estimate_roi(roof_area, capacity_kw, budget):
|
51 |
cost_per_kw = 65000 # INR/kW
|
|
|
63 |
"within_budget": budget >= net_cost
|
64 |
}
|
65 |
|
|
|
66 |
with st.form("solar_form"):
|
67 |
+
uploaded_file = st.file_uploader("Upload Rooftop Image", type=["jpg", "jpeg", "png"])
|
68 |
location = st.text_input("Location")
|
69 |
budget = st.number_input("Budget (INR)", min_value=10000.0, step=1000.0)
|
70 |
submitted = st.form_submit_button("Analyze")
|
71 |
+
|
72 |
if submitted:
|
73 |
+
if uploaded_file and location and budget:
|
74 |
+
st.image(uploaded_file, caption="Uploaded Rooftop Image", use_column_width=True)
|
75 |
with st.spinner("Analyzing rooftop image..."):
|
76 |
+
ai_response = analyze_image_with_openrouter(uploaded_file)
|
|
|
|
|
77 |
if "choices" in ai_response:
|
78 |
try:
|
79 |
choice = ai_response["choices"][0]
|
80 |
+
# Some models may return content as a string, others as a dict
|
81 |
+
content = choice.get("message", {}).get("content", "")
|
|
|
|
|
82 |
content_json = json.loads(content)
|
83 |
st.success("Analysis complete!")
|
84 |
st.subheader("Rooftop Analysis")
|
85 |
st.json(content_json)
|
|
|
86 |
if "Roof area (sqm)" in content_json and "Estimated capacity (kW)" in content_json:
|
87 |
roi = estimate_roi(
|
88 |
roof_area=content_json["Roof area (sqm)"],
|