Shahadbal commited on
Commit
e1ad72a
1 Parent(s): 3e207ad

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +95 -0
app.py ADDED
@@ -0,0 +1,95 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from transformers import pipeline
2
+ import pandas as pd
3
+ from openai import OpenAI
4
+ import gradio as gr
5
+ import matplotlib.pyplot as plt
6
+ from dotenv import dotenv_values
7
+
8
+ #This is a model for a multi-label classification task that classifies text into different emotions. It works only in English.
9
+ classifier = pipeline("text-classification", model="SamLowe/roberta-base-go_emotions")
10
+
11
+ # This is a model for a translation task, designed to translate text.
12
+ # We use it to translate any non-English text into English, so the classifier can then classify the emotions.
13
+
14
+ translator = pipeline(task="translation", model="facebook/nllb-200-distilled-600M")
15
+ languages = {
16
+ "English": "eng_Latn",
17
+ "French": "fra_Latn",
18
+ "Arabic": "arb_Arab",
19
+ "Spanish": "spa_Latn",
20
+ "German": "deu_Latn",
21
+ "Chinese (Simplified)": "zho_Hans",
22
+ "Hindi": "hin_Deva"
23
+ }
24
+
25
+ # prepare openAI client with our api key
26
+ env_values = dotenv_values("./app.env")
27
+ client = OpenAI(
28
+ api_key= env_values['OPENAI_API_KEY'],)
29
+
30
+
31
+ # Create a DataFrame to store user entries and perform analysis.
32
+
33
+ structure = {
34
+ 'Date': [],
35
+ 'Text': [],
36
+ 'Mood': []
37
+ }
38
+ df = pd.DataFrame(structure)
39
+
40
+
41
+ # Take the text and its source language, translate it to English, so that the classifier can perform the task.
42
+ def translator_text(text, src_lang):
43
+ translation = translator(text, src_lang=src_lang, tgt_lang="eng_Latn")
44
+ return translation[0]['translation_text']
45
+
46
+
47
+ # Take all the inputs from the user, including the mood (result from the classifier), and append them to the DataFrame.
48
+ def appender(date, text, mood):
49
+ global df
50
+ new_row = pd.DataFrame({'Date': [date], 'Text': [text], 'Mood': [mood]})
51
+ df = pd.concat([df, new_row], ignore_index=True)
52
+
53
+
54
+ def main(date, src_lang, text):
55
+
56
+ # First: Translate the text to English if it is not already in English.
57
+ if src_lang!= 'English':
58
+ text = translator_text(text, languages[src_lang])
59
+
60
+ # Second : Classify the text
61
+ mood = classifier(text)[0]['label']
62
+
63
+ # Third : Show a message to the user depending on how they feel.
64
+ chat_completion = client.chat.completions.create(
65
+ messages=[
66
+ {
67
+ "role": "user",
68
+ "content": f"I feel{mood}, can you tell me a message, without any introductory phrase, just the message itself.",
69
+ }
70
+ ],
71
+ model="gpt-3.5-turbo",
72
+ )
73
+
74
+ # Finally : Save to DataFrame
75
+ appender(date, text, mood)
76
+
77
+ #Highlighted the output utilizing 'HighlightedText' in gradio
78
+ highlighted_mood = [(f"Today you're feeling", mood)]
79
+ return highlighted_mood, chat_completion.choices[0].message.content
80
+
81
+ #Interface
82
+ demo = gr.Interface(
83
+ fn=main,
84
+ 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?")],
85
+ outputs=[gr.HighlightedText(label="Mood"), gr.Textbox(label="Message")],
86
+ title = "Daily Journal",
87
+ description=(
88
+ "Capture your daily experiences, reflections, and insights in a personal journal.\n"
89
+ "Log and monitor your mood daily to identify patterns and trends over time.\n"
90
+ "Get inspirational or motivational messages each day."
91
+ ),
92
+ theme=gr.themes.Soft() # theme form gradio documentation
93
+ )
94
+
95
+ demo.launch(debug=True)