NavyaNayer commited on
Commit
73bdac5
·
verified ·
1 Parent(s): 8320867

Delete task_prioritizer.py

Browse files
Files changed (1) hide show
  1. task_prioritizer.py +0 -105
task_prioritizer.py DELETED
@@ -1,105 +0,0 @@
1
- import torch
2
- from transformers import BertTokenizer, BertForSequenceClassification, DistilBertForSequenceClassification
3
- from datetime import datetime
4
-
5
- device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
6
-
7
- # Load the intent classifier model and tokenizer
8
- num_intent_labels = 151 # Set the correct number of labels for the intent classifier
9
- intent_model = BertForSequenceClassification.from_pretrained("bert-base-uncased", num_labels=num_intent_labels)
10
- intent_model.load_state_dict(torch.load("intent_classifier.pth"))
11
- intent_tokenizer = BertTokenizer.from_pretrained("bert-base-uncased")
12
- intent_model.to(device)
13
- intent_model.eval()
14
-
15
- # Load the emotions model and tokenizer
16
- emotions_model = DistilBertForSequenceClassification.from_pretrained("./saved_model")
17
- emotions_tokenizer = BertTokenizer.from_pretrained("./saved_model")
18
- emotions_model.to(device)
19
- emotions_model.eval()
20
-
21
- # Define the label names for emotions
22
- emotion_label_names = ["admiration", "amusement", "anger", "annoyance", "approval", "caring", "confusion", "curiosity", "desire", "disappointment", "disapproval", "disgust", "embarrassment", "excitement", "fear", "gratitude", "grief", "joy", "love", "nervousness", "optimism", "pride", "realization", "relief", "remorse", "sadness", "surprise", "neutral"]
23
-
24
- def predict_intent(sentence):
25
- inputs = intent_tokenizer(sentence, return_tensors="pt", padding="max_length", truncation=True, max_length=128)
26
- inputs = {key: val.to(device) for key, val in inputs.items()}
27
-
28
- with torch.no_grad():
29
- outputs = intent_model(**inputs)
30
- predicted_class = torch.argmax(outputs.logits, dim=1).cpu().numpy()[0]
31
-
32
- return predicted_class
33
-
34
- def predict_emotion(sentence):
35
- inputs = emotions_tokenizer(sentence, return_tensors="pt", padding="max_length", truncation=True, max_length=128)
36
- inputs = {key: val.to(device) for key, val in inputs.items() if key != "token_type_ids"}
37
-
38
- with torch.no_grad():
39
- outputs = emotions_model(**inputs)
40
- predicted_class = torch.argmax(outputs.logits, dim=1).cpu().numpy()[0]
41
-
42
- return predicted_class, emotion_label_names[predicted_class]
43
-
44
- def calculate_priority_score(intent, emotion, time_remaining):
45
- # Example priority score calculation
46
- intent_weight = 0.4
47
- emotion_weight = 0.3
48
- time_weight = 0.3
49
-
50
- # Normalize time_remaining to a score between 0 and 1
51
- time_score = max(0, min(1, 1 - (time_remaining.total_seconds() / (24 * 3600))))
52
-
53
- # Calculate priority score
54
- priority_score = (intent * intent_weight) + (emotion * emotion_weight) + (time_score * time_weight)
55
- return priority_score
56
-
57
- def prioritize_task(task_description, due_date_time, predicted_emotion, predicted_label_name):
58
- predicted_intent = predict_intent(task_description)
59
-
60
- # Calculate time remaining until the due date and time
61
- due_date_time = datetime.strptime(due_date_time, "%Y-%m-%d %H:%M:%S")
62
- time_remaining = due_date_time - datetime.now()
63
-
64
- priority_score = calculate_priority_score(predicted_intent, predicted_emotion, time_remaining)
65
-
66
- return {
67
- "description": task_description,
68
- "due_date_time": due_date_time,
69
- "time_remaining": time_remaining,
70
- "predicted_intent": predicted_intent,
71
- "predicted_emotion": predicted_emotion,
72
- "predicted_label_name": predicted_label_name,
73
- "priority_score": priority_score
74
- }
75
-
76
- # Example tasks
77
- tasks = [
78
- {"description": "Finish the report by tomorrow.", "due_date_time": "2025-03-02 09:00:00"},
79
- {"description": "meeting", "due_date_time": "2025-03-02 12:00:00"},
80
- {"description": "listen to music.", "due_date_time": "2025-03-02 15:00:00"},
81
- {"description": "daily linkedin queens game.", "due_date_time": "2025-03-02 18:00:00"},
82
- {"description": "prepare ppt", "due_date_time": "2025-03-02 21:00:00"}
83
- ]
84
-
85
- # Overall emotion sentence
86
- emotion_sentence = "I am feeling very tired and stressed now"
87
- predicted_emotion, predicted_label_name = predict_emotion(emotion_sentence)
88
-
89
- # Prioritize tasks
90
- prioritized_tasks = []
91
- for task in tasks:
92
- prioritized_tasks.append(prioritize_task(task["description"], task["due_date_time"], predicted_emotion, predicted_label_name))
93
-
94
- # Reorder tasks based on priority score (descending order)
95
- prioritized_tasks.sort(key=lambda x: x["priority_score"], reverse=True)
96
-
97
- # Print prioritized tasks
98
- for task in prioritized_tasks:
99
- print(f"Task Description: '{task['description']}'")
100
- print(f"Due Date and Time: {task['due_date_time']}")
101
- print(f"Time Remaining: {task['time_remaining']}")
102
- print(f"Predicted Intent: {task['predicted_intent']}")
103
- print(f"Predicted Emotion: {task['predicted_emotion']} ({task['predicted_label_name']})")
104
- print(f"Priority Score: {task['priority_score']:.4f}")
105
- print()