Spaces:
Sleeping
Sleeping
initial commit
Browse filesupdate images
update image
.
- .gitattributes +2 -0
- .gitignore +2 -0
- README.md +2 -2
- app.py +65 -0
- example1.png +3 -0
- example2.png +3 -0
- example3.png +3 -0
- requirements.txt +3 -0
- sustanability_scorer.py +103 -0
.gitattributes
CHANGED
@@ -33,3 +33,5 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
|
33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
|
|
|
|
|
33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
36 |
+
*png filter=lfs diff=lfs merge=lfs -text
|
37 |
+
*.png filter=lfs diff=lfs merge=lfs -text
|
.gitignore
ADDED
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
1 |
+
ai
|
2 |
+
__pycache__/
|
README.md
CHANGED
@@ -1,7 +1,7 @@
|
|
1 |
---
|
2 |
title: Sustainability Scorer
|
3 |
-
emoji:
|
4 |
-
colorFrom:
|
5 |
colorTo: gray
|
6 |
sdk: gradio
|
7 |
sdk_version: 4.41.0
|
|
|
1 |
---
|
2 |
title: Sustainability Scorer
|
3 |
+
emoji: π‘
|
4 |
+
colorFrom: green
|
5 |
colorTo: gray
|
6 |
sdk: gradio
|
7 |
sdk_version: 4.41.0
|
app.py
ADDED
@@ -0,0 +1,65 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# app.py
|
2 |
+
import gradio as gr
|
3 |
+
from sustanability_scorer import (
|
4 |
+
sustainability_scorer,
|
5 |
+
openai_completion,
|
6 |
+
HouseFeature,
|
7 |
+
encode_image,
|
8 |
+
)
|
9 |
+
import json
|
10 |
+
|
11 |
+
|
12 |
+
def combined_function(image):
|
13 |
+
base64_image = encode_image(image, output_format="PNG")
|
14 |
+
|
15 |
+
resp = openai_completion(base64_image) # response in string
|
16 |
+
# Convert the response to a dictionary
|
17 |
+
house_features = json.loads(resp)
|
18 |
+
score = sustainability_scorer(HouseFeature(**house_features))
|
19 |
+
return score
|
20 |
+
|
21 |
+
|
22 |
+
with gr.Blocks() as app:
|
23 |
+
gr.Markdown("## Sustainability Scorer")
|
24 |
+
|
25 |
+
with gr.Row():
|
26 |
+
with gr.Column():
|
27 |
+
image_input = gr.Image(
|
28 |
+
type="pil",
|
29 |
+
label="Upload Image",
|
30 |
+
)
|
31 |
+
submit_button = gr.Button("Submit")
|
32 |
+
|
33 |
+
with gr.Column():
|
34 |
+
score_output = gr.HTML()
|
35 |
+
|
36 |
+
submit_button.click(combined_function, inputs=image_input, outputs=score_output)
|
37 |
+
|
38 |
+
# Add CSS styles
|
39 |
+
gr.HTML(
|
40 |
+
"""
|
41 |
+
<style>
|
42 |
+
.score_output {
|
43 |
+
font-size: 58px;
|
44 |
+
font-weight: bold;
|
45 |
+
padding: 10px;
|
46 |
+
border-radius: 5px;
|
47 |
+
background-color: #f4f4f9;
|
48 |
+
text-align: center;
|
49 |
+
transition: all 0.3s ease;
|
50 |
+
margin-top: 20px;
|
51 |
+
}
|
52 |
+
</style>
|
53 |
+
"""
|
54 |
+
)
|
55 |
+
examples = gr.Examples(
|
56 |
+
examples=[
|
57 |
+
["example1.png"],
|
58 |
+
["example2.png"],
|
59 |
+
["example3.png"],
|
60 |
+
],
|
61 |
+
inputs=image_input,
|
62 |
+
)
|
63 |
+
|
64 |
+
# interface.launch()
|
65 |
+
app.launch()
|
example1.png
ADDED
![]() |
Git LFS Details
|
example2.png
ADDED
![]() |
Git LFS Details
|
example3.png
ADDED
![]() |
Git LFS Details
|
requirements.txt
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
gradio==4.41.0
|
2 |
+
openai==1.40.3
|
3 |
+
Pillow==10.3.0
|
sustanability_scorer.py
ADDED
@@ -0,0 +1,103 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
from openai import OpenAI
|
3 |
+
import base64
|
4 |
+
from pydantic import BaseModel
|
5 |
+
from PIL import Image
|
6 |
+
import io
|
7 |
+
import json
|
8 |
+
import random
|
9 |
+
|
10 |
+
|
11 |
+
class HouseFeature(BaseModel):
|
12 |
+
age_in_years: int
|
13 |
+
solar_panel: bool
|
14 |
+
green_roof: bool
|
15 |
+
is_property: bool
|
16 |
+
|
17 |
+
|
18 |
+
IMAGE_PATH = "house7.png"
|
19 |
+
|
20 |
+
|
21 |
+
# Open the image file and encode it as a base64 string
|
22 |
+
def encode_image(image, output_format="JPEG"):
|
23 |
+
# Resize the image
|
24 |
+
image = image.resize((256, 256))
|
25 |
+
|
26 |
+
# Convert the image to bytes
|
27 |
+
buffered = io.BytesIO()
|
28 |
+
# Save the image in the specified format
|
29 |
+
image.save(buffered, format=output_format)
|
30 |
+
image_bytes = buffered.getvalue()
|
31 |
+
|
32 |
+
# Encode the image bytes to base64
|
33 |
+
base64_encoded_data = base64.b64encode(image_bytes)
|
34 |
+
|
35 |
+
# Convert bytes to string
|
36 |
+
base64_encoded_string = base64_encoded_data.decode("utf-8")
|
37 |
+
|
38 |
+
return base64_encoded_string
|
39 |
+
|
40 |
+
|
41 |
+
def openai_completion(base64_image):
|
42 |
+
# Initialize OpenAI API key from environment variable
|
43 |
+
client = OpenAI(api_key=os.environ.get("OPENAI_API_KEY"))
|
44 |
+
MODEL = "gpt-4o-mini"
|
45 |
+
response = client.beta.chat.completions.parse(
|
46 |
+
model=MODEL,
|
47 |
+
messages=[
|
48 |
+
{
|
49 |
+
"role": "system",
|
50 |
+
"content": "You are a house expert who can estimate house's age. Help me identify houses!",
|
51 |
+
},
|
52 |
+
{
|
53 |
+
"role": "user",
|
54 |
+
"content": [
|
55 |
+
{
|
56 |
+
"type": "text",
|
57 |
+
"text": "Estimate the house's age and whether it has solar panels.",
|
58 |
+
},
|
59 |
+
{
|
60 |
+
"type": "image_url",
|
61 |
+
"image_url": {"url": f"data:image/jpeg;base64,{base64_image}"},
|
62 |
+
},
|
63 |
+
],
|
64 |
+
},
|
65 |
+
],
|
66 |
+
max_tokens=300,
|
67 |
+
response_format=HouseFeature,
|
68 |
+
)
|
69 |
+
|
70 |
+
return response.choices[0].message.content
|
71 |
+
|
72 |
+
|
73 |
+
def sustainability_scorer(house_features: HouseFeature):
|
74 |
+
# Calculate sustainability score based on house features, best score 100, lowest 30
|
75 |
+
score = 0
|
76 |
+
if house_features.is_property:
|
77 |
+
if house_features.age_in_years < 10:
|
78 |
+
score += 70
|
79 |
+
elif house_features.age_in_years < 20:
|
80 |
+
score += 65
|
81 |
+
elif house_features.age_in_years < 30:
|
82 |
+
score += 45
|
83 |
+
else:
|
84 |
+
score += 25
|
85 |
+
if house_features.solar_panel:
|
86 |
+
score += 40
|
87 |
+
if house_features.green_roof:
|
88 |
+
score += 30
|
89 |
+
score += random.randint(0, 5)
|
90 |
+
# score can't be over 100
|
91 |
+
score = min(score, 100)
|
92 |
+
if score > 80:
|
93 |
+
color = "green"
|
94 |
+
feedback = "π Excellent! π"
|
95 |
+
elif score > 50:
|
96 |
+
color = "orange"
|
97 |
+
feedback = "π Good!"
|
98 |
+
else:
|
99 |
+
color = "red"
|
100 |
+
feedback = "βοΈ Needs Improvement"
|
101 |
+
return f"<div class='score_output' style='color: {color};'>{feedback}<br>Sustainability Score: {score}</div>"
|
102 |
+
else:
|
103 |
+
return "Sorry, I'm unable to identify your house from the picture"
|