Shahadbal commited on
Commit
f2b0974
1 Parent(s): 11deca6

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +94 -0
app.py ADDED
@@ -0,0 +1,94 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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 the text and its source language, translate it to English, so that the classifier can perform the task.
48
+ def translator_text(text, src_lang):
49
+ translation = translator(text, src_lang=src_lang, tgt_lang="eng_Latn")
50
+ return translation[0]['translation_text']
51
+
52
+
53
+ def main(date, src_lang, text):
54
+
55
+ # First: Translate the text to English if it is not already in English.
56
+ if src_lang!= 'English':
57
+ text = translator_text(text, languages[src_lang])
58
+
59
+ # Second : Classify the text
60
+ mood = classifier(text)[0]['label']
61
+
62
+ # Third : Show a message to the user depending on how they feel.
63
+ chat_completion = client.chat.completions.create(
64
+ messages=[
65
+ {
66
+ "role": "user",
67
+ "content": f"I feel{mood}, can you tell me a message, without any introductory phrase, just the message itself.",
68
+ }
69
+ ],
70
+ model="gpt-3.5-turbo",
71
+ )
72
+
73
+ # Finally : Save to DataFrame
74
+ appender(date, text, mood)
75
+
76
+ #Highlighted the output utilizing 'HighlightedText' in gradio
77
+ highlighted_mood = [(f"Today you're feeling", mood)]
78
+ return highlighted_mood, chat_completion.choices[0].message.content
79
+
80
+ #Interface
81
+ demo = gr.Interface(
82
+ fn=main,
83
+ 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?")],
84
+ outputs=[gr.HighlightedText(label="Mood"), gr.Textbox(label="Message")],
85
+ title = "Daily Journal",
86
+ description=(
87
+ "Capture your daily experiences, reflections, and insights in a personal journal.\n"
88
+ "Log and monitor your mood daily to identify patterns and trends over time.\n"
89
+ "Get inspirational or motivational messages each day."
90
+ ),
91
+ theme=gr.themes.Soft() # theme form gradio documentation
92
+ )
93
+
94
+ demo.launch(debug=True)