ghostai1 commited on
Commit
386a1d7
·
verified ·
1 Parent(s): 7fdc931

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +64 -0
app.py ADDED
@@ -0,0 +1,64 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from transformers import pipeline
3
+ import matplotlib.pyplot as plt
4
+ from collections import Counter
5
+ import threading
6
+
7
+ # Initialize sentiment pipeline
8
+ sentiment_analyzer = pipeline(
9
+ "sentiment-analysis",
10
+ model="distilbert-base-uncased-finetuned-sst-2-english"
11
+ )
12
+
13
+ # Thread-safe log storage
14
+ log_lock = threading.Lock()
15
+ sentiment_log = []
16
+
17
+ def analyze_and_log(text):
18
+ result = sentiment_analyzer(text)[0]
19
+ label = result['label']
20
+ score = round(result['score'], 3)
21
+ entry = f"Input: {text} --> Sentiment: {label} (Confidence: {score})"
22
+
23
+ with log_lock:
24
+ sentiment_log.append((text, label))
25
+
26
+ return label, score, entry, update_chart()
27
+
28
+ def update_chart():
29
+ with log_lock:
30
+ labels = [label for _, label in sentiment_log]
31
+ counts = Counter(labels)
32
+
33
+ fig, ax = plt.subplots(figsize=(4, 3))
34
+ ax.bar(counts.keys(), counts.values(), color=['#4CAF50', '#F44336'])
35
+ ax.set_title("Sentiment Distribution")
36
+ ax.set_xlabel("Sentiment")
37
+ ax.set_ylabel("Count")
38
+ plt.tight_layout()
39
+
40
+ return fig
41
+
42
+ with gr.Blocks() as demo:
43
+ gr.Markdown("# DistilBERT Sentiment Analysis with Live Logs & Visualization")
44
+ gr.Markdown("Enter your salon feedback or product review below and get instant sentiment analysis, logging, and sentiment summary visualization.")
45
+
46
+ with gr.Row():
47
+ with gr.Column(scale=3):
48
+ input_text = gr.Textbox(lines=3, placeholder="Type your text here...")
49
+ analyze_btn = gr.Button("Analyze Sentiment")
50
+ sentiment_label = gr.Textbox(label="Sentiment Label", interactive=False)
51
+ confidence_score = gr.Textbox(label="Confidence Score", interactive=False)
52
+ log_output = gr.Textbox(label="Analysis Log", interactive=False, lines=10)
53
+
54
+ with gr.Column(scale=2):
55
+ sentiment_chart = gr.Plot(label="Sentiment Distribution Chart")
56
+
57
+ analyze_btn.click(
58
+ analyze_and_log,
59
+ inputs=input_text,
60
+ outputs=[sentiment_label, confidence_score, log_output, sentiment_chart]
61
+ )
62
+
63
+ if __name__ == "__main__":
64
+ demo.launch()