Hasitha16 commited on
Commit
fadde82
·
verified ·
1 Parent(s): 962625d

Update model.py

Browse files
Files changed (1) hide show
  1. model.py +11 -17
model.py CHANGED
@@ -15,16 +15,17 @@ import logging
15
  import re
16
 
17
  # === Pipelines ===
18
- summarizer = pipeline("summarization", model="sshleifer/distilbart-cnn-12-6")
19
  qa_pipeline = pipeline("question-answering", model="distilbert-base-cased-distilled-squad")
20
- emotion_pipeline = pipeline("text-classification", model="bhadresh-savani/distilbert-base-uncased-emotion", top_k=1)
21
 
22
  # === Brief Summarization ===
23
- def summarize_review(text, max_len=80, min_len=20):
24
  try:
25
- return summarizer(text, max_length=max_len, min_length=min_len, do_sample=False)[0]["summary_text"]
 
26
  except Exception as e:
27
- logging.warning(f"Summarization fallback used: {e}")
28
  return text
29
 
30
  # === Smart Summarization with Clustering ===
@@ -54,18 +55,11 @@ def smart_summarize(text, n_clusters=1):
54
 
55
  # === Emotion Detection (Fixed) ===
56
  def detect_emotion(text):
57
- try:
58
- result = emotion_pipeline(text)
59
- if isinstance(result, list) and len(result) > 0:
60
- item = result[0]
61
- if isinstance(item, list): # Nested list case
62
- return item[0]["label"]
63
- return item["label"]
64
- return "neutral"
65
- except Exception as e:
66
- logging.warning(f"Emotion detection failed: {e}")
67
- return "neutral"
68
-
69
  # === Follow-up Q&A ===
70
  def answer_followup(text, question, verbosity="brief"):
71
  try:
 
15
  import re
16
 
17
  # === Pipelines ===
18
+ summarizer = pipeline("summarization", model="google/pegasus-xsum")
19
  qa_pipeline = pipeline("question-answering", model="distilbert-base-cased-distilled-squad")
20
+ emotion_model = pipeline("text-classification", model="j-hartmann/emotion-english-distilroberta-base", top_k=1)
21
 
22
  # === Brief Summarization ===
23
+ def summarize_review(text, max_len=60, min_len=15):
24
  try:
25
+ result = summarizer(text, max_length=max_len, min_length=min_len, do_sample=False)
26
+ return result[0]["summary_text"]
27
  except Exception as e:
28
+ logging.warning(f"Fallback to raw text due to summarization error: {e}")
29
  return text
30
 
31
  # === Smart Summarization with Clustering ===
 
55
 
56
  # === Emotion Detection (Fixed) ===
57
  def detect_emotion(text):
58
+ result = emotion_model(text)
59
+ if isinstance(result, list) and len(result) > 0:
60
+ return result[0]["label"]
61
+ return "neutral"
62
+
 
 
 
 
 
 
 
63
  # === Follow-up Q&A ===
64
  def answer_followup(text, question, verbosity="brief"):
65
  try: