ksadarsh963 commited on
Commit
20d93df
·
verified ·
1 Parent(s): 6105a94

Update text_ai.py

Browse files
Files changed (1) hide show
  1. text_ai.py +25 -62
text_ai.py CHANGED
@@ -1,69 +1,32 @@
1
- # text_ai.py
2
- import speech_recognition as sr
3
- from deep_translator import GoogleTranslator
4
- from transformers import pipeline, AutoTokenizer, AutoModelForSequenceClassification
5
- import os
6
 
7
  class TextEngine:
8
  def __init__(self):
9
- print("[Text AI] Initializing Cloud DeBERTa Model...")
 
 
 
10
 
11
- # Don't forget to initialize the speech recognizer!
12
- self.recognizer = sr.Recognizer()
13
-
14
- model_id = "cross-encoder/nli-deberta-v3-small"
 
 
 
 
 
 
 
 
 
 
15
 
16
- try:
17
- self.tokenizer = AutoTokenizer.from_pretrained(model_id, use_fast=False)
18
- self.model = AutoModelForSequenceClassification.from_pretrained(model_id)
19
-
20
- print("[Text AI] Engine Ready & Loaded Successfully.")
21
- except Exception as e:
22
- print(f"❌ CRITICAL ERROR: Failed to fetch DeBERTa: {e}")
23
 
24
  def transcribe_and_translate(self, audio_path):
25
- """ Reads audio file -> Text -> English Text """
26
- try:
27
- with sr.AudioFile(audio_path) as source:
28
- audio_data = self.recognizer.record(source)
29
-
30
- # Speech to Text
31
- text = self.recognizer.recognize_google(audio_data)
32
- print(f"[Text AI] Transcribed: {text}")
33
-
34
- # Translate to English (if needed)
35
- translated_text = self.translator.translate(text)
36
- if translated_text != text:
37
- print(f"[Text AI] Translated: {translated_text}")
38
-
39
- return translated_text
40
-
41
- except sr.UnknownValueError:
42
- print("[Text AI] Audio was silent or unintelligible.")
43
- return ""
44
- except Exception as e:
45
- print(f"[Text AI] Audio Error: {e}")
46
- return ""
47
-
48
- def analyze_text(self, text):
49
- """ Analyzes the emotion of the English text """
50
- if not text or self.classifier is None:
51
- return None
52
-
53
- # 1. Run the Model
54
- results = self.classifier(text)[0]
55
- raw_probs = {item['label']: item['score'] for item in results}
56
-
57
- # 2. Map Professional Labels (lowercase) to Tracemind Labels (Capitalized)
58
- # The model uses 'joy', 'anger' -> We need 'Happy', 'Angry'
59
- final_probs = {
60
- 'Angry': raw_probs.get('anger', 0.0),
61
- 'Disgust': raw_probs.get('disgust', 0.0),
62
- 'Fear': raw_probs.get('fear', 0.0),
63
- 'Happy': raw_probs.get('joy', 0.0), # Map 'joy' to 'Happy'
64
- 'Neutral': raw_probs.get('neutral', 0.0),
65
- 'Sad': raw_probs.get('sadness', 0.0), # Map 'sadness' to 'Sad'
66
- 'Surprise': raw_probs.get('surprise', 0.0)
67
- }
68
-
69
- return final_probs
 
1
+ from transformers import pipeline
2
+ import config as cfg
 
 
 
3
 
4
  class TextEngine:
5
  def __init__(self):
6
+ # Using a specialized emotion model
7
+ self.classifier = pipeline("text-classification",
8
+ model="j-hartmann/emotion-english-distilroberta-base",
9
+ return_all_scores=True)
10
 
11
+ # Map model labels to TraceMind internal labels
12
+ self.mapping = {
13
+ 'anger': 'Angry',
14
+ 'disgust': 'Disgust',
15
+ 'fear': 'Fear',
16
+ 'joy': 'Happy',
17
+ 'neutral': 'Neutral',
18
+ 'sadness': 'Sad',
19
+ 'surprise': 'Surprise'
20
+ }
21
+
22
+ def analyze_text(self, text):
23
+ if not text:
24
+ return {label: 0.0 for label in ['Angry', 'Disgust', 'Fear', 'Happy', 'Neutral', 'Sad', 'Surprise']}
25
 
26
+ raw_results = self.classifier(text)[0]
27
+ # Convert and capitalize labels to match ai.py
28
+ return {self.mapping[item['label']]: item['score'] for item in raw_results}
 
 
 
 
29
 
30
  def transcribe_and_translate(self, audio_path):
31
+ # (Keep your existing Whisper/Translation logic here)
32
+ return "Sample transcribed text for demo"