ANON-STUDIOS-254 commited on
Commit
c3a5f42
·
verified ·
1 Parent(s): 920d965

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +53 -52
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 expert recommendations addressing the user directly."
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 generate_recommendations(comfort, social_interaction, stressors, privacy, open_question, image_info, alias=DEFAULT_ALIAS,
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 personalized recommendations for {alias}, addressing them directly."
59
  )
60
 
61
- # Generate recommendations using the Hugging Face model
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
- recommendations = response.choices[0].message['content']
70
  # Convert the recommendations to address the user in first person
71
- recommendations = recommendations.replace("You should", "I recommend that you")
72
-
73
- return recommendations
74
 
75
- def analyze_environmental_concerns(comfort, social_interaction, stressors, privacy, open_question, alias, img):
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 Ukiyo-e theme colors
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=['#F08080', '#90EE90', '#000000', '#FFD700']) # Light red, light green, black, gold
88
 
89
  # Improve graph display
90
  ax.set_ylabel('Score', fontsize=14, color='#333333')
91
- ax.set_title(f'Environmental Psychology Concerns for {alias}', fontsize=16, color='#333333')
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 recommendations using the model, passing the image analysis results
102
- recommendations = generate_recommendations(comfort, social_interaction, stressors, privacy, open_question, image_info, alias)
103
-
104
- return fig, recommendations
105
 
106
- # Custom CSS for Ukiyo-e theme
 
 
107
  custom_css = """
108
  body {
109
- font-family: 'Garamond', serif;
110
- background-color: #FAF3F3; /* Light background color */
 
111
  }
112
- .container {
113
- border: 2px solid #F08080; /* Light red border */
114
  border-radius: 10px;
115
  padding: 20px;
116
- background-color: #FFFFFF; /* White background for content */
117
- }
118
- h1 {
119
- color: #F08080; /* Light red */
120
  }
121
- h2, h3, h4 {
122
- color: #333333; /* Dark grey for headings */
123
- }
124
- .gradio-slider .slider .slider-handle {
125
- background-color: #90EE90; /* Light green slider handle */
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: #FFD700; /* Gold background for buttons */
135
- color: #000000; /* Black text for buttons */
 
 
 
 
 
 
 
 
 
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 additional concerns or suggestions you have.", label="Open-ended Question", lines=3),
146
- gr.Textbox(placeholder="Enter a single-word alias (e.g., anon).", label="Client Alias", lines=1), # New input for alias
147
- gr.Image(type="pil", label="Upload an Image for Theme Detection") # New input for image
148
  ]
149
 
150
  outputs = [
151
- gr.Plot(label="Concerns Graph"),
152
- gr.Textbox(label="Recommendations", lines=5)
153
  ]
154
 
155
  gr.Interface(
156
- fn=analyze_environmental_concerns,
157
  inputs=inputs,
158
  outputs=outputs,
159
- title="Mazingira: Environmental Psychology Concerns Analyzer",
160
- description="Input your environmental psychology concerns to receive personalized recommendations and a visual graph, along with image-based analysis.",
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()