|
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 |
|
|
|
|
|
classifier = pipeline("text-classification", model="SamLowe/roberta-base-go_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" |
|
} |
|
|
|
|
|
env_values = dotenv_values("./app.env") |
|
client = OpenAI( |
|
api_key= env_values['OPENAI_API_KEY'],) |
|
|
|
|
|
|
|
|
|
structure = { |
|
'Date': [], |
|
'Text': [], |
|
'Mood': [] |
|
} |
|
df = pd.DataFrame(structure) |
|
|
|
|
|
|
|
def translator_text(text, src_lang): |
|
translation = translator(text, src_lang=src_lang, tgt_lang="eng_Latn") |
|
return translation[0]['translation_text'] |
|
|
|
|
|
|
|
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): |
|
|
|
|
|
if src_lang!= 'English': |
|
text = translator_text(text, languages[src_lang]) |
|
|
|
|
|
mood = classifier(text)[0]['label'] |
|
|
|
|
|
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", |
|
) |
|
|
|
|
|
appender(date, text, mood) |
|
|
|
|
|
highlighted_mood = [(f"Today you're feeling", mood)] |
|
return highlighted_mood, chat_completion.choices[0].message.content |
|
|
|
|
|
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() |
|
) |
|
|
|
demo.launch(debug=True) |