ShanukaB commited on
Commit
77d99dc
Β·
verified Β·
1 Parent(s): 70521f4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +46 -37
app.py CHANGED
@@ -7,6 +7,8 @@ import shutil
7
  from pathlib import Path
8
  from huggingface_hub import hf_hub_download
9
  from transformers import pipeline, AutoModelForSequenceClassification, AutoTokenizer
 
 
10
 
11
  logging.basicConfig(level=logging.INFO, format="%(asctime)s - %(levelname)s - %(message)s")
12
 
@@ -15,6 +17,38 @@ app = FastAPI()
15
  models = None
16
  en_emotion_map = None
17
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
18
  def load_models():
19
  global models, en_emotion_map
20
  if models is not None:
@@ -23,7 +57,7 @@ def load_models():
23
  logging.info("πŸ“₯ Loading models...")
24
 
25
  try:
26
- # English & Sinhala (unchanged)
27
  en_repo = "E-motionAssistant/English_LR_Model_New"
28
  en_vectorizer = joblib.load(hf_hub_download(en_repo, "tfidf_vectorizer.joblib"))
29
  en_classifier = joblib.load(hf_hub_download(en_repo, "logreg_model.joblib"))
@@ -36,42 +70,29 @@ def load_models():
36
  except:
37
  en_emotion_map = None
38
 
 
39
  si_vectorizer = joblib.load(hf_hub_download("E-motionAssistant/Sinhala_Text_Emotion_Model_LR", "tfidf_vectorizer.joblib"))
40
  si_classifier = joblib.load(hf_hub_download("E-motionAssistant/Sinhala_Text_Emotion_Model_LR", "logreg_model.joblib"))
41
  si_label_encoder = joblib.load(hf_hub_download("E-motionAssistant/Sinhala_Text_Emotion_Model_LR", "label_encoder.joblib"))
42
 
43
- # ====================== TAMIL - STRONG FIX ======================
44
- logging.info("πŸ“₯ Loading Tamil model with manual components...")
45
-
46
- # Clean cache
47
  try:
48
  cache_dir = Path.home() / ".cache" / "huggingface" / "hub"
49
  model_cache = cache_dir / "models--E-motionAssistant--Tamil_Emotion_Recognition_Model"
50
  if model_cache.exists():
51
  shutil.rmtree(model_cache)
52
- logging.info("🧹 Cleaned Tamil cache")
53
  except:
54
  pass
55
 
56
- # Load manually (more reliable than pipeline sometimes)
57
  model_name = "E-motionAssistant/Tamil_Emotion_Recognition_Model"
58
  tokenizer = AutoTokenizer.from_pretrained(model_name)
59
  model = AutoModelForSequenceClassification.from_pretrained(model_name)
60
 
61
- tamil_pipe = pipeline(
62
- "text-classification",
63
- model=model,
64
- tokenizer=tokenizer,
65
- device=-1,
66
- truncation=True,
67
- max_length=512,
68
- top_k=1
69
- )
70
 
71
- logging.info("βœ… Tamil model loaded with manual tokenizer & model")
72
-
73
- models = (en_vectorizer, en_classifier, en_label_encoder,
74
- si_vectorizer, si_classifier, si_label_encoder, tamil_pipe)
75
 
76
  logging.info("πŸŽ‰ All models loaded successfully!")
77
  return models
@@ -116,28 +137,16 @@ def predict(req: PredictRequest):
116
  return {"emotion": emotion, "language": "English"}
117
 
118
  elif lang == "sinhala":
119
- X = si_vec.transform([req.text])
120
- pred = si_clf.predict(X)[0]
121
- emotion = si_le.inverse_transform([pred])[0]
122
- return {"emotion": str(emotion), "language": "Sinhala"}
123
 
124
  elif lang == "tamil":
125
- logging.info(f"Tamil input: {req.text[:200]}...")
126
-
127
  result = tamil_pipe(req.text)
128
-
129
- logging.info(f"Tamil raw result: {result}")
130
-
131
  emotion = result[0]['label']
132
  score = round(float(result[0]['score']), 4)
133
-
134
- logging.info(f"Tamil Final β†’ {emotion} ({score})")
135
-
136
- return {
137
- "emotion": emotion,
138
- "confidence": score,
139
- "language": "Tamil"
140
- }
141
 
142
  except Exception as e:
143
  logging.error(f"Error: {e}")
 
7
  from pathlib import Path
8
  from huggingface_hub import hf_hub_download
9
  from transformers import pipeline, AutoModelForSequenceClassification, AutoTokenizer
10
+ from groq import Groq # ← NEW for Groq API
11
+ import os
12
 
13
  logging.basicConfig(level=logging.INFO, format="%(asctime)s - %(levelname)s - %(message)s")
14
 
 
17
  models = None
18
  en_emotion_map = None
19
 
20
+ # ====================== GROQ CLIENT ======================
21
+ groq_client = Groq(api_key=os.getenv("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"))
22
+
23
+ def get_emotion_from_groq(text: str):
24
+ """Use Groq API to detect emotion for Sinhala text"""
25
+ try:
26
+ prompt = f"""
27
+ You are an expert emotion detector.
28
+ Analyze the emotion of the following Sinhala text and return ONLY one word from this list:
29
+ joy, sadness, anger, fear, surprise, disgust, love, neutral.
30
+
31
+ Text: {text}
32
+
33
+ Emotion:
34
+ """
35
+
36
+ response = groq_client.chat.completions.create(
37
+ model="llama-3.3-70b-versatile", # Fast and good at this task
38
+ messages=[{"role": "user", "content": prompt}],
39
+ temperature=0.1,
40
+ max_tokens=20
41
+ )
42
+
43
+ emotion = response.choices[0].message.content.strip().lower()
44
+ logging.info(f"Groq API returned emotion: {emotion}")
45
+ return emotion
46
+
47
+ except Exception as e:
48
+ logging.error(f"Groq API error: {e}")
49
+ return "neutral" # safe fallback
50
+
51
+
52
  def load_models():
53
  global models, en_emotion_map
54
  if models is not None:
 
57
  logging.info("πŸ“₯ Loading models...")
58
 
59
  try:
60
+ # English
61
  en_repo = "E-motionAssistant/English_LR_Model_New"
62
  en_vectorizer = joblib.load(hf_hub_download(en_repo, "tfidf_vectorizer.joblib"))
63
  en_classifier = joblib.load(hf_hub_download(en_repo, "logreg_model.joblib"))
 
70
  except:
71
  en_emotion_map = None
72
 
73
+ # Sinhala (still loaded but not used for prediction)
74
  si_vectorizer = joblib.load(hf_hub_download("E-motionAssistant/Sinhala_Text_Emotion_Model_LR", "tfidf_vectorizer.joblib"))
75
  si_classifier = joblib.load(hf_hub_download("E-motionAssistant/Sinhala_Text_Emotion_Model_LR", "logreg_model.joblib"))
76
  si_label_encoder = joblib.load(hf_hub_download("E-motionAssistant/Sinhala_Text_Emotion_Model_LR", "label_encoder.joblib"))
77
 
78
+ # Tamil
79
+ logging.info("πŸ“₯ Loading Tamil model...")
 
 
80
  try:
81
  cache_dir = Path.home() / ".cache" / "huggingface" / "hub"
82
  model_cache = cache_dir / "models--E-motionAssistant--Tamil_Emotion_Recognition_Model"
83
  if model_cache.exists():
84
  shutil.rmtree(model_cache)
 
85
  except:
86
  pass
87
 
 
88
  model_name = "E-motionAssistant/Tamil_Emotion_Recognition_Model"
89
  tokenizer = AutoTokenizer.from_pretrained(model_name)
90
  model = AutoModelForSequenceClassification.from_pretrained(model_name)
91
 
92
+ tamil_pipe = pipeline("text-classification", model=model, tokenizer=tokenizer, device=-1, truncation=True, max_length=512)
 
 
 
 
 
 
 
 
93
 
94
+ models = (en_vectorizer, en_clf=en_classifier, en_le=en_label_encoder,
95
+ si_vec=si_vectorizer, si_clf=si_classifier, si_le=si_label_encoder, tamil_pipe=tamil_pipe)
 
 
96
 
97
  logging.info("πŸŽ‰ All models loaded successfully!")
98
  return models
 
137
  return {"emotion": emotion, "language": "English"}
138
 
139
  elif lang == "sinhala":
140
+ # === USE GROQ API FOR CORRECT PREDICTION ===
141
+ emotion = get_emotion_from_groq(req.text)
142
+ logging.info(f"Sinhala final emotion from Groq: {emotion}")
143
+ return {"emotion": emotion, "language": "Sinhala"}
144
 
145
  elif lang == "tamil":
 
 
146
  result = tamil_pipe(req.text)
 
 
 
147
  emotion = result[0]['label']
148
  score = round(float(result[0]['score']), 4)
149
+ return {"emotion": emotion, "confidence": score, "language": "Tamil"}
 
 
 
 
 
 
 
150
 
151
  except Exception as e:
152
  logging.error(f"Error: {e}")