quocviethere commited on
Commit
7a1c2c4
β€’
1 Parent(s): 33759e3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +59 -15
app.py CHANGED
@@ -1,30 +1,72 @@
1
  import gradio as gr
2
- from transformers import pipeline
 
3
 
4
- # Initialize the sentiment analysis pipeline with your model
5
- sentiment_pipeline = pipeline("sentiment-analysis", model="quocviethere/imdb-roberta")
 
 
6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7
  def analyze_sentiment(text):
8
- result = sentiment_pipeline(text)[0]
9
- label = result['label']
10
- score = result['score']
11
- sentiment = "Positive 😊" if label == "POSITIVE" else "Negative 😞"
12
- confidence = f"Confidence: {round(score * 100, 2)}%"
13
- return sentiment, confidence
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
14
 
15
- # Define the Gradio interface using the updated API
 
 
16
  iface = gr.Interface(
17
  fn=analyze_sentiment,
18
  inputs=gr.Textbox(
19
  lines=5,
20
- placeholder="Enter a review here...",
21
- label="Review"
22
  ),
23
  outputs=[
24
  gr.Textbox(label="Sentiment"),
25
  gr.Textbox(label="Confidence")
26
  ],
27
- title="Sentiment Analysis",
28
  description="Analyze the sentiment of movie reviews using a fine-tuned RoBERTa model.",
29
  examples=[
30
  ["I loved the cinematography and the story was captivating."],
@@ -33,5 +75,7 @@ iface = gr.Interface(
33
  theme="default"
34
  )
35
 
36
- # Launch the interface
37
- iface.launch()
 
 
 
1
  import gradio as gr
2
+ from transformers import pipeline, AutoModelForSequenceClassification, AutoTokenizer
3
+ import torch
4
 
5
+ # -----------------------------
6
+ # Configuration Section
7
+ # -----------------------------
8
+ MODEL_NAME = "quocviethere/imdb-roberta" # Replace with your actual model ID
9
 
10
+ # -----------------------------
11
+ # Model Loading Section
12
+ # -----------------------------
13
+ try:
14
+ # Load tokenizer and model from Hugging Face Hub
15
+ tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
16
+ model = AutoModelForSequenceClassification.from_pretrained(MODEL_NAME)
17
+
18
+ # Initialize the sentiment analysis pipeline
19
+ sentiment_pipeline = pipeline("sentiment-analysis", model=model, tokenizer=tokenizer)
20
+
21
+ # Verify label mapping
22
+ label_mapping = model.config.id2label
23
+ print(f"Model label mapping: {label_mapping}")
24
+ except Exception as e:
25
+ print(f"Error loading model: {e}")
26
+ raise
27
+
28
+ # -----------------------------
29
+ # Sentiment Analysis Function
30
+ # -----------------------------
31
  def analyze_sentiment(text):
32
+ try:
33
+ # Perform sentiment analysis
34
+ result = sentiment_pipeline(text)[0]
35
+
36
+ # Extract label and score
37
+ label = result['label']
38
+ score = result['score']
39
+
40
+ # Map label to sentiment
41
+ if label in label_mapping.values():
42
+ sentiment = "Positive 😊" if label == "POSITIVE" else "Negative 😞"
43
+ else:
44
+ # Handle unexpected labels
45
+ sentiment = label
46
+ print(f"Unexpected label received: {label}")
47
+
48
+ confidence = f"Confidence: {round(score * 100, 2)}%"
49
+
50
+ return sentiment, confidence
51
+ except Exception as e:
52
+ print(f"Error during sentiment analysis: {e}")
53
+ return "Error", "Could not process the input."
54
 
55
+ # -----------------------------
56
+ # Gradio Interface Section
57
+ # -----------------------------
58
  iface = gr.Interface(
59
  fn=analyze_sentiment,
60
  inputs=gr.Textbox(
61
  lines=5,
62
+ placeholder="Enter a movie review here...",
63
+ label="Movie Review"
64
  ),
65
  outputs=[
66
  gr.Textbox(label="Sentiment"),
67
  gr.Textbox(label="Confidence")
68
  ],
69
+ title="IMDb Sentiment Analysis with RoBERTa",
70
  description="Analyze the sentiment of movie reviews using a fine-tuned RoBERTa model.",
71
  examples=[
72
  ["I loved the cinematography and the story was captivating."],
 
75
  theme="default"
76
  )
77
 
78
+ # -----------------------------
79
+ # Launch the Interface
80
+ # -----------------------------
81
+ iface.launch()