saurav384 commited on
Commit
130b0c4
·
verified ·
1 Parent(s): 47fa098

Update services/sentiment.py

Browse files
Files changed (1) hide show
  1. services/sentiment.py +35 -35
services/sentiment.py CHANGED
@@ -1,35 +1,35 @@
1
- from transformers import pipeline
2
-
3
- # Pretrained emotion detection model
4
- # No training required
5
- emotion_classifier = pipeline(
6
- task="text-classification",
7
- model="j-hartmann/emotion-english-distilroberta-base",
8
- return_all_scores=True
9
- )
10
-
11
- def analyze_emotion(ad_text: str):
12
- """
13
- Analyze emotional tone of an ad caption
14
- Returns emotion scores
15
- """
16
-
17
- if not ad_text or len(ad_text.strip()) == 0:
18
- return {"error": "Empty ad text"}
19
-
20
- result = emotion_classifier(ad_text)[0]
21
-
22
- emotions = []
23
- for item in result:
24
- emotions.append({
25
- "emotion": item["label"],
26
- "confidence": round(item["score"], 3)
27
- })
28
-
29
- # Sort by highest confidence
30
- emotions = sorted(emotions, key=lambda x: x["confidence"], reverse=True)
31
-
32
- return {
33
- "ad_text": ad_text,
34
- "emotions": emotions
35
- }
 
1
+ from transformers import pipeline
2
+
3
+ # Pretrained emotion detection model
4
+ # No training required
5
+ emotion_classifier = pipeline(
6
+ task="text-classification",
7
+ model="j-hartmann/emotion-english-distilroberta-base",
8
+ return_all_scores=True
9
+ )
10
+
11
+ def detect_emotion(ad_text: str):
12
+ """
13
+ Analyze emotional tone of an ad caption
14
+ Returns emotion scores
15
+ """
16
+
17
+ if not ad_text or len(ad_text.strip()) == 0:
18
+ return {"error": "Empty ad text"}
19
+
20
+ result = emotion_classifier(ad_text)[0]
21
+
22
+ emotions = []
23
+ for item in result:
24
+ emotions.append({
25
+ "emotion": item["label"],
26
+ "confidence": round(item["score"], 3)
27
+ })
28
+
29
+ # Sort by highest confidence
30
+ emotions = sorted(emotions, key=lambda x: x["confidence"], reverse=True)
31
+
32
+ return {
33
+ "ad_text": ad_text,
34
+ "emotions": emotions
35
+ }