Mood-Tracker / app.py
Shahadbal's picture
Create app.py
e1ad72a verified
from transformers import pipeline
import pandas as pd
from openai import OpenAI
import gradio as gr
import matplotlib.pyplot as plt
from dotenv import dotenv_values
#This is a model for a multi-label classification task that classifies text into different emotions. It works only in English.
classifier = pipeline("text-classification", model="SamLowe/roberta-base-go_emotions")
# This is a model for a translation task, designed to translate text.
# We use it to translate any non-English text into English, so the classifier can then classify the emotions.
translator = pipeline(task="translation", model="facebook/nllb-200-distilled-600M")
languages = {
"English": "eng_Latn",
"French": "fra_Latn",
"Arabic": "arb_Arab",
"Spanish": "spa_Latn",
"German": "deu_Latn",
"Chinese (Simplified)": "zho_Hans",
"Hindi": "hin_Deva"
}
# prepare openAI client with our api key
env_values = dotenv_values("./app.env")
client = OpenAI(
api_key= env_values['OPENAI_API_KEY'],)
# Create a DataFrame to store user entries and perform analysis.
structure = {
'Date': [],
'Text': [],
'Mood': []
}
df = pd.DataFrame(structure)
# Take the text and its source language, translate it to English, so that the classifier can perform the task.
def translator_text(text, src_lang):
translation = translator(text, src_lang=src_lang, tgt_lang="eng_Latn")
return translation[0]['translation_text']
# Take all the inputs from the user, including the mood (result from the classifier), and append them to the DataFrame.
def appender(date, text, mood):
global df
new_row = pd.DataFrame({'Date': [date], 'Text': [text], 'Mood': [mood]})
df = pd.concat([df, new_row], ignore_index=True)
def main(date, src_lang, text):
# First: Translate the text to English if it is not already in English.
if src_lang!= 'English':
text = translator_text(text, languages[src_lang])
# Second : Classify the text
mood = classifier(text)[0]['label']
# Third : Show a message to the user depending on how they feel.
chat_completion = client.chat.completions.create(
messages=[
{
"role": "user",
"content": f"I feel{mood}, can you tell me a message, without any introductory phrase, just the message itself.",
}
],
model="gpt-3.5-turbo",
)
# Finally : Save to DataFrame
appender(date, text, mood)
#Highlighted the output utilizing 'HighlightedText' in gradio
highlighted_mood = [(f"Today you're feeling", mood)]
return highlighted_mood, chat_completion.choices[0].message.content
#Interface
demo = gr.Interface(
fn=main,
inputs=[gr.Textbox(label="Enter Date (YYYY-MM-DD)"), gr.Dropdown(choices=list(languages.keys()),label="Select a Language",value="English"), gr.Textbox(label="What's happened today?")],
outputs=[gr.HighlightedText(label="Mood"), gr.Textbox(label="Message")],
title = "Daily Journal",
description=(
"Capture your daily experiences, reflections, and insights in a personal journal.\n"
"Log and monitor your mood daily to identify patterns and trends over time.\n"
"Get inspirational or motivational messages each day."
),
theme=gr.themes.Soft() # theme form gradio documentation
)
demo.launch(debug=True)