Vadim Borisov commited on
Commit
3ef995c
β€’
1 Parent(s): 7997069

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +32 -13
app.py CHANGED
@@ -3,23 +3,20 @@ import spaces
3
  import torch
4
  from transformers import AutoTokenizer, AutoModelForSequenceClassification
5
 
6
- # Initialize GPU
7
  zero = torch.Tensor([0]).cuda()
8
  print(f"Initial device: {zero.device}")
9
 
10
  # Load model and tokenizer
11
  model_name = "tabularisai/robust-sentiment-analysis"
12
  tokenizer = AutoTokenizer.from_pretrained(model_name)
13
- model = AutoModelForSequenceClassification.from_pretrained(model_name)
14
-
15
- # Move model to GPU
16
- model = model.to(zero.device)
17
 
18
  @spaces.GPU
19
- def predict_sentiment(text):
20
  print(f"Device inside function: {zero.device}")
21
- inputs = tokenizer(text.lower(), return_tensors="pt", truncation=True, padding=True, max_length=512)
22
- inputs = {k: v.to(zero.device) for k, v in inputs.items()}
23
 
24
  with torch.no_grad():
25
  outputs = model(**inputs)
@@ -28,15 +25,37 @@ def predict_sentiment(text):
28
  predicted_class = torch.argmax(probabilities, dim=-1).item()
29
 
30
  sentiment_map = {0: "Very Negative", 1: "Negative", 2: "Neutral", 3: "Positive", 4: "Very Positive"}
31
- return sentiment_map[predicted_class]
 
 
 
 
 
 
 
 
 
 
 
32
 
33
- # Gradio interface
34
  demo = gr.Interface(
35
  fn=predict_sentiment,
36
- inputs=gr.Textbox(label="Enter your text here"),
37
- outputs=gr.Textbox(label="Sentiment"),
 
 
 
38
  title="🎭 Sentiment Analysis Wizard",
39
- description="Discover the emotional tone behind any text with our advanced AI model!"
 
 
 
 
 
 
 
 
40
  )
41
 
42
  demo.launch()
 
3
  import torch
4
  from transformers import AutoTokenizer, AutoModelForSequenceClassification
5
 
6
+ # Initialize GPU tensor
7
  zero = torch.Tensor([0]).cuda()
8
  print(f"Initial device: {zero.device}")
9
 
10
  # Load model and tokenizer
11
  model_name = "tabularisai/robust-sentiment-analysis"
12
  tokenizer = AutoTokenizer.from_pretrained(model_name)
13
+ model = AutoModelForSequenceClassification.from_pretrained(model_name).cuda()
 
 
 
14
 
15
  @spaces.GPU
16
+ def predict_sentiment(text, show_probabilities=False):
17
  print(f"Device inside function: {zero.device}")
18
+
19
+ inputs = tokenizer(text, return_tensors="pt", truncation=True, padding=True, max_length=512).to(zero.device)
20
 
21
  with torch.no_grad():
22
  outputs = model(**inputs)
 
25
  predicted_class = torch.argmax(probabilities, dim=-1).item()
26
 
27
  sentiment_map = {0: "Very Negative", 1: "Negative", 2: "Neutral", 3: "Positive", 4: "Very Positive"}
28
+ predicted_sentiment = sentiment_map[predicted_class]
29
+
30
+ confidence = probabilities[0][predicted_class].item()
31
+
32
+ result = f"Sentiment: {predicted_sentiment}\nConfidence: {confidence:.2%}\n\n"
33
+
34
+ if show_probabilities:
35
+ result += "Probabilities for each class:\n"
36
+ for i, (sentiment, prob) in enumerate(zip(sentiment_map.values(), probabilities[0])):
37
+ result += f"{sentiment}: {prob.item():.2%}\n"
38
+
39
+ return result
40
 
41
+ # Create Gradio interface
42
  demo = gr.Interface(
43
  fn=predict_sentiment,
44
+ inputs=[
45
+ gr.Textbox(lines=5, label="Enter text for sentiment analysis"),
46
+ gr.Checkbox(label="Show probabilities for each class")
47
+ ],
48
+ outputs=gr.Textbox(label="Result"),
49
  title="🎭 Sentiment Analysis Wizard",
50
+ description="Discover the emotional tone behind any text with our advanced AI model! This app uses a state-of-the-art language model to analyze the sentiment of your text, classifying it into one of five categories: Very Negative, Negative, Neutral, Positive, or Very Positive.",
51
+ examples=[
52
+ ["I absolutely loved this movie! The acting was superb and the plot was engaging.", True],
53
+ ["The service at this restaurant was terrible. I'll never go back.", False],
54
+ ["The product works as expected. Nothing special, but it gets the job done.", True],
55
+ ["I'm somewhat disappointed with my purchase. It's not as good as I hoped.", False],
56
+ ["This book changed my life! I couldn't put it down and learned so much.", True]
57
+ ],
58
+ theme=gr.themes.Soft()
59
  )
60
 
61
  demo.launch()