Spaces:
Running
Running
Commit
•
681b50f
1
Parent(s):
9f4a6fe
Update app.py (#1)
Browse files- Update app.py (3923a5c74fd0f3d20d79b96ddaa1897cd022ac62)
Co-authored-by: shecodes <shecodesinsg@users.noreply.huggingface.co>
app.py
CHANGED
@@ -1,4 +1,3 @@
|
|
1 |
-
|
2 |
import pandas as pd
|
3 |
from transformers import AutoTokenizer, AutoModelForSequenceClassification
|
4 |
import random
|
@@ -10,13 +9,10 @@ device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
|
10 |
print(f"Using device: {device}")
|
11 |
|
12 |
# Path to your model file (adjust this if needed)
|
13 |
-
model_path = "bhadresh-savani/bert-base-uncased-emotion"
|
14 |
-
|
15 |
-
#my code
|
16 |
-
# Load pre-trained model and tokenizer
|
17 |
model_name = "bhadresh-savani/bert-base-uncased-emotion"
|
18 |
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
19 |
model = AutoModelForSequenceClassification.from_pretrained(model_name)
|
|
|
20 |
def predict_emotion(text):
|
21 |
# Tokenize the input text
|
22 |
inputs = tokenizer(text, return_tensors="pt", truncation=True, padding=True, max_length=512)
|
@@ -46,50 +42,36 @@ def predict_emotion(text):
|
|
46 |
motivational_quotes = {
|
47 |
"anger": [
|
48 |
"It’s okay to feel angry when things don’t go as expected. Your emotions are valid and show you care deeply. Take a moment to breathe and channel that energy into finding a solution—you’ve got the strength to turn this around!"
|
49 |
-
|
50 |
"joy": [
|
51 |
"Your joy is contagious, and it’s wonderful to see you so happy! Celebrate this moment fully, and let it inspire you to keep reaching for more moments like these."
|
52 |
],
|
53 |
"optimism": [
|
54 |
"Your positive outlook is inspiring! Keep holding onto that hope and belief in yourself—it will guide you through any challenge. The best is yet to come!"
|
55 |
-
|
56 |
"sadness": [
|
57 |
"It’s okay to feel sad—allow yourself to process and heal. Remember, tough times don’t last forever. Lean on those who care about you, and know that brighter days are ahead."
|
58 |
],
|
59 |
"fear": [
|
60 |
"Feeling afraid is natural; it shows you’re stepping outside your comfort zone. Trust in your abilities and take things one step at a time. You’re braver than you think!"
|
61 |
-
|
62 |
-
"surprise"
|
63 |
"Life’s surprises can be startling or exciting, but they often bring new opportunities. Embrace the unexpected with an open mind—you might discover something amazing."
|
64 |
-
|
65 |
-
|
66 |
}
|
67 |
|
68 |
-
#
|
69 |
def get_motivational_quote(emotion):
|
70 |
if emotion in motivational_quotes:
|
71 |
return random.choice(motivational_quotes[emotion])
|
72 |
else:
|
73 |
return "Stay strong and keep moving forward!"
|
74 |
|
75 |
-
date = input("Enter date of entry (DD/MM/YYYY): ")
|
76 |
-
entry = input("Enter your feelings & thoughts: ")
|
77 |
-
|
78 |
-
emotion, confidence = predict_emotion(entry)
|
79 |
-
quote = get_motivational_quote(emotion)
|
80 |
-
|
81 |
-
print(f"\nOn {date}, your detected emotion is: {emotion} (Confidence: {confidence:.2f})")
|
82 |
-
print(f"Here's something to tell yourself: {quote}")
|
83 |
-
print(f"No matter what you’re feeling, today is a fresh start —- embrace it with courage, knowing you have the strength to handle anything that comes your way! Thanks for sharing your feelings!", end='')
|
84 |
-
#end of my code
|
85 |
-
|
86 |
-
|
87 |
def process_entry(date, text):
|
88 |
emotion, confidence = predict_emotion(text)
|
89 |
quote = get_motivational_quote(emotion)
|
90 |
return f"On {date}, your detected emotion is: {emotion} (Confidence: {confidence:.2f})", quote
|
91 |
|
92 |
-
|
93 |
interface = gr.Interface(
|
94 |
fn=process_entry,
|
95 |
inputs=[
|
|
|
|
|
1 |
import pandas as pd
|
2 |
from transformers import AutoTokenizer, AutoModelForSequenceClassification
|
3 |
import random
|
|
|
9 |
print(f"Using device: {device}")
|
10 |
|
11 |
# Path to your model file (adjust this if needed)
|
|
|
|
|
|
|
|
|
12 |
model_name = "bhadresh-savani/bert-base-uncased-emotion"
|
13 |
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
14 |
model = AutoModelForSequenceClassification.from_pretrained(model_name)
|
15 |
+
|
16 |
def predict_emotion(text):
|
17 |
# Tokenize the input text
|
18 |
inputs = tokenizer(text, return_tensors="pt", truncation=True, padding=True, max_length=512)
|
|
|
42 |
motivational_quotes = {
|
43 |
"anger": [
|
44 |
"It’s okay to feel angry when things don’t go as expected. Your emotions are valid and show you care deeply. Take a moment to breathe and channel that energy into finding a solution—you’ve got the strength to turn this around!"
|
45 |
+
],
|
46 |
"joy": [
|
47 |
"Your joy is contagious, and it’s wonderful to see you so happy! Celebrate this moment fully, and let it inspire you to keep reaching for more moments like these."
|
48 |
],
|
49 |
"optimism": [
|
50 |
"Your positive outlook is inspiring! Keep holding onto that hope and belief in yourself—it will guide you through any challenge. The best is yet to come!"
|
51 |
+
],
|
52 |
"sadness": [
|
53 |
"It’s okay to feel sad—allow yourself to process and heal. Remember, tough times don’t last forever. Lean on those who care about you, and know that brighter days are ahead."
|
54 |
],
|
55 |
"fear": [
|
56 |
"Feeling afraid is natural; it shows you’re stepping outside your comfort zone. Trust in your abilities and take things one step at a time. You’re braver than you think!"
|
57 |
+
],
|
58 |
+
"surprise": [
|
59 |
"Life’s surprises can be startling or exciting, but they often bring new opportunities. Embrace the unexpected with an open mind—you might discover something amazing."
|
60 |
+
]
|
|
|
61 |
}
|
62 |
|
63 |
+
# Function to generate motivational quotes
|
64 |
def get_motivational_quote(emotion):
|
65 |
if emotion in motivational_quotes:
|
66 |
return random.choice(motivational_quotes[emotion])
|
67 |
else:
|
68 |
return "Stay strong and keep moving forward!"
|
69 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
70 |
def process_entry(date, text):
|
71 |
emotion, confidence = predict_emotion(text)
|
72 |
quote = get_motivational_quote(emotion)
|
73 |
return f"On {date}, your detected emotion is: {emotion} (Confidence: {confidence:.2f})", quote
|
74 |
|
|
|
75 |
interface = gr.Interface(
|
76 |
fn=process_entry,
|
77 |
inputs=[
|