davidcompsc commited on
Commit
1e874a3
·
verified ·
1 Parent(s): b0b3efc

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +93 -0
app.py ADDED
@@ -0,0 +1,93 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from transformers import pipeline
3
+
4
+ # Load the sentiment analysis pipeline
5
+ # We use a model specifically trained on product reviews (Amazon reviews)
6
+ model_name = "LiYuan/amazon-review-sentiment-analysis"
7
+ sentiment_pipeline = pipeline("sentiment-analysis", model=model_name)
8
+
9
+ def analyze_sentiment(review_text):
10
+ """
11
+ Analyzes the sentiment of the input text and returns a formatted result.
12
+ The model outputs star ratings (1-5 stars).
13
+ """
14
+ if not review_text.strip():
15
+ return "Please enter some text to analyze.", None
16
+
17
+ try:
18
+ # Perform sentiment analysis
19
+ results = sentiment_pipeline(review_text)
20
+
21
+ # The model returns labels like '1 star', '2 stars', etc.
22
+ label = results[0]['label']
23
+ score = results[0]['score']
24
+
25
+ # Map star ratings to sentiment categories
26
+ star_count = int(label.split()[0])
27
+
28
+ if star_count >= 4:
29
+ sentiment = "Positive"
30
+ color = "🟢"
31
+ elif star_count == 3:
32
+ sentiment = "Neutral"
33
+ color = "🟡"
34
+ else:
35
+ sentiment = "Negative"
36
+ color = "🔴"
37
+
38
+ result_text = f"### Sentiment: {sentiment} {color}\n"
39
+ result_text += f"**Rating:** {label} ({score:.2%} confidence)\n\n"
40
+
41
+ # Add some context for computer system products
42
+ if "battery" in review_text.lower():
43
+ result_text += "- *Note: This review mentions battery life.*\n"
44
+ if "performance" in review_text.lower() or "fast" in review_text.lower() or "slow" in review_text.lower():
45
+ result_text += "- *Note: This review mentions system performance.*\n"
46
+ if "screen" in review_text.lower() or "display" in review_text.lower():
47
+ result_text += "- *Note: This review mentions the display/screen.*\n"
48
+
49
+ return result_text, {label: score}
50
+
51
+ except Exception as e:
52
+ return f"Error during analysis: {str(e)}", None
53
+
54
+ # Define the Gradio interface
55
+ with gr.Blocks(theme=gr.themes.Soft()) as demo:
56
+ gr.Markdown("# 💻 Computer System Sentiment Analyzer")
57
+ gr.Markdown(
58
+ "Enter a review for a computer, laptop, or hardware component to analyze its sentiment. "
59
+ "This tool uses a model trained on millions of product reviews to provide accurate star ratings."
60
+ )
61
+
62
+ with gr.Row():
63
+ with gr.Column():
64
+ input_text = gr.Textbox(
65
+ label="Product Review",
66
+ placeholder="e.g., The MacBook Pro has amazing performance and a stunning display, but the price is a bit high...",
67
+ lines=5
68
+ )
69
+ submit_btn = gr.Button("Analyze Sentiment", variant="primary")
70
+
71
+ with gr.Column():
72
+ output_markdown = gr.Markdown(label="Analysis Result")
73
+ output_label = gr.Label(label="Confidence Score")
74
+
75
+ # Examples for users to try
76
+ gr.Examples(
77
+ examples=[
78
+ ["The laptop is incredibly fast and the battery lasts all day. Highly recommended!"],
79
+ ["The screen arrived with dead pixels and the customer service was unhelpful. Disappointed."],
80
+ ["It's a decent computer for the price. Not the fastest, but gets the job done for basic tasks."],
81
+ ["The cooling system is quite loud under load, but the gaming performance is top-notch."]
82
+ ],
83
+ inputs=input_text
84
+ )
85
+
86
+ submit_btn.click(
87
+ fn=analyze_sentiment,
88
+ inputs=input_text,
89
+ outputs=[output_markdown, output_label]
90
+ )
91
+
92
+ if __name__ == "__main__":
93
+ demo.launch(server_name="0.0.0.0",show_error=True)