Daeyongkwon98 commited on
Commit
802f276
0 Parent(s):
User_Intents_and_Musical_Attributes_Classifier ADDED
@@ -0,0 +1 @@
 
 
1
+ Subproject commit e73401a0af87103a963af5b6800e5a6f1bdfe382
app.py ADDED
@@ -0,0 +1,99 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import warnings
2
+ warnings.simplefilter('ignore')
3
+ import numpy as np
4
+ import torch
5
+ import torch.nn as nn
6
+ from transformers import DistilBertTokenizer, DistilBertModel
7
+ import logging
8
+ logging.basicConfig(level=logging.ERROR)
9
+ from torch import cuda
10
+ import gradio as gr
11
+
12
+ def classify(sentence):
13
+ output = ""
14
+ class DistilBERTClass(nn.Module):
15
+ def __init__(self, num_intents):
16
+ super(DistilBERTClass, self).__init__()
17
+ self.l1 = DistilBertModel.from_pretrained("distilbert-base-uncased")
18
+ self.fc1 = nn.Sequential(
19
+ nn.Linear(768, 64),
20
+ nn.BatchNorm1d(64),
21
+ nn.ReLU(),
22
+ )
23
+ self.fc2 = nn.Sequential(
24
+ nn.Linear(64, num_intents)
25
+ )
26
+
27
+ def forward(self, input_ids, attention_mask):
28
+ output_1 = self.l1(input_ids=input_ids, attention_mask=attention_mask)
29
+ hidden_state = output_1[0]
30
+ pooler = hidden_state[:, 0]
31
+ pooler = self.fc1(pooler)
32
+ output = self.fc2(pooler)
33
+ return output
34
+
35
+ user_intents = ['initial_query', 'greeting', 'add_filter', 'remove_filter', 'continue', 'accept_response', 'reject_response']
36
+ musical_attributes = ['track', 'artist', 'year', 'popularity', 'culture', 'similar_track', 'similar_artist', 'user', 'theme', 'mood', 'genre', 'instrument', 'vocal', 'tempo']
37
+ intents_dict = {"user": user_intents, "music": musical_attributes}
38
+ num_intents_dict = {'user': 7, 'music': 14}
39
+
40
+ device = 'cuda:0' if cuda.is_available() else 'cpu'
41
+ tokenizer = DistilBertTokenizer.from_pretrained("distilbert-base-uncased")
42
+
43
+ for data_type in ["user", "music"]:
44
+ num_intents = num_intents_dict[data_type]
45
+
46
+ model = DistilBERTClass(num_intents)
47
+ model.load_state_dict(torch.load(f"./models/{data_type}_finetune_model.pth"))
48
+ model.to(device)
49
+
50
+ # Tokenize the input sentence
51
+ inputs = tokenizer.encode_plus(
52
+ sentence,
53
+ None,
54
+ add_special_tokens=True,
55
+ max_length=128,
56
+ pad_to_max_length=True,
57
+ return_token_type_ids=False,
58
+ return_attention_mask=True,
59
+ truncation=True
60
+ )
61
+
62
+ input_ids = torch.tensor(inputs['input_ids']).unsqueeze(0).to(device)
63
+ attention_mask = torch.tensor(inputs['attention_mask']).unsqueeze(0).to(device)
64
+
65
+ model.eval()
66
+ with torch.no_grad():
67
+ outputs = model(input_ids, attention_mask)
68
+ probability_outputs = torch.sigmoid(outputs).cpu().detach().numpy()
69
+
70
+ binary_outputs = (probability_outputs >= 0.5)
71
+ binary_outputs[np.all(binary_outputs == False, axis=1), -1] = True
72
+
73
+ intents = intents_dict[data_type]
74
+ predicted_intents = [intent for i, intent in enumerate(intents) if binary_outputs[0][i] == 1]
75
+
76
+ if data_type=="user":
77
+ output += f"User Intents: {predicted_intents}\n"
78
+ else:
79
+ output += f"Musical Attributes: {predicted_intents}\n"
80
+ return output
81
+
82
+ title = "User Intents and Musical Attributes Classifier"
83
+ description = """
84
+ You can engage in a conversation with the music recommendation system, imagining a situation where it recommends music to you. The model will then predict the intents and musical attributes based on the sentence you provide.
85
+ <img src="https://github.com/user-attachments/assets/a8bfb1dc-856b-4f85-82dd-510cddcc2aeb" width=400px>
86
+ """
87
+ article = "For more information, visit [Github Repository.](https://github.com/DaeyongKwon98/Intent-Classification/tree/main)"
88
+
89
+ demo = gr.Interface(
90
+ fn=classify,
91
+ inputs="text",
92
+ outputs="text",
93
+ title=title,
94
+ description=description,
95
+ article=article,
96
+ examples=[["Hi, I need a playlist of rock songs to listen when I exercise."], ["I love Ariana Grande! Give me more."], ["I think these are too fast for me."]],
97
+ )
98
+
99
+ demo.launch()
requirements.txt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ torch
2
+ transformers
3
+ gradio