Spaces:
Running
Running
Initial sentiment analyzer demo
Browse files- README.md +16 -6
- app.py +75 -0
- index.html +347 -53
- requirements.txt +3 -0
README.md
CHANGED
|
@@ -1,10 +1,20 @@
|
|
| 1 |
---
|
| 2 |
-
title: Sentiment
|
| 3 |
-
emoji:
|
| 4 |
-
colorFrom:
|
| 5 |
-
colorTo:
|
| 6 |
-
sdk:
|
|
|
|
|
|
|
| 7 |
pinned: false
|
|
|
|
| 8 |
---
|
| 9 |
|
| 10 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
---
|
| 2 |
+
title: Simple Sentiment Analyzer
|
| 3 |
+
emoji: 🎭
|
| 4 |
+
colorFrom: blue
|
| 5 |
+
colorTo: indigo
|
| 6 |
+
sdk: gradio
|
| 7 |
+
sdk_version: 4.44.0
|
| 8 |
+
app_file: app.py
|
| 9 |
pinned: false
|
| 10 |
+
license: mit
|
| 11 |
---
|
| 12 |
|
| 13 |
+
# Simple Sentiment Analyzer
|
| 14 |
+
|
| 15 |
+
Analyze sentiment of any English sentence. Output percentages for:
|
| 16 |
+
- Positive
|
| 17 |
+
- Negative
|
| 18 |
+
- Neutral
|
| 19 |
+
|
| 20 |
+
Built with Gradio and Hugging Face Transformers.
|
app.py
ADDED
|
@@ -0,0 +1,75 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from transformers import pipeline
|
| 3 |
+
|
| 4 |
+
# Load sentiment analysis pipeline
|
| 5 |
+
classifier = pipeline(
|
| 6 |
+
"sentiment-analysis",
|
| 7 |
+
model="distilbert-base-uncased-finetuned-sst-2-english",
|
| 8 |
+
return_all_scores=True
|
| 9 |
+
)
|
| 10 |
+
|
| 11 |
+
def analyze_sentiment(text):
|
| 12 |
+
"""Analyze sentiment and return percentages."""
|
| 13 |
+
if not text.strip():
|
| 14 |
+
return {"Positive": 0, "Negative": 0, "Neutral": 0}, ""
|
| 15 |
+
|
| 16 |
+
results = classifier(text)[0]
|
| 17 |
+
|
| 18 |
+
# Convert to percentage format
|
| 19 |
+
sentiment_map = {}
|
| 20 |
+
for r in results:
|
| 21 |
+
label = r['label']
|
| 22 |
+
score = r['score'] * 100
|
| 23 |
+
if label == 'POSITIVE':
|
| 24 |
+
sentiment_map['Positive'] = round(score, 2)
|
| 25 |
+
elif label == 'NEGATIVE':
|
| 26 |
+
sentiment_map['Negative'] = round(score, 2)
|
| 27 |
+
|
| 28 |
+
# Add neutral if needed (for models that only have pos/neg)
|
| 29 |
+
if 'Neutral' not in sentiment_map:
|
| 30 |
+
sentiment_map['Neutral'] = round(100 - sentiment_map.get('Positive', 0) - sentiment_map.get('Negative', 0), 2)
|
| 31 |
+
|
| 32 |
+
# Create summary text
|
| 33 |
+
dominant = max(sentiment_map, key=sentiment_map.get)
|
| 34 |
+
summary = f"**Dominant: {dominant} ({sentiment_map[dominant]}%)**"
|
| 35 |
+
|
| 36 |
+
return sentiment_map, summary
|
| 37 |
+
|
| 38 |
+
# Create Gradio interface
|
| 39 |
+
with gr.Blocks(title="Simple Sentiment Analyzer") as demo:
|
| 40 |
+
gr.Markdown("# 🎭 Simple Sentiment Analyzer")
|
| 41 |
+
gr.Markdown("Enter a sentence to see sentiment breakdown with percentages.")
|
| 42 |
+
|
| 43 |
+
with gr.Row():
|
| 44 |
+
with gr.Column():
|
| 45 |
+
text_input = gr.Textbox(
|
| 46 |
+
label="Input Text",
|
| 47 |
+
placeholder="Type your sentence here...",
|
| 48 |
+
lines=4
|
| 49 |
+
)
|
| 50 |
+
analyze_btn = gr.Button("Analyze", variant="primary")
|
| 51 |
+
|
| 52 |
+
with gr.Column():
|
| 53 |
+
output_json = gr.Label(label="Sentiment Scores (%)", num_top_classes=3)
|
| 54 |
+
summary_md = gr.Markdown()
|
| 55 |
+
|
| 56 |
+
analyze_btn.click(
|
| 57 |
+
fn=analyze_sentiment,
|
| 58 |
+
inputs=text_input,
|
| 59 |
+
outputs=[output_json, summary_md]
|
| 60 |
+
)
|
| 61 |
+
|
| 62 |
+
# Example inputs
|
| 63 |
+
gr.Examples(
|
| 64 |
+
examples=[
|
| 65 |
+
"I absolutely love this product! It's amazing!",
|
| 66 |
+
"This is terrible. I hate it.",
|
| 67 |
+
"The weather is okay today.",
|
| 68 |
+
"The movie was okay, nothing special but not bad either.",
|
| 69 |
+
"Best day ever! Everything went perfectly!"
|
| 70 |
+
],
|
| 71 |
+
inputs=text_input
|
| 72 |
+
)
|
| 73 |
+
|
| 74 |
+
if __name__ == "__main__":
|
| 75 |
+
demo.launch()
|
index.html
CHANGED
|
@@ -1,57 +1,351 @@
|
|
| 1 |
<!DOCTYPE html>
|
| 2 |
-
<html>
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
| 57 |
</html>
|
|
|
|
| 1 |
<!DOCTYPE html>
|
| 2 |
+
<html lang="en">
|
| 3 |
+
<head>
|
| 4 |
+
<meta charset="UTF-8">
|
| 5 |
+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
| 6 |
+
<title>Simple Sentiment Analyzer</title>
|
| 7 |
+
<script src="https://cdn.jsdelivr.net/npm/@huggingface/transformers@3.0.0/dist/transformers.min.js"></script>
|
| 8 |
+
<style>
|
| 9 |
+
* { box-sizing: border-box; }
|
| 10 |
+
body {
|
| 11 |
+
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
|
| 12 |
+
max-width: 720px;
|
| 13 |
+
margin: 0 auto;
|
| 14 |
+
padding: 2rem 1rem;
|
| 15 |
+
background: #f8fafc;
|
| 16 |
+
color: #0f172a;
|
| 17 |
+
line-height: 1.6;
|
| 18 |
+
}
|
| 19 |
+
.container {
|
| 20 |
+
background: white;
|
| 21 |
+
border-radius: 16px;
|
| 22 |
+
padding: 2.5rem;
|
| 23 |
+
box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1);
|
| 24 |
+
}
|
| 25 |
+
h1 {
|
| 26 |
+
font-size: 2rem;
|
| 27 |
+
font-weight: 700;
|
| 28 |
+
margin: 0 0 0.5rem;
|
| 29 |
+
text-align: center;
|
| 30 |
+
color: #1e293b;
|
| 31 |
+
}
|
| 32 |
+
.subtitle {
|
| 33 |
+
text-align: center;
|
| 34 |
+
color: #64748b;
|
| 35 |
+
margin-bottom: 2rem;
|
| 36 |
+
}
|
| 37 |
+
.input-group {
|
| 38 |
+
margin-bottom: 1.5rem;
|
| 39 |
+
}
|
| 40 |
+
label {
|
| 41 |
+
display: block;
|
| 42 |
+
font-weight: 500;
|
| 43 |
+
margin-bottom: 0.5rem;
|
| 44 |
+
color: #334155;
|
| 45 |
+
}
|
| 46 |
+
textarea {
|
| 47 |
+
width: 100%;
|
| 48 |
+
min-height: 120px;
|
| 49 |
+
padding: 1rem;
|
| 50 |
+
border: 2px solid #e2e8f0;
|
| 51 |
+
border-radius: 10px;
|
| 52 |
+
font-size: 1rem;
|
| 53 |
+
font-family: inherit;
|
| 54 |
+
resize: vertical;
|
| 55 |
+
transition: border-color 0.2s;
|
| 56 |
+
}
|
| 57 |
+
textarea:focus {
|
| 58 |
+
outline: none;
|
| 59 |
+
border-color: #3b82f6;
|
| 60 |
+
}
|
| 61 |
+
.btn {
|
| 62 |
+
width: 100%;
|
| 63 |
+
padding: 1rem;
|
| 64 |
+
background: linear-gradient(135deg, #3b82f6, #6366f1);
|
| 65 |
+
color: white;
|
| 66 |
+
border: none;
|
| 67 |
+
border-radius: 10px;
|
| 68 |
+
font-size: 1.1rem;
|
| 69 |
+
font-weight: 600;
|
| 70 |
+
cursor: pointer;
|
| 71 |
+
transition: transform 0.1s, box-shadow 0.2s;
|
| 72 |
+
}
|
| 73 |
+
.btn:hover:not(:disabled) {
|
| 74 |
+
transform: translateY(-1px);
|
| 75 |
+
box-shadow: 0 4px 12px rgba(59, 130, 246, 0.4);
|
| 76 |
+
}
|
| 77 |
+
.btn:active:not(:disabled) {
|
| 78 |
+
transform: translateY(0);
|
| 79 |
+
}
|
| 80 |
+
.btn:disabled {
|
| 81 |
+
opacity: 0.6;
|
| 82 |
+
cursor: not-allowed;
|
| 83 |
+
}
|
| 84 |
+
.results {
|
| 85 |
+
margin-top: 2rem;
|
| 86 |
+
padding-top: 1.5rem;
|
| 87 |
+
border-top: 1px solid #e2e8f0;
|
| 88 |
+
display: none;
|
| 89 |
+
}
|
| 90 |
+
.results.visible {
|
| 91 |
+
display: block;
|
| 92 |
+
animation: fadeIn 0.3s ease;
|
| 93 |
+
}
|
| 94 |
+
@keyframes fadeIn {
|
| 95 |
+
from { opacity: 0; transform: translateY(10px); }
|
| 96 |
+
to { opacity: 1; transform: translateY(0); }
|
| 97 |
+
}
|
| 98 |
+
.sentiment-bar {
|
| 99 |
+
margin-bottom: 1rem;
|
| 100 |
+
}
|
| 101 |
+
.bar-header {
|
| 102 |
+
display: flex;
|
| 103 |
+
justify-content: space-between;
|
| 104 |
+
margin-bottom: 0.4rem;
|
| 105 |
+
font-size: 0.95rem;
|
| 106 |
+
}
|
| 107 |
+
.bar-label {
|
| 108 |
+
font-weight: 600;
|
| 109 |
+
text-transform: capitalize;
|
| 110 |
+
}
|
| 111 |
+
.bar-value {
|
| 112 |
+
font-weight: 700;
|
| 113 |
+
color: #3b82f6;
|
| 114 |
+
}
|
| 115 |
+
.bar-track {
|
| 116 |
+
height: 14px;
|
| 117 |
+
background: #e2e8f0;
|
| 118 |
+
border-radius: 7px;
|
| 119 |
+
overflow: hidden;
|
| 120 |
+
}
|
| 121 |
+
.bar-fill {
|
| 122 |
+
height: 100%;
|
| 123 |
+
border-radius: 7px;
|
| 124 |
+
transition: width 0.6s cubic-bezier(0.4, 0, 0.2, 1);
|
| 125 |
+
}
|
| 126 |
+
.bar-fill.positive { background: linear-gradient(90deg, #22c55e, #16a34a); }
|
| 127 |
+
.bar-fill.negative { background: linear-gradient(90deg, #ef4444, #dc2626); }
|
| 128 |
+
.bar-fill.neutral { background: linear-gradient(90deg, #f59e0b, #d97706); }
|
| 129 |
+
.summary {
|
| 130 |
+
text-align: center;
|
| 131 |
+
margin-top: 1.5rem;
|
| 132 |
+
padding: 1rem;
|
| 133 |
+
background: #f1f5f9;
|
| 134 |
+
border-radius: 10px;
|
| 135 |
+
font-size: 1.1rem;
|
| 136 |
+
font-weight: 600;
|
| 137 |
+
}
|
| 138 |
+
.examples {
|
| 139 |
+
margin-top: 2rem;
|
| 140 |
+
padding-top: 1.5rem;
|
| 141 |
+
border-top: 1px solid #e2e8f0;
|
| 142 |
+
}
|
| 143 |
+
.example-btn {
|
| 144 |
+
display: inline-block;
|
| 145 |
+
margin: 0.3rem;
|
| 146 |
+
padding: 0.5rem 1rem;
|
| 147 |
+
background: #f1f5f9;
|
| 148 |
+
border: 1px solid #e2e8f0;
|
| 149 |
+
border-radius: 8px;
|
| 150 |
+
font-size: 0.85rem;
|
| 151 |
+
color: #475569;
|
| 152 |
+
cursor: pointer;
|
| 153 |
+
transition: all 0.2s;
|
| 154 |
+
}
|
| 155 |
+
.example-btn:hover {
|
| 156 |
+
background: #e2e8f0;
|
| 157 |
+
border-color: #3b82f6;
|
| 158 |
+
color: #3b82f6;
|
| 159 |
+
}
|
| 160 |
+
.loading {
|
| 161 |
+
display: inline-block;
|
| 162 |
+
width: 18px;
|
| 163 |
+
height: 18px;
|
| 164 |
+
border: 2px solid #ffffff50;
|
| 165 |
+
border-top-color: white;
|
| 166 |
+
border-radius: 50%;
|
| 167 |
+
animation: spin 0.8s linear infinite;
|
| 168 |
+
margin-right: 0.5rem;
|
| 169 |
+
vertical-align: middle;
|
| 170 |
+
}
|
| 171 |
+
@keyframes spin {
|
| 172 |
+
to { transform: rotate(360deg); }
|
| 173 |
+
}
|
| 174 |
+
.loading-text {
|
| 175 |
+
display: none;
|
| 176 |
+
}
|
| 177 |
+
.btn.loading .btn-text { display: none; }
|
| 178 |
+
.btn.loading .loading-text { display: inline; }
|
| 179 |
+
</style>
|
| 180 |
+
</head>
|
| 181 |
+
<body>
|
| 182 |
+
<div class="container">
|
| 183 |
+
<h1>🎭 Simple Sentiment Analyzer</h1>
|
| 184 |
+
<p class="subtitle">Enter any English sentence to see sentiment breakdown with percentages</p>
|
| 185 |
+
|
| 186 |
+
<div class="input-group">
|
| 187 |
+
<label for="textInput">Input Text</label>
|
| 188 |
+
<textarea id="textInput" placeholder="Type your sentence here..."></textarea>
|
| 189 |
+
</div>
|
| 190 |
+
|
| 191 |
+
<button class="btn" id="analyzeBtn">
|
| 192 |
+
<span class="btn-text">Analyze Sentiment</span>
|
| 193 |
+
<span class="loading-text"><span class="loading"></span> Analyzing...</span>
|
| 194 |
+
</button>
|
| 195 |
+
|
| 196 |
+
<div class="results" id="results">
|
| 197 |
+
<div class="sentiment-bar">
|
| 198 |
+
<div class="bar-header">
|
| 199 |
+
<span class="bar-label">Positive</span>
|
| 200 |
+
<span class="bar-value" id="positiveVal">0%</span>
|
| 201 |
+
</div>
|
| 202 |
+
<div class="bar-track">
|
| 203 |
+
<div class="bar-fill positive" id="positiveBar" style="width: 0%"></div>
|
| 204 |
+
</div>
|
| 205 |
+
</div>
|
| 206 |
+
<div class="sentiment-bar">
|
| 207 |
+
<div class="bar-header">
|
| 208 |
+
<span class="bar-label">Negative</span>
|
| 209 |
+
<span class="bar-value" id="negativeVal">0%</span>
|
| 210 |
+
</div>
|
| 211 |
+
<div class="bar-track">
|
| 212 |
+
<div class="bar-fill negative" id="negativeBar" style="width: 0%"></div>
|
| 213 |
+
</div>
|
| 214 |
+
</div>
|
| 215 |
+
<div class="sentiment-bar">
|
| 216 |
+
<div class="bar-header">
|
| 217 |
+
<span class="bar-label">Neutral</span>
|
| 218 |
+
<span class="bar-value" id="neutralVal">0%</span>
|
| 219 |
+
</div>
|
| 220 |
+
<div class="bar-track">
|
| 221 |
+
<div class="bar-fill neutral" id="neutralBar" style="width: 0%"></div>
|
| 222 |
+
</div>
|
| 223 |
+
</div>
|
| 224 |
+
<div class="summary" id="summary"></div>
|
| 225 |
+
</div>
|
| 226 |
+
|
| 227 |
+
<div class="examples">
|
| 228 |
+
<label>Try examples:</label>
|
| 229 |
+
<div>
|
| 230 |
+
<button class="example-btn" onclick="setExample('I absolutely love this product! It is amazing!')">Love it!</button>
|
| 231 |
+
<button class="example-btn" onclick="setExample('This is terrible. I hate it.')">Hate it</button>
|
| 232 |
+
<button class="example-btn" onclick="setExample('The weather is okay today.')">Neutral</button>
|
| 233 |
+
<button class="example-btn" onclick="setExample('The movie was okay, nothing special but not bad.')">Mixed</button>
|
| 234 |
+
<button class="example-btn" onclick="setExample('Best day ever! Everything went perfectly!')">Very Positive</button>
|
| 235 |
+
</div>
|
| 236 |
+
</div>
|
| 237 |
+
</div>
|
| 238 |
|
| 239 |
+
<script>
|
| 240 |
+
let classifier = null;
|
| 241 |
+
let isModelLoading = false;
|
| 242 |
+
|
| 243 |
+
async function loadModel() {
|
| 244 |
+
if (classifier || isModelLoading) return;
|
| 245 |
+
isModelLoading = true;
|
| 246 |
+
|
| 247 |
+
try {
|
| 248 |
+
classifier = await Transformers.pipeline(
|
| 249 |
+
'sentiment-analysis',
|
| 250 |
+
'Xenova/distilbert-base-uncased-finetuned-sst-2-english',
|
| 251 |
+
{ device: 'auto' }
|
| 252 |
+
);
|
| 253 |
+
} catch (e) {
|
| 254 |
+
console.error('Model load error:', e);
|
| 255 |
+
alert('Failed to load model. Please refresh and try again.');
|
| 256 |
+
} finally {
|
| 257 |
+
isModelLoading = false;
|
| 258 |
}
|
| 259 |
+
}
|
| 260 |
+
|
| 261 |
+
// Pre-load model on page load
|
| 262 |
+
loadModel();
|
| 263 |
+
|
| 264 |
+
function setExample(text) {
|
| 265 |
+
document.getElementById('textInput').value = text;
|
| 266 |
+
}
|
| 267 |
+
|
| 268 |
+
function updateBars(scores) {
|
| 269 |
+
const positive = scores.Positive || 0;
|
| 270 |
+
const negative = scores.Negative || 0;
|
| 271 |
+
const neutral = scores.Neutral || 0;
|
| 272 |
+
|
| 273 |
+
document.getElementById('positiveVal').textContent = positive.toFixed(1) + '%';
|
| 274 |
+
document.getElementById('negativeVal').textContent = negative.toFixed(1) + '%';
|
| 275 |
+
document.getElementById('neutralVal').textContent = neutral.toFixed(1) + '%';
|
| 276 |
+
|
| 277 |
+
document.getElementById('positiveBar').style.width = positive + '%';
|
| 278 |
+
document.getElementById('negativeBar').style.width = negative + '%';
|
| 279 |
+
document.getElementById('neutralBar').style.width = neutral + '%';
|
| 280 |
+
}
|
| 281 |
+
|
| 282 |
+
function getDominant(scores) {
|
| 283 |
+
let dominant = 'Neutral';
|
| 284 |
+
let maxScore = scores.Neutral || 0;
|
| 285 |
+
if ((scores.Positive || 0) > maxScore) {
|
| 286 |
+
dominant = 'Positive';
|
| 287 |
+
maxScore = scores.Positive;
|
| 288 |
+
}
|
| 289 |
+
if ((scores.Negative || 0) > maxScore) {
|
| 290 |
+
dominant = 'Negative';
|
| 291 |
+
maxScore = scores.Negative;
|
| 292 |
+
}
|
| 293 |
+
return { dominant, score: maxScore };
|
| 294 |
+
}
|
| 295 |
+
|
| 296 |
+
async function analyze() {
|
| 297 |
+
const text = document.getElementById('textInput').value.trim();
|
| 298 |
+
if (!text) return;
|
| 299 |
+
|
| 300 |
+
const btn = document.getElementById('analyzeBtn');
|
| 301 |
+
const results = document.getElementById('results');
|
| 302 |
+
|
| 303 |
+
btn.classList.add('loading');
|
| 304 |
+
btn.disabled = true;
|
| 305 |
+
|
| 306 |
+
// Ensure model is loaded
|
| 307 |
+
if (!classifier) {
|
| 308 |
+
await loadModel();
|
| 309 |
+
}
|
| 310 |
+
|
| 311 |
+
try {
|
| 312 |
+
const result = await classifier(text);
|
| 313 |
+
const scores = {};
|
| 314 |
+
|
| 315 |
+
for (const r of result) {
|
| 316 |
+
const label = r.label;
|
| 317 |
+
const score = (r.score * 100).toFixed(1);
|
| 318 |
+
if (label === 'POSITIVE') scores.Positive = parseFloat(score);
|
| 319 |
+
else if (label === 'NEGATIVE') scores.Negative = parseFloat(score);
|
| 320 |
+
}
|
| 321 |
+
|
| 322 |
+
// Calculate neutral
|
| 323 |
+
scores.Neutral = parseFloat((100 - (scores.Positive || 0) - (scores.Negative || 0)).toFixed(1));
|
| 324 |
+
if (scores.Neutral < 0) scores.Neutral = 0;
|
| 325 |
+
|
| 326 |
+
updateBars(scores);
|
| 327 |
+
|
| 328 |
+
const { dominant, score } = getDominant(scores);
|
| 329 |
+
document.getElementById('summary').innerHTML =
|
| 330 |
+
`Dominant: <span style="color: #3b82f6">${dominant}</span> (${score}%)`;
|
| 331 |
+
|
| 332 |
+
results.classList.add('visible');
|
| 333 |
+
} catch (e) {
|
| 334 |
+
console.error('Analysis error:', e);
|
| 335 |
+
alert('Analysis failed. Please try again.');
|
| 336 |
+
} finally {
|
| 337 |
+
btn.classList.remove('loading');
|
| 338 |
+
btn.disabled = false;
|
| 339 |
+
}
|
| 340 |
+
}
|
| 341 |
+
|
| 342 |
+
document.getElementById('analyzeBtn').addEventListener('click', analyze);
|
| 343 |
+
document.getElementById('textInput').addEventListener('keydown', (e) => {
|
| 344 |
+
if (e.key === 'Enter' && !e.shiftKey) {
|
| 345 |
+
e.preventDefault();
|
| 346 |
+
analyze();
|
| 347 |
+
}
|
| 348 |
+
});
|
| 349 |
+
</script>
|
| 350 |
+
</body>
|
| 351 |
</html>
|
requirements.txt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
gradio>=4.0
|
| 2 |
+
transformers>=4.30
|
| 3 |
+
torch>=2.0
|