File size: 5,050 Bytes
1628190
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f5a32f3
1628190
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
import io
import base64
import gradio as gr
from PIL import Image
from openai import OpenAI


def run_demo():
    """Setup the app interface and launch it."""
    with gr.Blocks() as app:

        gr.Markdown('# Mental Health Nudging with Generative AI Demo')
        with gr.Row():

            # input features
            with gr.Column(scale=2):

                # demographics
                gender = gr.Radio(label='Gender', value='N/A',
                                  choices=['Male', 'Female', 'Non-Binary', 'N/A'])
                age = gr.Slider(label='Age', minimum=18, maximum=80, step=1)
                race = gr.Radio(label='Race', value='N/A',
                                choices=['White', 'Hispanic', 'Black', 'Asian', 'N/A'])

                # symptoms
                disorders = ['Sadness', 'Inability to concentrate', 'Anxiety', 'Extreme mood changes',
                             'Social withdrawal', 'Tiredness', 'Lack of appetite', 'Increased appetite']
                symptoms = gr.CheckboxGroup(label='Symptoms', choices=disorders)

                # interests
                interests = gr.Textbox(label='Interests', placeholder='Comma-separated list of interests...')

                # submit button
                submit_button = gr.Button('Generate Nudge')

            # resulting nudge
            with gr.Column(scale=1):
                nudge_image = gr.Image(label='Nudge Image')
                nudge_message = gr.Textbox(label='Nudge Message')

        # submit parameters for nudge generation
        inputs = [gender, age, race, interests, symptoms]
        outputs = [nudge_image, nudge_message]
        submit_button.click(fn=generate, inputs=inputs, outputs=outputs)

    # launch the app
    gr.close_all()
    app.queue(default_concurrency_limit=None)
    app.launch()


def generate(gender, age, race, interests, symptoms):
    """Generate nudging image and message for the given person."""
    nudge_message = generate_nudge_message(gender, age, interests, symptoms)
    nudge_image = generate_nudge_image(gender, age, race, nudge_message)
    return nudge_image, nudge_message


def generate_nudge_message(gender, age, interests, symptoms):
    """Generate a message for a given person."""
    # construct description of the person
    desc = f'A {age} year old '
    if gender == 'Male':
        desc += 'man.'
    elif gender == 'Female':
        desc += 'woman.'
    elif gender == 'Non-Binary':
        desc += 'non-binary person.'
    else:
        desc += 'person.'
    if interests:
        desc += f' They like {interests}.'
    if symptoms:
        desc += f' They have the following mental health symptoms: {", ".join(map(str.lower, symptoms))}.'
    else:
        desc += f' They do not have any mental health symptoms.'

    # generate nudge message
    system_prompt = 'You are writing motivational text messages to help people with their mental health. '\
                  + 'Messages should be friendly and positive, but also professional and super short. '\
                  + 'You are limited on space. Messages should be written at the reading level of an eighth grader. '\
                  + 'Word choice should be short and simple so everyone can understand. \n\n'\
                  + 'You will be given some basic information about the person you are addressing. '\
                  + 'Messages should be short, so be discerning. You should try to use the person\'s '\
                  + 'information to give them relevant and actionable tips for improving their mental health symptoms.'
    user_prompt = f'Write a short inspirational message for the person with the following description:\n\n{desc}'
    messages = [{'role': 'system', 'content': f'{system_prompt}'},
                {'role': 'user', 'content': f'{user_prompt}'}]
    completion = client.chat.completions.create(messages=messages, model='gpt-3.5-turbo', temperature=.5)
    nudge_message = completion.choices[0].message.content

    return nudge_message


def generate_nudge_image(gender, age, race, nudge_message):
    """Generate an image for a given person and message."""
    # construct description of the person
    desc = f'a {age} year old '
    if race != 'N/A':
        desc += f'{race.lower()} '
    if gender == 'Male':
        desc += 'man.'
    elif gender == 'Female':
        desc += 'woman.'
    elif gender == 'Non-Binary':
        desc += 'non-binary person.'
    else:
        desc += 'person.'

    # generate nudge image
    prompt = 'Illustrate one simple, inspirational, fun image to help a person with their mental health. NO TEXT. '\
           + f'The style is cute and illustrative. It is focused on {desc} '\
           + f'The image should suit the following message:\n\n{nudge_message}'
    response = client.images.generate(prompt=prompt, model='dall-e-3', response_format='b64_json')
    nudge_image = Image.open(io.BytesIO(base64.b64decode(response.data[0].b64_json)))

    return nudge_image


if __name__ == '__main__':
    client = OpenAI()
    run_demo()