Vadim Borisov commited on
Commit
7997069
1 Parent(s): a5c3607

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -30
app.py CHANGED
@@ -1,20 +1,25 @@
1
  import gradio as gr
2
- from transformers import AutoTokenizer, AutoModelForSequenceClassification
3
  import torch
 
 
 
 
 
4
 
5
  # Load model and tokenizer
6
  model_name = "tabularisai/robust-sentiment-analysis"
7
  tokenizer = AutoTokenizer.from_pretrained(model_name)
8
  model = AutoModelForSequenceClassification.from_pretrained(model_name)
9
 
10
- # Move model to GPU if available
11
- device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
12
- model = model.to(device)
13
 
14
- # Function to predict sentiment
15
  def predict_sentiment(text):
 
16
  inputs = tokenizer(text.lower(), return_tensors="pt", truncation=True, padding=True, max_length=512)
17
- inputs = {k: v.to(device) for k, v in inputs.items()}
18
 
19
  with torch.no_grad():
20
  outputs = model(**inputs)
@@ -23,31 +28,15 @@ def predict_sentiment(text):
23
  predicted_class = torch.argmax(probabilities, dim=-1).item()
24
 
25
  sentiment_map = {0: "Very Negative", 1: "Negative", 2: "Neutral", 3: "Positive", 4: "Very Positive"}
26
- confidence = probabilities[0][predicted_class].item()
27
-
28
- return sentiment_map[predicted_class], f"{confidence:.2%}"
29
 
30
  # Gradio interface
31
- def gradio_sentiment_analysis(text):
32
- sentiment, confidence = predict_sentiment(text)
33
- return f"Sentiment: {sentiment}\nConfidence: {confidence}"
34
-
35
- # Create Gradio interface
36
- iface = gr.Interface(
37
- fn=gradio_sentiment_analysis,
38
- inputs=gr.Textbox(lines=5, label="Enter text for sentiment analysis"),
39
- outputs=gr.Textbox(label="Result"),
40
- title="Sentiment Analysis",
41
- description="Analyze the sentiment of your text using a 5-class sentiment model.",
42
- theme="huggingface",
43
- examples=[
44
- ["I absolutely loved this movie! The acting was superb and the plot was engaging."],
45
- ["The service at this restaurant was terrible. I'll never go back."],
46
- ["The product works as expected. Nothing special, but it gets the job done."],
47
- ["I'm somewhat disappointed with my purchase. It's not as good as I hoped."],
48
- ["This book changed my life! I couldn't put it down and learned so much."]
49
- ]
50
  )
51
 
52
- # Launch the app
53
- iface.launch()
 
1
  import gradio as gr
2
+ 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
  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()