ANON-STUDIOS-254 commited on
Commit
b74c10d
1 Parent(s): 7655cc8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +54 -15
app.py CHANGED
@@ -2,27 +2,61 @@ import gradio as gr
2
  import matplotlib.pyplot as plt
3
  from matplotlib.ticker import MaxNLocator
4
  from huggingface_hub import InferenceClient
 
 
 
5
 
6
  # Initialize Hugging Face Inference Client
7
  client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
8
 
9
- # Set default values for model parameters and system message
 
 
 
 
 
 
 
10
  DEFAULT_MAX_TOKENS = 1000
11
  DEFAULT_TEMPERATURE = 0.7
12
  DEFAULT_TOP_P = 0.95
13
  DEFAULT_SYSTEM_MESSAGE = "You are an expert in environmental psychology. Provide expert recommendations addressing the user directly."
14
  DEFAULT_ALIAS = "anon" # Default alias
15
 
16
- def generate_recommendations(comfort, social_interaction, stressors, privacy, open_question, alias=DEFAULT_ALIAS, max_tokens=DEFAULT_MAX_TOKENS, temperature=DEFAULT_TEMPERATURE, top_p=DEFAULT_TOP_P, system_message=DEFAULT_SYSTEM_MESSAGE):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
17
  # Construct the input message for the model with context
18
- message = (f"{system_message}\n"
19
- f"On a scale of 1-5, with 5 being the highest and 1 being the least ideal, {alias} rated the following:\n"
20
- f"Comfort and Well-being: {comfort}\n"
21
- f"Social Interaction: {social_interaction}\n"
22
- f"Environmental Stressors: {stressors}\n"
23
- f"Privacy and Personal Space: {privacy}\n"
24
- f"Open-ended Question: {open_question}\n"
25
- f"Please provide personalized recommendations for {alias}, addressing them directly.")
 
 
 
 
26
 
27
  # Generate recommendations using the Hugging Face model
28
  response = client.chat_completion(
@@ -35,9 +69,13 @@ def generate_recommendations(comfort, social_interaction, stressors, privacy, op
35
  recommendations = response.choices[0].message['content']
36
  # Convert the recommendations to address the user in first person
37
  recommendations = recommendations.replace("You should", "I recommend that you")
 
38
  return recommendations
39
 
40
- def analyze_environmental_concerns(comfort, social_interaction, stressors, privacy, open_question, alias):
 
 
 
41
  # Use default alias if none is provided
42
  alias = alias or DEFAULT_ALIAS
43
 
@@ -60,8 +98,8 @@ def analyze_environmental_concerns(comfort, social_interaction, stressors, priva
60
  yval = bar.get_height()
61
  ax.text(bar.get_x() + bar.get_width()/2, yval, int(yval), va='bottom', ha='center', color='black', fontsize=12)
62
 
63
- # Generate recommendations using the model
64
- recommendations = generate_recommendations(comfort, social_interaction, stressors, privacy, open_question, alias)
65
 
66
  return fig, recommendations
67
 
@@ -105,7 +143,8 @@ inputs = [
105
  gr.Slider(minimum=1, maximum=5, step=1, label="How would you rate Environmental Stressors?"),
106
  gr.Slider(minimum=1, maximum=5, step=1, label="How would you rate Privacy and Personal Space?"),
107
  gr.Textbox(placeholder="Describe any additional concerns or suggestions you have.", label="Open-ended Question", lines=3),
108
- gr.Textbox(placeholder="Enter a single-word alias (e.g., anon).", label="Client Alias", lines=1) # New input for alias
 
109
  ]
110
 
111
  outputs = [
@@ -118,6 +157,6 @@ gr.Interface(
118
  inputs=inputs,
119
  outputs=outputs,
120
  title="Mazingira: Environmental Psychology Concerns Analyzer",
121
- description="Input your environmental psychology concerns to receive personalized recommendations and a visual graph.",
122
  css=custom_css # Apply custom CSS
123
  ).launch()
 
2
  import matplotlib.pyplot as plt
3
  from matplotlib.ticker import MaxNLocator
4
  from huggingface_hub import InferenceClient
5
+ 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("/content/keras_model.h5", compile=False)
14
+
15
+ # Load class labels for environmental themes
16
+ with open("/content/labels.txt", "r") as file:
17
+ class_names = [line.strip() for line in file.readlines()]
18
+
19
+ # Default parameters for the model
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):
27
+ # Prepare the image for prediction
28
+ image = ImageOps.fit(img, (224, 224), Image.Resampling.LANCZOS)
29
+ image_array = np.asarray(image)
30
+ normalized_image_array = (image_array.astype(np.float32) / 127.5) - 1
31
+ data = normalized_image_array.reshape((1, 224, 224, 3))
32
+
33
+ # Get the model prediction
34
+ prediction = model.predict(data)
35
+ index = np.argmax(prediction)
36
+ class_name = class_names[index]
37
+ confidence_score = prediction[0][index]
38
+
39
+ return {
40
+ "Detected Theme": class_name,
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"
50
+ f"On a scale of 1-5, with 5 being the highest and 1 being the least ideal, {alias} rated the following:\n"
51
+ f"Comfort and Well-being: {comfort}\n"
52
+ f"Social Interaction: {social_interaction}\n"
53
+ f"Environmental Stressors: {stressors}\n"
54
+ f"Privacy and Personal Space: {privacy}\n"
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(
 
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
 
 
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
 
 
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 = [
 
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()