Spaces:
Sleeping
Sleeping
ANON-STUDIOS-254
commited on
Commit
•
60b075a
1
Parent(s):
25b3714
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,68 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import matplotlib.pyplot as plt
|
3 |
+
from huggingface_hub import InferenceClient
|
4 |
+
|
5 |
+
# Initialize Hugging Face Inference Client
|
6 |
+
client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
|
7 |
+
|
8 |
+
# Set default values for model parameters and system message
|
9 |
+
DEFAULT_MAX_TOKENS = 512
|
10 |
+
DEFAULT_TEMPERATURE = 0.7
|
11 |
+
DEFAULT_TOP_P = 0.95
|
12 |
+
DEFAULT_SYSTEM_MESSAGE = "You are an expert in environmental psychology. Provide expert recommendations."
|
13 |
+
|
14 |
+
def generate_recommendations(comfort, social_interaction, stressors, privacy, max_tokens=DEFAULT_MAX_TOKENS, temperature=DEFAULT_TEMPERATURE, top_p=DEFAULT_TOP_P, system_message=DEFAULT_SYSTEM_MESSAGE):
|
15 |
+
# Construct the input message for the model with context
|
16 |
+
message = (f"{system_message}\n"
|
17 |
+
f"On a scale of 1-5, with 5 being the highest and 1 being the least ideal, the user rated the following:\n"
|
18 |
+
f"Comfort and Well-being: {comfort}\n"
|
19 |
+
f"Social Interaction: {social_interaction}\n"
|
20 |
+
f"Environmental Stressors: {stressors}\n"
|
21 |
+
f"Privacy and Personal Space: {privacy}\n"
|
22 |
+
f"Provide recommendations for improving the environment based on these ratings.")
|
23 |
+
|
24 |
+
# Generate recommendations using the Hugging Face model
|
25 |
+
response = client.chat_completion(
|
26 |
+
[{"role": "user", "content": message}],
|
27 |
+
max_tokens=max_tokens,
|
28 |
+
temperature=temperature,
|
29 |
+
top_p=top_p
|
30 |
+
)
|
31 |
+
|
32 |
+
recommendations = response.choices[0].message['content']
|
33 |
+
return recommendations
|
34 |
+
|
35 |
+
def analyze_environmental_concerns(comfort, social_interaction, stressors, privacy):
|
36 |
+
# Generate a bar graph for the input scores
|
37 |
+
fig, ax = plt.subplots()
|
38 |
+
categories = ["Comfort and Well-being", "Social Interaction", "Environmental Stressors", "Privacy and Personal Space"]
|
39 |
+
values = [comfort, social_interaction, stressors, privacy]
|
40 |
+
ax.bar(categories, values, color=['blue', 'orange', 'green', 'red'])
|
41 |
+
ax.set_ylabel('Score')
|
42 |
+
ax.set_title('Environmental Psychology Concerns')
|
43 |
+
|
44 |
+
# Generate recommendations using the model
|
45 |
+
recommendations = generate_recommendations(comfort, social_interaction, stressors, privacy)
|
46 |
+
|
47 |
+
return fig, recommendations
|
48 |
+
|
49 |
+
# Create the Gradio interface without exposing internal parameters to the user
|
50 |
+
inputs = [
|
51 |
+
gr.Slider(minimum=1, maximum=5, step=1, label="Comfort and Well-being"),
|
52 |
+
gr.Slider(minimum=1, maximum=5, step=1, label="Social Interaction"),
|
53 |
+
gr.Slider(minimum=1, maximum=5, step=1, label="Environmental Stressors"),
|
54 |
+
gr.Slider(minimum=1, maximum=5, step=1, label="Privacy and Personal Space"),
|
55 |
+
]
|
56 |
+
|
57 |
+
outputs = [
|
58 |
+
gr.Plot(label="Concerns Graph"),
|
59 |
+
gr.Textbox(label="Recommendations", lines=5)
|
60 |
+
]
|
61 |
+
|
62 |
+
gr.Interface(
|
63 |
+
fn=analyze_environmental_concerns,
|
64 |
+
inputs=inputs,
|
65 |
+
outputs=outputs,
|
66 |
+
title="Mazingira: Environmental Psychology Concerns Analyzer",
|
67 |
+
description="Input your environmental psychology concerns to receive personalized recommendations and a visual graph."
|
68 |
+
).launch()
|