Spaces:
Sleeping
Sleeping
| import os | |
| import gradio as gr | |
| from langchain.prompts import PromptTemplate | |
| from langchain.memory import ConversationBufferWindowMemory | |
| from langchain.chains import LLMChain | |
| from langchain.chat_models import ChatOpenAI | |
| import openai | |
| OPENAI_API_KEY = os.environ['OPENAI_API_KEY'] | |
| emotional_states_list = [ | |
| "angry", | |
| "bored", | |
| "confident", | |
| "dazed", | |
| "embarrased", | |
| "energized", | |
| "fine", | |
| "flirty", | |
| "focused", | |
| "happy", | |
| "playful", | |
| "uncomfortable", | |
| "sad", | |
| "scared", | |
| "asleep" | |
| ] | |
| intensity_levels = ["none", "low", "medium", "high"] | |
| def self_care_activity(*emotional_states): | |
| activities = { | |
| "angry": { | |
| "low": "Try meditating for 5 minutes.", | |
| "medium": "Go for a walk to cool off.", | |
| "high": "Do an intense workout session." | |
| }, | |
| "bored": { | |
| "low": "Listen to a podcast or audiobook.", | |
| "medium": "Try a new hobby or craft.", | |
| "high": "Plan a day trip or outing." | |
| }, | |
| # Add the remaining emotional states and suggestions here | |
| } | |
| #print(emotional_states) | |
| emotional_dict ={} | |
| for idx, intensity in enumerate(emotional_states): | |
| if intensity != 'none': | |
| emotional_dict[emotional_states_list[idx]] = emotional_states[idx] | |
| #return activities.get(state, {}).get(intensity, "No activity found for this combination.") | |
| print(emotional_dict) | |
| prompt_input = ''' | |
| You are a motivational coach, a kind and caring psychologist and an expert in helping unleashing a person's full potential in anything. | |
| You are given a set of emotional state of an individual below: | |
| {} | |
| If the emotional state is selected as "none", ignore that emotional state. | |
| From the table above, take into account the individual's emotional state. | |
| You want to make the individual feel safe and cared-for. | |
| Think step by step and directly recommend 3 self-care activities in bullet points for the individual based on the individual's emotional state. | |
| '''.format(emotional_dict) | |
| template = ''' | |
| {history} | |
| {human_input} | |
| ''' | |
| prompt = PromptTemplate( | |
| input_variables=["history", "human_input"], | |
| template=template | |
| ) | |
| chatgpt_chain = LLMChain( | |
| llm=ChatOpenAI(model="gpt-3.5-turbo", temperature=0.6,openai_api_key=OPENAI_API_KEY), | |
| prompt=prompt, | |
| verbose=True, | |
| memory=ConversationBufferWindowMemory(k=10), | |
| ) | |
| output = chatgpt_chain.predict(human_input=prompt_input) | |
| return output | |
| inputs = [] | |
| for state in emotional_states_list: | |
| inputs.append(gr.inputs.Radio(intensity_levels, label=state.capitalize(), default="none")) | |
| iface = gr.Interface( | |
| fn=self_care_activity, | |
| inputs=inputs, | |
| outputs=gr.outputs.Textbox(label="Suggested Self-Care Activity"), | |
| title="Self-Care Activity Suggestion", | |
| description="Select an emotional state and intensity level to receive a self-care activity suggestion. \n Please note that this is NOT a professional mental health tool and should not be used as such. \n If you are having any severe symptoms or self-harm thoughts, please consult a professional IMMEDIATELY." | |
| ) | |
| iface.launch() | |