jkmaina commited on
Commit
7e54273
·
1 Parent(s): da38f58

Movie Review Sentiment Analysis - BERT

Browse files
Files changed (2) hide show
  1. app-old.py +8 -0
  2. app.py +57 -5
app-old.py ADDED
@@ -0,0 +1,8 @@
 
 
 
 
 
 
 
 
 
1
+ #lesson81.py
2
+ import gradio as gr
3
+
4
+ def sentiment_analysis(text):
5
+ return "Positive" if "good" in text.lower() else "Negative"
6
+
7
+ iface = gr.Interface(fn=sentiment_analysis, inputs="text", outputs="text")
8
+ iface.launch()
app.py CHANGED
@@ -1,8 +1,60 @@
1
- #lesson81.py
2
  import gradio as gr
 
3
 
4
- def sentiment_analysis(text):
5
- return "Positive" if "good" in text.lower() else "Negative"
 
6
 
7
- iface = gr.Interface(fn=sentiment_analysis, inputs="text", outputs="text")
8
- iface.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Import required libraries
2
  import gradio as gr
3
+ from transformers import pipeline
4
 
5
+ def create_sentiment_analyzer():
6
+ """Initialize the BERT sentiment analyzer"""
7
+ return pipeline("sentiment-analysis", model="zavora/bert-sentiment-imdb")
8
 
9
+ def analyze_sentiment(text, classifier):
10
+ """
11
+ Analyze sentiment of input text using BERT model
12
+ Returns sentiment and confidence score
13
+ """
14
+ try:
15
+ if not text.strip():
16
+ return "Please enter some text"
17
+
18
+ result = classifier(text)[0]
19
+ label = result['label']
20
+ confidence = result['score']
21
+
22
+ sentiment = "Positive" if label == "LABEL_1" else "Negative"
23
+ # Format the output
24
+ return f"Sentiment: {sentiment}\nConfidence: {confidence:.2%}"
25
+
26
+ except Exception as e:
27
+ return f"Error: {str(e)}"
28
+
29
+ # Create and cache the classifier
30
+ classifier = create_sentiment_analyzer()
31
+
32
+ # Create the Gradio interface
33
+ demo = gr.Interface(
34
+ fn=lambda text: analyze_sentiment(text, classifier),
35
+ inputs=[
36
+ gr.Textbox(
37
+ lines=4,
38
+ placeholder="Enter your movie review here...",
39
+ label="Movie Review"
40
+ )
41
+ ],
42
+ outputs=[
43
+ gr.Textbox(
44
+ label="Analysis Result"
45
+ )
46
+ ],
47
+ title="Movie Review Sentiment Analysis",
48
+ description="""This app uses a BERT model fine-tuned on IMDB movie reviews to analyze sentiment.
49
+ Enter your movie review and get an analysis of whether it's positive or negative.""",
50
+ examples=[
51
+ ["This movie was absolutely fantastic! The acting was superb and the plot kept me engaged throughout."],
52
+ ["I couldn't sit through this movie. The plot was confusing and the acting was terrible."],
53
+ ["While the movie had some good moments, overall it was just average."]
54
+ ],
55
+ theme=gr.themes.Soft()
56
+ )
57
+
58
+ # Launch the app
59
+ if __name__ == "__main__":
60
+ demo.launch()