Spaces:
Sleeping
Sleeping
from transformers import pipeline | |
import time | |
# Load once at startup | |
classifier = pipeline( | |
"zero-shot-classification", | |
model="valhalla/distilbart-mnli-12-3" | |
) | |
summarizer = pipeline("summarization", model="sshleifer/distilbart-cnn-12-6") | |
def analyze_article(text, title, link): | |
t0 = time.time() | |
result = classifier( | |
text, | |
candidate_labels=["analytics", "data science", "business insight"], | |
multi_label=True | |
) | |
print(f"[DEBUG] Classifier Model inference time: {time.time() - t0:.2f}s") | |
# Summarize article text | |
summary = summarizer(text[:1024], max_length=150, min_length=40, do_sample=False)[0]['summary_text'] | |
print(f"[DEBUG] Summarizer Model inference time: {time.time() - t0:.2f}s") | |
is_useful = any(label in ["analytics", "data science"] for label in result['labels'][:2]) | |
return { | |
"title": title, | |
"link": link, | |
"summary": summary, | |
"top_label": result['labels'][0], | |
"relevance": "Useful" if is_useful else "Not useful" | |
} | |