Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -6,10 +6,10 @@ from keras.models import load_model
|
|
6 |
from PIL import Image, ImageOps
|
7 |
import numpy as np
|
8 |
|
9 |
-
# Initialize Hugging Face Inference Client
|
10 |
client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
|
11 |
|
12 |
-
# Load the pre-trained Keras model for Mazingira 254
|
13 |
model = load_model("keras_model.h5", compile=False)
|
14 |
|
15 |
# Load class labels for environmental themes
|
@@ -20,7 +20,7 @@ with open("labels.txt", "r") as file:
|
|
20 |
DEFAULT_MAX_TOKENS = 1000
|
21 |
DEFAULT_TEMPERATURE = 0.7
|
22 |
DEFAULT_TOP_P = 0.95
|
23 |
-
DEFAULT_SYSTEM_MESSAGE = "You are an expert in environmental psychology. Provide
|
24 |
DEFAULT_ALIAS = "anon" # Default alias
|
25 |
|
26 |
def classify_image(img):
|
@@ -41,9 +41,7 @@ def classify_image(img):
|
|
41 |
"Confidence Score": f"{confidence_score:.2f}"
|
42 |
}
|
43 |
|
44 |
-
def
|
45 |
-
max_tokens=DEFAULT_MAX_TOKENS, temperature=DEFAULT_TEMPERATURE, top_p=DEFAULT_TOP_P,
|
46 |
-
system_message=DEFAULT_SYSTEM_MESSAGE):
|
47 |
# Construct the input message for the model with context
|
48 |
message = (
|
49 |
f"{system_message}\n"
|
@@ -55,10 +53,10 @@ def generate_recommendations(comfort, social_interaction, stressors, privacy, op
|
|
55 |
f"Open-ended Question: {open_question}\n"
|
56 |
f"Detected Image Theme: {image_info['Detected Theme']}\n"
|
57 |
f"Confidence Score: {image_info['Confidence Score']}\n"
|
58 |
-
f"Please provide
|
59 |
)
|
60 |
|
61 |
-
# Generate
|
62 |
response = client.chat_completion(
|
63 |
[{"role": "user", "content": message}],
|
64 |
max_tokens=max_tokens,
|
@@ -66,29 +64,28 @@ def generate_recommendations(comfort, social_interaction, stressors, privacy, op
|
|
66 |
top_p=top_p
|
67 |
)
|
68 |
|
69 |
-
|
70 |
# Convert the recommendations to address the user in first person
|
71 |
-
|
72 |
-
|
73 |
-
return recommendations
|
74 |
|
75 |
-
def
|
76 |
-
# Classify the image
|
77 |
image_info = classify_image(img)
|
78 |
|
79 |
# Use default alias if none is provided
|
80 |
alias = alias or DEFAULT_ALIAS
|
81 |
-
|
82 |
-
# Generate a bar graph for the input scores with
|
83 |
fig, ax = plt.subplots(figsize=(10, 6)) # Increased size for better visibility
|
84 |
categories = ["Comfort and Well-being", "Social Interaction", "Environmental Stressors", "Privacy and Personal Space"]
|
85 |
values = [comfort, social_interaction, stressors, privacy]
|
86 |
|
87 |
-
bars = ax.bar(categories, values, color=['#
|
88 |
|
89 |
# Improve graph display
|
90 |
ax.set_ylabel('Score', fontsize=14, color='#333333')
|
91 |
-
ax.set_title(f'Environmental
|
92 |
ax.yaxis.set_major_locator(MaxNLocator(integer=True))
|
93 |
ax.tick_params(axis='y', colors='#333333')
|
94 |
ax.tick_params(axis='x', colors='#333333')
|
@@ -98,41 +95,45 @@ def analyze_environmental_concerns(comfort, social_interaction, stressors, priva
|
|
98 |
yval = bar.get_height()
|
99 |
ax.text(bar.get_x() + bar.get_width()/2, yval, int(yval), va='bottom', ha='center', color='black', fontsize=12)
|
100 |
|
101 |
-
# Generate
|
102 |
-
|
103 |
-
|
104 |
-
return fig, recommendations
|
105 |
|
106 |
-
|
|
|
|
|
107 |
custom_css = """
|
108 |
body {
|
109 |
-
font-family: '
|
110 |
-
background-color: #
|
|
|
111 |
}
|
112 |
-
|
113 |
-
|
114 |
border-radius: 10px;
|
115 |
padding: 20px;
|
116 |
-
background
|
117 |
-
|
118 |
-
h1 {
|
119 |
-
color: #F08080; /* Light red */
|
120 |
}
|
121 |
-
|
122 |
-
|
123 |
-
|
124 |
-
|
125 |
-
|
126 |
-
|
127 |
-
.gradio-slider .slider .slider-track {
|
128 |
-
background-color: #F08080; /* Light red track */
|
129 |
-
}
|
130 |
-
.gradio-textbox input, .gradio-textbox textarea {
|
131 |
-
border: 2px solid #000000; /* Black border for inputs */
|
132 |
}
|
|
|
133 |
.gradio-button {
|
134 |
-
background-color: #
|
135 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
136 |
}
|
137 |
"""
|
138 |
|
@@ -142,21 +143,21 @@ inputs = [
|
|
142 |
gr.Slider(minimum=1, maximum=5, step=1, label="How would you rate Social Interaction?"),
|
143 |
gr.Slider(minimum=1, maximum=5, step=1, label="How would you rate Environmental Stressors?"),
|
144 |
gr.Slider(minimum=1, maximum=5, step=1, label="How would you rate Privacy and Personal Space?"),
|
145 |
-
gr.Textbox(placeholder="Describe any
|
146 |
-
gr.Textbox(placeholder="Enter
|
147 |
-
gr.Image(type="pil", label="Upload an Image for Theme Detection") # New input for image
|
148 |
]
|
149 |
|
150 |
outputs = [
|
151 |
-
gr.Plot(label="
|
152 |
-
gr.Textbox(label="
|
153 |
]
|
154 |
|
155 |
gr.Interface(
|
156 |
-
fn=
|
157 |
inputs=inputs,
|
158 |
outputs=outputs,
|
159 |
-
title="
|
160 |
-
description="Input your environmental
|
161 |
css=custom_css # Apply custom CSS
|
162 |
).launch()
|
|
|
6 |
from PIL import Image, ImageOps
|
7 |
import numpy as np
|
8 |
|
9 |
+
# Initialize Hugging Face Inference Client for climate change and design recommendations
|
10 |
client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
|
11 |
|
12 |
+
# Load the pre-trained Keras model for Mazingira 254 (Environmental Theme Detection)
|
13 |
model = load_model("keras_model.h5", compile=False)
|
14 |
|
15 |
# Load class labels for environmental themes
|
|
|
20 |
DEFAULT_MAX_TOKENS = 1000
|
21 |
DEFAULT_TEMPERATURE = 0.7
|
22 |
DEFAULT_TOP_P = 0.95
|
23 |
+
DEFAULT_SYSTEM_MESSAGE = "You are an expert in environmental psychology and sustainable design. Provide innovative environmental design ideas to improve the climate and curb climate change, addressing the user directly."
|
24 |
DEFAULT_ALIAS = "anon" # Default alias
|
25 |
|
26 |
def classify_image(img):
|
|
|
41 |
"Confidence Score": f"{confidence_score:.2f}"
|
42 |
}
|
43 |
|
44 |
+
def generate_design_ideas(comfort, social_interaction, stressors, privacy, open_question, image_info, alias=DEFAULT_ALIAS, max_tokens=DEFAULT_MAX_TOKENS, temperature=DEFAULT_TEMPERATURE, top_p=DEFAULT_TOP_P, system_message=DEFAULT_SYSTEM_MESSAGE):
|
|
|
|
|
45 |
# Construct the input message for the model with context
|
46 |
message = (
|
47 |
f"{system_message}\n"
|
|
|
53 |
f"Open-ended Question: {open_question}\n"
|
54 |
f"Detected Image Theme: {image_info['Detected Theme']}\n"
|
55 |
f"Confidence Score: {image_info['Confidence Score']}\n"
|
56 |
+
f"Please provide innovative environmental design ideas to improve climate and curb climate change, addressing {alias} directly."
|
57 |
)
|
58 |
|
59 |
+
# Generate design ideas using the Hugging Face model
|
60 |
response = client.chat_completion(
|
61 |
[{"role": "user", "content": message}],
|
62 |
max_tokens=max_tokens,
|
|
|
64 |
top_p=top_p
|
65 |
)
|
66 |
|
67 |
+
design_ideas = response.choices[0].message['content']
|
68 |
# Convert the recommendations to address the user in first person
|
69 |
+
design_ideas = design_ideas.replace("You should", "I recommend that you")
|
70 |
+
return design_ideas
|
|
|
71 |
|
72 |
+
def analyze_environmental_design(comfort, social_interaction, stressors, privacy, open_question, alias, img):
|
73 |
+
# Classify the image for environmental themes
|
74 |
image_info = classify_image(img)
|
75 |
|
76 |
# Use default alias if none is provided
|
77 |
alias = alias or DEFAULT_ALIAS
|
78 |
+
|
79 |
+
# Generate a bar graph for the input scores with creative theme colors
|
80 |
fig, ax = plt.subplots(figsize=(10, 6)) # Increased size for better visibility
|
81 |
categories = ["Comfort and Well-being", "Social Interaction", "Environmental Stressors", "Privacy and Personal Space"]
|
82 |
values = [comfort, social_interaction, stressors, privacy]
|
83 |
|
84 |
+
bars = ax.bar(categories, values, color=['#4CAF50', '#FFC107', '#2196F3', '#FF5722']) # Green, Amber, Blue, Red
|
85 |
|
86 |
# Improve graph display
|
87 |
ax.set_ylabel('Score', fontsize=14, color='#333333')
|
88 |
+
ax.set_title(f'Environmental Design Assessment for {alias}', fontsize=16, color='#333333')
|
89 |
ax.yaxis.set_major_locator(MaxNLocator(integer=True))
|
90 |
ax.tick_params(axis='y', colors='#333333')
|
91 |
ax.tick_params(axis='x', colors='#333333')
|
|
|
95 |
yval = bar.get_height()
|
96 |
ax.text(bar.get_x() + bar.get_width()/2, yval, int(yval), va='bottom', ha='center', color='black', fontsize=12)
|
97 |
|
98 |
+
# Generate design ideas using the model, passing the image analysis results
|
99 |
+
design_ideas = generate_design_ideas(comfort, social_interaction, stressors, privacy, open_question, image_info, alias)
|
|
|
|
|
100 |
|
101 |
+
return fig, design_ideas
|
102 |
+
|
103 |
+
# Custom CSS for modern design
|
104 |
custom_css = """
|
105 |
body {
|
106 |
+
font-family: 'Arial', sans-serif;
|
107 |
+
background-color: #e0f7fa;
|
108 |
+
color: #00695c;
|
109 |
}
|
110 |
+
|
111 |
+
.gradio-container {
|
112 |
border-radius: 10px;
|
113 |
padding: 20px;
|
114 |
+
background: linear-gradient(135deg, #a5d6a7, #1b5e20);
|
115 |
+
box-shadow: 0px 4px 15px rgba(0, 0, 0, 0.2);
|
|
|
|
|
116 |
}
|
117 |
+
|
118 |
+
.gradio-container h1 {
|
119 |
+
font-family: 'Arial', sans-serif;
|
120 |
+
font-size: 2.5em;
|
121 |
+
text-align: center;
|
122 |
+
color: #ffffff;
|
|
|
|
|
|
|
|
|
|
|
123 |
}
|
124 |
+
|
125 |
.gradio-button {
|
126 |
+
background-color: #00796b;
|
127 |
+
border: none;
|
128 |
+
color: white;
|
129 |
+
padding: 10px 20px;
|
130 |
+
font-size: 1em;
|
131 |
+
cursor: pointer;
|
132 |
+
border-radius: 5px;
|
133 |
+
}
|
134 |
+
|
135 |
+
.gradio-button:hover {
|
136 |
+
background-color: #004d40;
|
137 |
}
|
138 |
"""
|
139 |
|
|
|
143 |
gr.Slider(minimum=1, maximum=5, step=1, label="How would you rate Social Interaction?"),
|
144 |
gr.Slider(minimum=1, maximum=5, step=1, label="How would you rate Environmental Stressors?"),
|
145 |
gr.Slider(minimum=1, maximum=5, step=1, label="How would you rate Privacy and Personal Space?"),
|
146 |
+
gr.Textbox(placeholder="Describe any environmental challenges or ideas you have.", label="Open-ended Question", lines=3),
|
147 |
+
gr.Textbox(placeholder="Enter your alias (e.g., anon).", label="Client Alias", lines=1), # New input for alias
|
148 |
+
gr.Image(type="pil", label="Upload an Image for Environmental Theme Detection") # New input for image
|
149 |
]
|
150 |
|
151 |
outputs = [
|
152 |
+
gr.Plot(label="Environmental Design Assessment"),
|
153 |
+
gr.Textbox(label="Creative Design Ideas", lines=5)
|
154 |
]
|
155 |
|
156 |
gr.Interface(
|
157 |
+
fn=analyze_environmental_design,
|
158 |
inputs=inputs,
|
159 |
outputs=outputs,
|
160 |
+
title="MAZINGIRA 254: Climate-Smart Environmental Design Ideas",
|
161 |
+
description="Input your environmental concerns and ideas to receive creative design recommendations and a visual assessment of climate change impact.",
|
162 |
css=custom_css # Apply custom CSS
|
163 |
).launch()
|