Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,12 +2,10 @@ import gradio as gr
|
|
| 2 |
from transformers import pipeline
|
| 3 |
import time
|
| 4 |
|
| 5 |
-
# Load model once at startup
|
| 6 |
print("Loading sentiment model...")
|
| 7 |
classifier = pipeline("sentiment-analysis", model="distilbert-base-uncased-finetuned-sst-2-english")
|
| 8 |
print("Model ready!")
|
| 9 |
|
| 10 |
-
# ── Single analysis ──────────────────────────────────────────
|
| 11 |
def analyze_single(text):
|
| 12 |
if not text or not text.strip():
|
| 13 |
return "⚠️ Please enter some text.", "", ""
|
|
@@ -30,8 +28,6 @@ def analyze_single(text):
|
|
| 30 |
|
| 31 |
return sentiment_out, confidence_out, time_out
|
| 32 |
|
| 33 |
-
|
| 34 |
-
# ── Batch analysis ───────────────────────────────────────────
|
| 35 |
def analyze_batch(texts_input):
|
| 36 |
if not texts_input or not texts_input.strip():
|
| 37 |
return "⚠️ Please enter at least one sentence."
|
|
@@ -55,26 +51,23 @@ def analyze_batch(texts_input):
|
|
| 55 |
summary_neg = len(results) - summary_pos
|
| 56 |
|
| 57 |
output_lines.append("")
|
| 58 |
-
output_lines.append(f"
|
| 59 |
|
| 60 |
return "\n".join(output_lines)
|
| 61 |
|
| 62 |
-
|
| 63 |
-
# ── UI ───────────────────────────────────────────────────────
|
| 64 |
with gr.Blocks(
|
| 65 |
theme=gr.themes.Soft(primary_hue="blue", secondary_hue="indigo"),
|
| 66 |
title="Sentiment Analyzer"
|
| 67 |
) as demo:
|
| 68 |
|
| 69 |
gr.Markdown("""
|
| 70 |
-
#
|
| 71 |
Detects **Positive** or **Negative** sentiment using DistilBERT.
|
| 72 |
Built with HuggingFace Transformers · Model accuracy ~91% on SST-2 benchmark.
|
| 73 |
""")
|
| 74 |
|
| 75 |
with gr.Tabs():
|
| 76 |
|
| 77 |
-
# ── Tab 1: Single ──
|
| 78 |
with gr.TabItem("Single Analysis"):
|
| 79 |
with gr.Row():
|
| 80 |
with gr.Column(scale=2):
|
|
@@ -84,7 +77,7 @@ with gr.Blocks(
|
|
| 84 |
label="Input Text",
|
| 85 |
max_lines=6
|
| 86 |
)
|
| 87 |
-
analyze_btn = gr.Button("Analyze Sentiment
|
| 88 |
|
| 89 |
with gr.Column(scale=1):
|
| 90 |
sentiment_out = gr.Textbox(label="Sentiment", interactive=False)
|
|
@@ -109,7 +102,6 @@ with gr.Blocks(
|
|
| 109 |
outputs=[sentiment_out, confidence_out, time_out]
|
| 110 |
)
|
| 111 |
|
| 112 |
-
# ── Tab 2: Batch ──
|
| 113 |
with gr.TabItem("Batch Analysis"):
|
| 114 |
gr.Markdown("Enter **one sentence per line** (max 20 sentences)")
|
| 115 |
|
|
|
|
| 2 |
from transformers import pipeline
|
| 3 |
import time
|
| 4 |
|
|
|
|
| 5 |
print("Loading sentiment model...")
|
| 6 |
classifier = pipeline("sentiment-analysis", model="distilbert-base-uncased-finetuned-sst-2-english")
|
| 7 |
print("Model ready!")
|
| 8 |
|
|
|
|
| 9 |
def analyze_single(text):
|
| 10 |
if not text or not text.strip():
|
| 11 |
return "⚠️ Please enter some text.", "", ""
|
|
|
|
| 28 |
|
| 29 |
return sentiment_out, confidence_out, time_out
|
| 30 |
|
|
|
|
|
|
|
| 31 |
def analyze_batch(texts_input):
|
| 32 |
if not texts_input or not texts_input.strip():
|
| 33 |
return "⚠️ Please enter at least one sentence."
|
|
|
|
| 51 |
summary_neg = len(results) - summary_pos
|
| 52 |
|
| 53 |
output_lines.append("")
|
| 54 |
+
output_lines.append(f" Summary: {summary_pos} Positive | {summary_neg} Negative | {len(results)} Total")
|
| 55 |
|
| 56 |
return "\n".join(output_lines)
|
| 57 |
|
|
|
|
|
|
|
| 58 |
with gr.Blocks(
|
| 59 |
theme=gr.themes.Soft(primary_hue="blue", secondary_hue="indigo"),
|
| 60 |
title="Sentiment Analyzer"
|
| 61 |
) as demo:
|
| 62 |
|
| 63 |
gr.Markdown("""
|
| 64 |
+
# Sentiment Analyzer
|
| 65 |
Detects **Positive** or **Negative** sentiment using DistilBERT.
|
| 66 |
Built with HuggingFace Transformers · Model accuracy ~91% on SST-2 benchmark.
|
| 67 |
""")
|
| 68 |
|
| 69 |
with gr.Tabs():
|
| 70 |
|
|
|
|
| 71 |
with gr.TabItem("Single Analysis"):
|
| 72 |
with gr.Row():
|
| 73 |
with gr.Column(scale=2):
|
|
|
|
| 77 |
label="Input Text",
|
| 78 |
max_lines=6
|
| 79 |
)
|
| 80 |
+
analyze_btn = gr.Button("Analyze Sentiment ", variant="primary", size="lg")
|
| 81 |
|
| 82 |
with gr.Column(scale=1):
|
| 83 |
sentiment_out = gr.Textbox(label="Sentiment", interactive=False)
|
|
|
|
| 102 |
outputs=[sentiment_out, confidence_out, time_out]
|
| 103 |
)
|
| 104 |
|
|
|
|
| 105 |
with gr.TabItem("Batch Analysis"):
|
| 106 |
gr.Markdown("Enter **one sentence per line** (max 20 sentences)")
|
| 107 |
|