Views
No views yet
distilbert/distilbert-base-uncased for sentiment analysis. Trained only on syntethic data.1from transformers import AutoTokenizer, AutoModelForSequenceClassification
2import torch
3
4# Load model and tokenizer
5model_name = "tabularisai/robust-sentiment-analysis"
6tokenizer = AutoTokenizer.from_pretrained(model_name)
7model = AutoModelForSequenceClassification.from_pretrained(model_name)
8
9# Function to predict sentiment
10def predict_sentiment(text):
11 inputs = tokenizer(text.lower(), return_tensors="pt", truncation=True, padding=True, max_length=512)
12 with torch.no_grad():
13 outputs = model(**inputs)
14
15 probabilities = torch.nn.functional.softmax(outputs.logits, dim=-1)
16 predicted_class = torch.argmax(probabilities, dim=-1).item()
17
18 sentiment_map = {0: "Very Negative", 1: "Negative", 2: "Neutral", 3: "Positive", 4: "Very Positive"}
19 return sentiment_map[predicted_class]
20
21# Example usage
22texts = [
23 "I absolutely loved this movie! The acting was superb and the plot was engaging.",
24 "The service at this restaurant was terrible. I'll never go back.",
25 "The product works as expected. Nothing special, but it gets the job done.",
26 "I'm somewhat disappointed with my purchase. It's not as good as I hoped.",
27 "This book changed my life! I couldn't put it down and learned so much."
28]
29
30for text in texts:
31 sentiment = predict_sentiment(text)
32 print(f"Text: {text}")
33 print(f"Sentiment: {sentiment}\n")1. "I absolutely loved this movie! The acting was superb and the plot was engaging."
Predicted Sentiment: Very Positive
2. "The service at this restaurant was terrible. I'll never go back."
Predicted Sentiment: Very Negative
3. "The product works as expected. Nothing special, but it gets the job done."
Predicted Sentiment: Neutral
4. "I'm somewhat disappointed with my purchase. It's not as good as I hoped."
Predicted Sentiment: Negative
5. "This book changed my life! I couldn't put it down and learned so much."
Predicted Sentiment: Very Positive1<!DOCTYPE html>
2<html lang="en">
3<head>
4 <meta charset="UTF-8">
5 <title>Tabularis Sentiment Analysis</title>
6</head>
7<body>
8 <div id="output"></div>
9
10 <script type="module">
11 import { AutoTokenizer, AutoModel, env } from 'https://cdn.jsdelivr.net/npm/@xenova/transformers@2.6.0';
12
13 env.allowLocalModels = false;
14 env.useCDN = true;
15
16 const MODEL_NAME = 'tabularisai/robust-sentiment-analysis';
17
18 function softmax(arr) {
19 const max = Math.max(...arr);
20 const exp = arr.map(x => Math.exp(x - max));
21 const sum = exp.reduce((acc, val) => acc + val);
22 return exp.map(x => x / sum);
23 }
24
25 async function analyzeSentiment() {
26 try {
27 const tokenizer = await AutoTokenizer.from_pretrained(MODEL_NAME);
28 const model = await AutoModel.from_pretrained(MODEL_NAME);
29
30 const texts = [
31 "I absolutely loved this movie! The acting was superb and the plot was engaging.",
32 "The service at this restaurant was terrible. I'll never go back.",
33 "The product works as expected. Nothing special, but it gets the job done.",
34 "I'm somewhat disappointed with my purchase. It's not as good as I hoped.",
35 "This book changed my life! I couldn't put it down and learned so much."
36 ];
37
38 const output = document.getElementById('output');
39
40 for (const text of texts) {
41 const inputs = await tokenizer(text, { return_tensors: 'pt' });
42 const result = await model(inputs);
43
44 console.log('Model output:', result);
45
46 if (result.output && result.output.data) {
47 const logitsArray = Array.from(result.output.data);
48 console.log('Logits array:', logitsArray);
49
50 const probabilities = softmax(logitsArray);
51 const predicted_class = probabilities.indexOf(Math.max(...probabilities));
52
53 const sentimentMap = {
54 0: "Very Negative",
55 1: "Negative",
56 2: "Neutral",
57 3: "Positive",
58 4: "Very Positive"
59 };
60
61 const sentiment = sentimentMap[predicted_class];
62 const score = probabilities[predicted_class];
63
64 output.innerHTML += ``;
65 output.innerHTML += ``;
66 } else {
67 console.error('Unexpected model output structure:', result);
68 output.innerHTML += ``;
69 }
70 }
71 } catch (error) {
72 console.error('Error:', error);
73 document.getElementById('output').innerHTML = 'An error occurred. Please check the console for details.';
74 }
75 }
76
77 analyzeSentiment();
78 </script>
79</body>
80</html>distilbert/distilbert-base-uncased architecture. The training process involved:Will be includedinfo@tabularis.ai