File size: 3,289 Bytes
f2b0974
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e26b867
 
 
 
 
f2b0974
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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)