Spaces:
Running
Running
Update model.py
Browse files
model.py
CHANGED
@@ -12,29 +12,26 @@ from nltk.tokenize import sent_tokenize
|
|
12 |
from transformers import pipeline
|
13 |
import numpy as np
|
14 |
|
15 |
-
# ===
|
16 |
-
summarizer = pipeline("summarization", model="
|
17 |
qa_pipeline = pipeline("question-answering", model="distilbert-base-cased-distilled-squad")
|
18 |
emotion_pipeline = pipeline("text-classification", model="bhadresh-savani/distilbert-base-uncased-emotion", top_k=1)
|
19 |
|
20 |
-
# === Summarization
|
21 |
def summarize_review(text, max_len=60, min_len=10):
|
22 |
return summarizer(text, max_length=max_len, min_length=min_len, do_sample=False)[0]["summary_text"]
|
23 |
|
|
|
24 |
def smart_summarize(text, n_clusters=1):
|
25 |
sentences = sent_tokenize(text)
|
26 |
if len(sentences) <= 1:
|
27 |
return text
|
28 |
-
|
29 |
tfidf = TfidfVectorizer(stop_words="english")
|
30 |
tfidf_matrix = tfidf.fit_transform(sentences)
|
31 |
-
|
32 |
if len(sentences) <= n_clusters:
|
33 |
return " ".join(sentences)
|
34 |
-
|
35 |
kmeans = KMeans(n_clusters=n_clusters, random_state=42).fit(tfidf_matrix)
|
36 |
summary_sentences = []
|
37 |
-
|
38 |
for i in range(n_clusters):
|
39 |
idx = np.where(kmeans.labels_ == i)[0]
|
40 |
if not len(idx):
|
@@ -43,7 +40,6 @@ def smart_summarize(text, n_clusters=1):
|
|
43 |
sim = cosine_similarity(avg_vector, tfidf_matrix[idx].toarray())
|
44 |
most_representative = sentences[idx[np.argmax(sim)]]
|
45 |
summary_sentences.append(most_representative)
|
46 |
-
|
47 |
return " ".join(sorted(summary_sentences, key=sentences.index))
|
48 |
|
49 |
# === Emotion Detection ===
|
@@ -76,6 +72,15 @@ def answer_followup(text, question, verbosity="brief"):
|
|
76 |
except Exception:
|
77 |
return "Sorry, I couldn't generate a follow-up answer."
|
78 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
79 |
# === Optional Explanation Generator ===
|
80 |
def generate_explanation(text):
|
81 |
try:
|
|
|
12 |
from transformers import pipeline
|
13 |
import numpy as np
|
14 |
|
15 |
+
# === Pipelines ===
|
16 |
+
summarizer = pipeline("summarization", model="csebuetnlp/mT5_multilingual_XLSum")
|
17 |
qa_pipeline = pipeline("question-answering", model="distilbert-base-cased-distilled-squad")
|
18 |
emotion_pipeline = pipeline("text-classification", model="bhadresh-savani/distilbert-base-uncased-emotion", top_k=1)
|
19 |
|
20 |
+
# === Summarization (Brief) ===
|
21 |
def summarize_review(text, max_len=60, min_len=10):
|
22 |
return summarizer(text, max_length=max_len, min_length=min_len, do_sample=False)[0]["summary_text"]
|
23 |
|
24 |
+
# === Smart Summarization ===
|
25 |
def smart_summarize(text, n_clusters=1):
|
26 |
sentences = sent_tokenize(text)
|
27 |
if len(sentences) <= 1:
|
28 |
return text
|
|
|
29 |
tfidf = TfidfVectorizer(stop_words="english")
|
30 |
tfidf_matrix = tfidf.fit_transform(sentences)
|
|
|
31 |
if len(sentences) <= n_clusters:
|
32 |
return " ".join(sentences)
|
|
|
33 |
kmeans = KMeans(n_clusters=n_clusters, random_state=42).fit(tfidf_matrix)
|
34 |
summary_sentences = []
|
|
|
35 |
for i in range(n_clusters):
|
36 |
idx = np.where(kmeans.labels_ == i)[0]
|
37 |
if not len(idx):
|
|
|
40 |
sim = cosine_similarity(avg_vector, tfidf_matrix[idx].toarray())
|
41 |
most_representative = sentences[idx[np.argmax(sim)]]
|
42 |
summary_sentences.append(most_representative)
|
|
|
43 |
return " ".join(sorted(summary_sentences, key=sentences.index))
|
44 |
|
45 |
# === Emotion Detection ===
|
|
|
72 |
except Exception:
|
73 |
return "Sorry, I couldn't generate a follow-up answer."
|
74 |
|
75 |
+
# === Fast follow-up (no formatting, for /followup) ===
|
76 |
+
def answer_only(text, question):
|
77 |
+
try:
|
78 |
+
if not question:
|
79 |
+
return "No question provided."
|
80 |
+
return qa_pipeline({"question": question, "context": text}).get("answer", "No answer found.")
|
81 |
+
except Exception:
|
82 |
+
return "Q&A failed."
|
83 |
+
|
84 |
# === Optional Explanation Generator ===
|
85 |
def generate_explanation(text):
|
86 |
try:
|