File size: 1,724 Bytes
b554ca5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
from transformers import pipeline

from src.utils import preprocess_text


def summarize_note(note_data):
    """Generate a summary of the note using a pre-trained LLM."""
    try:
        summarizer = pipeline("summarization", model="facebook/bart-large-cnn")
        text = f"{note_data['title']}\n{note_data['note']}"
        summary = summarizer(preprocess_text(text), max_length=100, min_length=20, do_sample=False)[0]["summary_text"]
        return summary
    except Exception as e:
        return f"Error summarizing note: {str(e)}"
    


def assign_tag(note_data):
    """Assign a single tag using zero-shot classification with clearer labels."""
    try:
        classifier = pipeline("zero-shot-classification", model="facebook/bart-large-mnli")
        text = f"{note_data['title']}\n{note_data['note']}"

        # More explicit candidate labels
        candidate_labels = ["Educational session", "Project meeting", "Personal note"]

        result = classifier(preprocess_text(text), candidate_labels, multi_label=False)

        top_label = result["labels"][0]

        return top_label
    except Exception as e:
        return {"error": str(e)}    
    


def analyze_sentiment(note_data) -> dict:
    """Analyze sentiment and map to an emoji."""
    try:
        sentiment_analyzer = pipeline("sentiment-analysis", model="distilbert-base-uncased-finetuned-sst-2-english")
        text = f"{note_data['title']}\n{note_data['note']}"
        sentiment = sentiment_analyzer(preprocess_text(text))[0]
        label, score = sentiment["label"], sentiment["score"]
        return {
            "label": label,
            "score": score,
        }
    except Exception as e:
        return {"error": str(e)}