Spaces:
Running
Running
Update src/streamlit_app.py
Browse files- src/streamlit_app.py +41 -18
src/streamlit_app.py
CHANGED
@@ -3,6 +3,7 @@ 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")
|
@@ -19,17 +20,21 @@ def analyze_image_with_openrouter(image_file):
|
|
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:
|
26 |
-
"Sunlight availability (%), Shading (Yes/No),
|
27 |
-
"Estimated capacity (kW)]."
|
28 |
)
|
|
|
29 |
headers = {
|
30 |
"Authorization": f"Bearer {OPENROUTER_API_KEY}",
|
31 |
"Content-Type": "application/json"
|
32 |
}
|
|
|
33 |
payload = {
|
34 |
"model": VISION_MODEL_NAME,
|
35 |
"messages": [
|
@@ -42,11 +47,23 @@ def analyze_image_with_openrouter(image_file):
|
|
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
|
52 |
estimated_cost = capacity_kw * cost_per_kw
|
@@ -74,23 +91,28 @@ if submitted:
|
|
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 |
-
|
80 |
-
|
81 |
-
|
82 |
-
content_json
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
|
|
|
|
|
|
|
|
94 |
except Exception as e:
|
95 |
st.error(f"Error parsing analysis content: {e}")
|
96 |
st.json(ai_response)
|
@@ -98,3 +120,4 @@ if submitted:
|
|
98 |
st.error("Failed to analyze the image. Please try again.")
|
99 |
else:
|
100 |
st.warning("Please upload an image and fill all fields.")
|
|
|
|
3 |
import requests
|
4 |
import json
|
5 |
import io
|
6 |
+
import re
|
7 |
from PIL import Image
|
8 |
|
9 |
st.set_page_config(page_title="Solar Rooftop Analyzer", layout="centered")
|
|
|
20 |
buffer = io.BytesIO()
|
21 |
img.save(buffer, format="JPEG")
|
22 |
jpeg_bytes = buffer.getvalue()
|
23 |
+
|
24 |
# Base64 encode with content-type prefix
|
25 |
encoded_image = "data:image/jpeg;base64," + base64.b64encode(jpeg_bytes).decode("utf-8")
|
26 |
+
|
27 |
prompt = (
|
28 |
+
"Analyze the rooftop in this image. Output JSON with: "
|
29 |
+
"[Roof area (sqm), Sunlight availability (%), Shading (Yes/No), "
|
30 |
+
"Recommended solar panel type, Estimated capacity (kW)]."
|
31 |
)
|
32 |
+
|
33 |
headers = {
|
34 |
"Authorization": f"Bearer {OPENROUTER_API_KEY}",
|
35 |
"Content-Type": "application/json"
|
36 |
}
|
37 |
+
|
38 |
payload = {
|
39 |
"model": VISION_MODEL_NAME,
|
40 |
"messages": [
|
|
|
47 |
}
|
48 |
]
|
49 |
}
|
50 |
+
|
51 |
response = requests.post("https://openrouter.ai/api/v1/chat/completions", json=payload, headers=headers)
|
52 |
+
|
53 |
if response.status_code == 200:
|
54 |
return response.json()
|
55 |
+
|
56 |
return {"error": f"Failed to analyze image. Status code: {response.status_code}, Response: {response.text}"}
|
57 |
|
58 |
+
def extract_json_from_response(content):
|
59 |
+
try:
|
60 |
+
match = re.search(r"\{.*\}", content, re.DOTALL)
|
61 |
+
if match:
|
62 |
+
return json.loads(match.group(0))
|
63 |
+
except Exception as e:
|
64 |
+
st.warning(f"Failed to parse JSON: {e}")
|
65 |
+
return None
|
66 |
+
|
67 |
def estimate_roi(roof_area, capacity_kw, budget):
|
68 |
cost_per_kw = 65000 # INR/kW
|
69 |
estimated_cost = capacity_kw * cost_per_kw
|
|
|
91 |
st.image(uploaded_file, caption="Uploaded Rooftop Image", use_column_width=True)
|
92 |
with st.spinner("Analyzing rooftop image..."):
|
93 |
ai_response = analyze_image_with_openrouter(uploaded_file)
|
94 |
+
|
95 |
if "choices" in ai_response:
|
96 |
try:
|
97 |
+
content = ai_response["choices"][0]["message"]["content"]
|
98 |
+
content_json = extract_json_from_response(content)
|
99 |
+
|
100 |
+
if content_json:
|
101 |
+
st.success("Analysis complete!")
|
102 |
+
st.subheader("Rooftop Analysis")
|
103 |
+
st.json(content_json)
|
104 |
+
|
105 |
+
if "Roof area (sqm)" in content_json and "Estimated capacity (kW)" in content_json:
|
106 |
+
roi = estimate_roi(
|
107 |
+
roof_area=content_json["Roof area (sqm)"],
|
108 |
+
capacity_kw=content_json["Estimated capacity (kW)"],
|
109 |
+
budget=budget
|
110 |
+
)
|
111 |
+
st.subheader("ROI Estimation")
|
112 |
+
st.json(roi)
|
113 |
+
else:
|
114 |
+
st.error("Could not extract structured data from the AI response.")
|
115 |
+
st.text(content) # Fallback: show raw content
|
116 |
except Exception as e:
|
117 |
st.error(f"Error parsing analysis content: {e}")
|
118 |
st.json(ai_response)
|
|
|
120 |
st.error("Failed to analyze the image. Please try again.")
|
121 |
else:
|
122 |
st.warning("Please upload an image and fill all fields.")
|
123 |
+
|