Spaces:
Running
Running
Update src/streamlit_app.py
Browse files- src/streamlit_app.py +19 -6
src/streamlit_app.py
CHANGED
@@ -2,6 +2,8 @@ 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")
|
@@ -14,7 +16,13 @@ VISION_MODEL_NAME = "opengvlab/internvl3-14b:free"
|
|
14 |
|
15 |
# Helpers
|
16 |
def analyze_image_with_openrouter(image_bytes):
|
17 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
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, "
|
@@ -27,18 +35,22 @@ def analyze_image_with_openrouter(image_bytes):
|
|
27 |
payload = {
|
28 |
"model": VISION_MODEL_NAME,
|
29 |
"messages": [
|
30 |
-
{
|
31 |
-
|
32 |
-
|
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
|
@@ -64,6 +76,7 @@ with st.form("solar_form"):
|
|
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)
|
|
|
2 |
import base64
|
3 |
import requests
|
4 |
import json
|
5 |
+
from PIL import Image
|
6 |
+
import io
|
7 |
|
8 |
st.set_page_config(page_title="Solar Rooftop Analyzer", layout="centered")
|
9 |
st.title("\U0001F31E Solar Rooftop Analysis")
|
|
|
16 |
|
17 |
# Helpers
|
18 |
def analyze_image_with_openrouter(image_bytes):
|
19 |
+
# Convert image to JPEG bytes just in case and then base64 encode
|
20 |
+
img = Image.open(io.BytesIO(image_bytes)).convert("RGB")
|
21 |
+
buffer = io.BytesIO()
|
22 |
+
img.save(buffer, format="JPEG")
|
23 |
+
jpeg_bytes = buffer.getvalue()
|
24 |
+
|
25 |
+
encoded_image = base64.b64encode(jpeg_bytes).decode("utf-8")
|
26 |
prompt = (
|
27 |
"Analyze the rooftop in this image. Output JSON with: [Roof area (sqm), "
|
28 |
"Sunlight availability (%), Shading (Yes/No), Recommended solar panel type, "
|
|
|
35 |
payload = {
|
36 |
"model": VISION_MODEL_NAME,
|
37 |
"messages": [
|
38 |
+
{
|
39 |
+
"role": "user",
|
40 |
+
"content": [
|
41 |
+
{"type": "text", "text": prompt},
|
42 |
+
{"type": "image_base64", "image_base64": encoded_image}
|
43 |
+
]
|
44 |
+
}
|
45 |
]
|
46 |
}
|
47 |
response = requests.post("https://openrouter.ai/api/v1/chat/completions", json=payload, headers=headers)
|
48 |
+
st.write(f"API Response status: {response.status_code}")
|
49 |
+
st.write(f"API Response text: {response.text}")
|
50 |
if response.status_code == 200:
|
51 |
return response.json()
|
52 |
return {"error": "Failed to analyze image."}
|
53 |
|
|
|
54 |
def estimate_roi(roof_area, capacity_kw, budget):
|
55 |
cost_per_kw = 65000 # INR/kW
|
56 |
estimated_cost = capacity_kw * cost_per_kw
|
|
|
76 |
|
77 |
if submitted:
|
78 |
if image and location and budget:
|
79 |
+
st.image(image, caption="Uploaded Rooftop Image", use_column_width=True)
|
80 |
with st.spinner("Analyzing rooftop image..."):
|
81 |
image_data = image.read()
|
82 |
ai_response = analyze_image_with_openrouter(image_data)
|