Understanding and managing emotions is crucial for overall well-being. In today’s fast-paced world, technological solutions can provide valuable insights and support in this area. In this blog post, we’ll explore how to build a Mood Analyzer app using Streamlit, a popular Python library for building interactive web applications, and Lyzr Automata, a powerful tool for natural language processing and task automation.
Building the Mood Analyzer app with Lyzr Automata offers a streamlined approach to leveraging cutting-edge natural language processing (NLP) capabilities and task automation for assessing and improving emotional well-being. Lyzr Automata’s robust NLP models seamlessly integrate with Streamlit, enabling the creation of an intuitive user interface where users can input their emotions and receive personalized recommendations.
By automating the mood analysis process and providing clear instructions to the AI agent, Lyzr Automata ensures accurate assessments and insightful insights into users’ emotional states, ultimately empowering them to cultivate a more balanced and positive outlook on life.
Why use Lyzr SDK’s?
With Lyzr SDKs, crafting your own GenAI application is a breeze, requiring only a few lines of code to get up and running swiftly.
Lets get Started!
Create an app.py file first
import streamlit as st
from lyzr_automata.ai_models.openai import OpenAIModel
from lyzr_automata import Agent, Task
from lyzr_automata.pipelines.linear_sync_pipeline import LinearSyncPipeline
from PIL import Image
from lyzr_automata.tasks.task_literals import InputType, OutputType
import os
This code imports essential libraries and modules for building a Streamlit app integrated with Lyzr Automata, a toolset for AI-driven tasks. Streamlit enables easy web app development in Python, while Lyzr Automata provides advanced AI models and task management capabilities, allowing for streamlined implementation of features like mood analysis or text generation.
The lines of code below sets the OpenAI API key by accessing a secret stored in the Streamlit app’s configuration. The API key is essential for authenticating and accessing OpenAI’s services, such as language models for text generation. By securely retrieving the API key from the app’s secrets, this ensures secure and authorized communication with the OpenAI platform within the application.
# Set the OpenAI API key
os.environ["OPENAI_API_KEY"] = st.secrets["apikey"]
input = st.text_input("Please take a moment to share your emotions with us—it's an important step towards your well-being. By describing how you feel, you're helping us gain valuable insights into your emotional well-being. ",placeholder=f"""Type here""")
This line of code creates a text input field using Streamlit, prompting the user to share their emotions. The placeholder text “Type here” provides guidance to the user on where to input their response. By encouraging users to describe their feelings, the app aims to gather valuable insights into their emotional well-being, facilitating personalized analysis and recommendations through Lyzr Automata.
open_ai_text_completion_model = OpenAIModel(
api_key=st.secrets["apikey"],
parameters={
"model": "gpt-4-turbo-preview",
"temperature": 0.2,
"max_tokens": 1500,
},
)
This code initializes an instance of the OpenAIModel class from Lyzr Automata, specifying the parameters for text completion. The API key is retrieved from the Streamlit app’s secrets, ensuring secure access to OpenAI’s services. The model parameter indicates the specific AI model to use, in this case, “gpt-4-turbo-preview”. Other parameters, such as temperature and max_tokens, control the diversity and length of generated text, respectively. This model will be utilized for generating text-based on user input for mood analysis within the app.
def mood_generation(input):
generator_agent = Agent(
role=" MOOD ANALYZER expert",
prompt_persona=f"Your task is to DETERMINE the emotional state of a user and IDENTIFY whether they are feeling any of the following emotions: Happy, Sad, Angry, Anxious, Stressed, Excited, Content, Irritable, Calm, Energetic, Tired, Frustrated, Overwhelmed, Relaxed, Optimistic, Pessimistic, Lonely, Loved, Bored or Motivated. Furthermore, you MUST MAKE RECOMMENDATIONS on how to IMPROVE their mood."
)
prompt = f"""
You are an Expert MOOD ANALYZER. Your task is to DETERMINE the emotional state of a user and IDENTIFY whether they are feeling any of the following emotions: Happy, Sad, Angry, Anxious, Stressed, Excited, Content, Irritable, Calm, Energetic, Tired, Frustrated, Overwhelmed, Relaxed, Optimistic, Pessimistic, Lonely, Loved, Bored or Motivated. Furthermore, you MUST MAKE RECOMMENDATIONS on how to IMPROVE their mood.
[prompts here]
"""
In the mood_generation function, an instance of the Agent class is created, representing an expert Mood Analyzer persona. The agent is tasked with determining the user's emotional state and identifying specific emotions they may be feeling from a predefined list. Additionally, the agent is required to make recommendations on how to improve the user's mood.
The prompt variable contains instructions for the agent, guiding it on its task. This prompt provides detailed guidelines for analyzing the user's input, identifying emotions, and offering recommendations.
generator_agent_task = Task(
name="Mood Generation",
model=open_ai_text_completion_model,
agent=generator_agent,
instructions=prompt,
default_input=input,
output_type=OutputType.TEXT,
input_type=InputType.TEXT,
).execute()
return generator_agent_task
This code snippet defines a task using Lyzr Automata’s Task class, naming it "Mood Generation" and utilizing an OpenAI model for text generation. The task involves an agent representing an expert Mood Analyzer persona, guided by detailed instructions provided in the prompt. The user's input describing their emotions is used as default input for the task, and the output is expected to be in text format. The execute() method is then called to perform the mood analysis process, and the result is returned from the function.
if st.button("Analyze"):
solution = mood_generation(input)
st.markdown(solution)
This code creates a button using Streamlit, labeled “Analyze”. When the button is clicked, it triggers the mood_generation function, passing the user's input as a parameter. The result of the mood analysis process is stored in the solution variable. Finally, the st.markdown() function is used to display the solution, likely containing the generated text or analysis insights, within the Streamlit app interface.
In this blog post, we’ve demonstrated how to build a Mood Analyzer app using Streamlit and Lyzr Automata. By leveraging the power of natural language processing and task automation, we can create valuable tools to support emotional well-being. Whether for personal use or as part of a larger mental health initiative, apps like these have the potential to make a positive impact on people’s lives.
App link: https://moodtracker-lyzr.streamlit.app/
Source Code: https://github.com/isakshay007/Mood_Tracker
Connect with Lyzr
To learn more about Lyzr and its SDK’s, visit our website or get in touch with our team:
Website: Lyzr.ai
Book a Demo: Book a Demo
Discord: Join our Discord community
Slack : Join our Slack channel
Top comments (0)