Pranav0111 commited on
Commit
0d6d702
·
verified ·
1 Parent(s): c1ca45e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +33 -22
app.py CHANGED
@@ -2,6 +2,7 @@ import gradio as gr
2
  from transformers import pipeline
3
  import random
4
  from datetime import datetime
 
5
 
6
  # Initialize models with smaller, faster alternatives
7
  sentiment_analyzer = pipeline(
@@ -10,6 +11,9 @@ sentiment_analyzer = pipeline(
10
  device=-1 # Force CPU usage
11
  )
12
 
 
 
 
13
  # Pre-defined prompts and affirmations for different sentiments
14
  PROMPT_TEMPLATES = {
15
  "POSITIVE": [
@@ -21,6 +25,11 @@ PROMPT_TEMPLATES = {
21
  "- What can you learn from this challenging situation?",
22
  "- What small step could you take to feel better?",
23
  "- Who or what helps you feel supported during difficult times?"
 
 
 
 
 
24
  ]
25
  }
26
 
@@ -34,6 +43,11 @@ AFFIRMATIONS = {
34
  "This too shall pass, and I am growing stronger.",
35
  "I trust in my ability to handle challenging situations.",
36
  "Every experience is teaching me something valuable."
 
 
 
 
 
37
  ]
38
  }
39
 
@@ -42,11 +56,11 @@ class JournalCompanion:
42
  self.entries = []
43
 
44
  def get_prompts(self, sentiment):
45
- prompts = PROMPT_TEMPLATES.get(sentiment, PROMPT_TEMPLATES["POSITIVE"])
46
  return "\n\nReflective Prompts:\n" + "\n".join(prompts)
47
 
48
  def get_affirmation(self, sentiment):
49
- affirmations = AFFIRMATIONS.get(sentiment, AFFIRMATIONS["POSITIVE"])
50
  return random.choice(affirmations)
51
 
52
  def analyze_entry(self, entry_text):
@@ -59,7 +73,7 @@ class JournalCompanion:
59
  sentiment = sentiment_result["label"].upper()
60
  sentiment_score = sentiment_result["score"]
61
  except Exception as e:
62
- print("Error during sentiment analysis:", e)
63
  return (
64
  "An error occurred during analysis. Please try again.",
65
  "Error",
@@ -86,18 +100,28 @@ class JournalCompanion:
86
  def get_monthly_insights(self):
87
  if not self.entries:
88
  return "No entries yet to analyze."
89
-
90
- total_entries = len(self.entries)
91
- positive_entries = sum(1 for entry in self.entries if entry["sentiment"] == "POSITIVE")
 
 
 
 
 
 
 
 
92
 
93
  try:
94
  percentage_positive = (positive_entries / total_entries * 100)
95
- percentage_negative = ((total_entries - positive_entries) / total_entries * 100)
 
96
 
97
  insights = f"""Monthly Insights:
98
  Total Entries: {total_entries}
99
  Positive Entries: {positive_entries} ({percentage_positive:.1f}%)
100
- Negative Entries: {total_entries - positive_entries} ({percentage_negative:.1f}%)
 
101
  """
102
  return insights
103
  except ZeroDivisionError:
@@ -227,17 +251,4 @@ def create_journal_interface():
227
  submit_btn.click(
228
  fn=journal.analyze_entry,
229
  inputs=[entry_input],
230
- outputs=[result_message, sentiment_output, prompt_output, affirmation_output]
231
- )
232
-
233
- insights_btn.click(
234
- fn=journal.get_monthly_insights,
235
- inputs=[],
236
- outputs=[insights_output]
237
- )
238
-
239
- return interface
240
-
241
- if __name__ == "__main__":
242
- interface = create_journal_interface()
243
- interface.launch()
 
2
  from transformers import pipeline
3
  import random
4
  from datetime import datetime
5
+ import logging
6
 
7
  # Initialize models with smaller, faster alternatives
8
  sentiment_analyzer = pipeline(
 
11
  device=-1 # Force CPU usage
12
  )
13
 
14
+ # Configure logging
15
+ logging.basicConfig(level=logging.INFO, format="%(asctime)s - %(levelname)s - %(message)s")
16
+
17
  # Pre-defined prompts and affirmations for different sentiments
18
  PROMPT_TEMPLATES = {
19
  "POSITIVE": [
 
25
  "- What can you learn from this challenging situation?",
26
  "- What small step could you take to feel better?",
27
  "- Who or what helps you feel supported during difficult times?"
28
+ ],
29
+ "NEUTRAL": [
30
+ "- What does this experience teach you about balance?",
31
+ "- How does this experience fit into your overall life story?",
32
+ "- What is something small you are grateful for today?"
33
  ]
34
  }
35
 
 
43
  "This too shall pass, and I am growing stronger.",
44
  "I trust in my ability to handle challenging situations.",
45
  "Every experience is teaching me something valuable."
46
+ ],
47
+ "NEUTRAL": [
48
+ "I appreciate the calmness of the present moment.",
49
+ "I am in harmony with life’s natural flow.",
50
+ "Balance is a gift I cultivate every day."
51
  ]
52
  }
53
 
 
56
  self.entries = []
57
 
58
  def get_prompts(self, sentiment):
59
+ prompts = PROMPT_TEMPLATES.get(sentiment, PROMPT_TEMPLATES["NEUTRAL"])
60
  return "\n\nReflective Prompts:\n" + "\n".join(prompts)
61
 
62
  def get_affirmation(self, sentiment):
63
+ affirmations = AFFIRMATIONS.get(sentiment, AFFIRMATIONS["NEUTRAL"])
64
  return random.choice(affirmations)
65
 
66
  def analyze_entry(self, entry_text):
 
73
  sentiment = sentiment_result["label"].upper()
74
  sentiment_score = sentiment_result["score"]
75
  except Exception as e:
76
+ logging.error("Error during sentiment analysis: %s", e)
77
  return (
78
  "An error occurred during analysis. Please try again.",
79
  "Error",
 
100
  def get_monthly_insights(self):
101
  if not self.entries:
102
  return "No entries yet to analyze."
103
+
104
+ current_month = datetime.now().month
105
+ monthly_entries = [entry for entry in self.entries if datetime.fromisoformat(entry["timestamp"]).month == current_month]
106
+
107
+ total_entries = len(monthly_entries)
108
+ if total_entries == 0:
109
+ return "No entries this month to analyze."
110
+
111
+ positive_entries = sum(1 for entry in monthly_entries if entry["sentiment"] == "POSITIVE")
112
+ neutral_entries = sum(1 for entry in monthly_entries if entry["sentiment"] == "NEUTRAL")
113
+ negative_entries = total_entries - positive_entries - neutral_entries
114
 
115
  try:
116
  percentage_positive = (positive_entries / total_entries * 100)
117
+ percentage_neutral = (neutral_entries / total_entries * 100)
118
+ percentage_negative = (negative_entries / total_entries * 100)
119
 
120
  insights = f"""Monthly Insights:
121
  Total Entries: {total_entries}
122
  Positive Entries: {positive_entries} ({percentage_positive:.1f}%)
123
+ Neutral Entries: {neutral_entries} ({percentage_neutral:.1f}%)
124
+ Negative Entries: {negative_entries} ({percentage_negative:.1f}%)
125
  """
126
  return insights
127
  except ZeroDivisionError:
 
251
  submit_btn.click(
252
  fn=journal.analyze_entry,
253
  inputs=[entry_input],
254
+ outputs=[result