giftjohnpaul27 commited on
Commit
53dd6c0
·
verified ·
1 Parent(s): 50d879e

Upload index.html

Browse files
Files changed (1) hide show
  1. index.html +75 -0
index.html ADDED
@@ -0,0 +1,75 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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">
6
+ <title>Sentiment AI Analyzer</title>
7
+ <style>
8
+ :root{color-scheme:light dark;--bg:#101827;--card:#1a2638;--text:#e9eef7;--muted:#9aa9bc;--accent:#6c9cff;--border:#34445b}
9
+ *{box-sizing:border-box}body{margin:0;background:var(--bg);color:var(--text);font-family:system-ui,-apple-system,Segoe UI,sans-serif;padding:24px}
10
+ main{max-width:950px;margin:auto}h1{margin:0 0 8px}p{color:var(--muted)}.card{background:var(--card);border:1px solid var(--border);border-radius:16px;padding:20px;margin:18px 0}
11
+ textarea{width:100%;min-height:140px;padding:14px;border-radius:12px;border:1px solid var(--border);background:var(--bg);color:var(--text);font:inherit}
12
+ button{padding:10px 14px;border-radius:10px;border:1px solid var(--border);background:#263852;color:var(--text);cursor:pointer;margin:5px 5px 5px 0}button.primary{background:var(--accent);color:#081222;border:0;font-weight:700}
13
+ .row{display:flex;gap:14px;flex-wrap:wrap}.row>*{flex:1;min-width:180px}.score{margin:12px 0}.bar{height:12px;border-radius:8px;background:#34445b;overflow:hidden}.fill{height:100%;background:var(--accent);width:0}.muted{color:var(--muted);font-size:.9rem}.history-item{border:1px solid var(--border);padding:12px;border-radius:12px;margin:10px 0;overflow-wrap:anywhere}.hidden{display:none}.status{padding:10px;border-radius:8px;background:#263852;margin:10px 0}
14
+ </style>
15
+ </head>
16
+ <body>
17
+ <main>
18
+ <h1>💬 Sentiment AI Analyzer</h1>
19
+ <p>Analyze text sentiment with a Hugging Face model directly in your browser.</p>
20
+ <div class="card">
21
+ <label for="textInput"><strong>Enter text to analyze</strong></label>
22
+ <textarea id="textInput" placeholder="Type or paste your text here..."></textarea>
23
+ <p class="muted"><span id="wordCount">0</span> words · <span id="charCount">0</span> characters</p>
24
+ <div>
25
+ <button class="primary" id="analyzeBtn">🔍 Analyze Sentiment</button><button id="clearBtn">Clear text</button>
26
+ <button class="example" data-text="I absolutely love this product. It works wonderfully!">Positive example</button>
27
+ <button class="example" data-text="This was a terrible experience and I am very disappointed.">Negative example</button>
28
+ <button class="example" data-text="The service was okay, nothing special.">Neutral-ish example</button>
29
+ </div><div id="status" class="status">Loading sentiment model…</div>
30
+ </div>
31
+ <div id="results" class="card hidden">
32
+ <h2>Analysis Result</h2><h3 id="resultLabel"></h3>
33
+ <div class="score"><strong>Positive: <span id="positiveText"></span></strong><div class="bar"><div class="fill" id="positiveBar"></div></div></div>
34
+ <div class="score"><strong>Neutral*: <span id="neutralText"></span></strong><div class="bar"><div class="fill" id="neutralBar"></div></div></div>
35
+ <div class="score"><strong>Negative: <span id="negativeText"></span></strong><div class="bar"><div class="fill" id="negativeBar"></div></div></div>
36
+ <p class="muted">*Neutral is a heuristic estimate based on the binary model's confidence, not a genuine third model class.</p>
37
+ </div>
38
+ <div class="card">
39
+ <div class="row" style="align-items:center"><div><h2>📜 History</h2><p class="muted">Saved in this browser · up to 100 entries</p></div><div><button id="exportCsvBtn">Export CSV</button><button id="exportJsonBtn">Export JSON</button><button id="historyClearBtn">Clear history</button></div></div>
40
+ <div id="historyList"></div>
41
+ </div>
42
+ </main>
43
+ <script type="module">
44
+ import { pipeline } from 'https://cdn.jsdelivr.net/npm/@huggingface/transformers@3.7.2';
45
+
46
+ const textInput=document.getElementById('textInput');
47
+ let history=JSON.parse(localStorage.getItem('sentimentHistory')||'[]');
48
+ let classifier=null;
49
+
50
+ function updateCounter(){const t=textInput.value;document.getElementById('wordCount').textContent=t.trim()?t.trim().split(/\s+/).length:0;document.getElementById('charCount').textContent=t.length;}
51
+ function setStatus(msg){document.getElementById('status').textContent=msg;}
52
+ async function loadModel(){try{setStatus('Loading model (first run may take a while)…');classifier=await pipeline('sentiment-analysis','Xenova/distilbert-base-uncased-finetuned-sst-2-english');setStatus('Model ready. Enter text and click Analyze.');}catch(e){setStatus('Model loading failed: '+e.message);}}
53
+ function renderHistory(){const el=document.getElementById('historyList');if(!history.length){el.innerHTML='<p class="muted">No analyses yet.</p>';return;}el.innerHTML=history.map((item,i)=>`<div class="history-item"><strong>${escapeHtml(item.label)}</strong> · ${escapeHtml(item.time)}<p>${escapeHtml(item.text)}</p><small>Positive ${pct(item.positive)} · Neutral ${pct(item.neutral)} · Negative ${pct(item.negative)}</small></div>`).join('');}
54
+ function escapeHtml(s){return String(s??'').replace(/[&<>"']/g,c=>({'&':'&amp;','<':'&lt;','>':'&gt;','"':'&quot;',"'":'&#39;'}[c]));}
55
+ function pct(v){return (Number(v||0)*100).toFixed(1)+'%';}
56
+ function showResults(text,label,scores,confidence){document.getElementById('results').classList.remove('hidden');document.getElementById('resultLabel').textContent=label+' ('+pct(confidence)+' model confidence)';for(const k of ['positive','neutral','negative']){document.getElementById(k+'Text').textContent=pct(scores[k]);document.getElementById(k+'Bar').style.width=(scores[k]*100)+'%';}
57
+ history.unshift({text,label,positive:scores.positive,neutral:scores.neutral,negative:scores.negative,confidence,time:new Date().toLocaleString()});
58
+ history=history.slice(0,100);
59
+ localStorage.setItem('sentimentHistory',JSON.stringify(history));
60
+ renderHistory();}
61
+ async function analyze(){const text=textInput.value.trim();if(!text){setStatus('Please enter some text first.');return;}if(!classifier){setStatus('Model is not ready yet.');return;}setStatus('Analyzing…');try{const out=await classifier(text.slice(0,5000));const raw=out[0];const isPositive=raw.label.toUpperCase().includes('POS');const confidence=raw.score;const scores={positive:isPositive?confidence:0,negative:isPositive?0:confidence,neutral:Math.max(0,1-confidence)};if(scores.neutral<.15)scores.neutral=0;const label=isPositive?'Positive':'Negative';showResults(text,label,scores,confidence);setStatus('Analysis complete.');}catch(e){setStatus('Analysis error: '+e.message);}}
62
+ textInput.addEventListener('input',updateCounter);
63
+ document.getElementById('analyzeBtn').addEventListener('click',analyze);
64
+ document.getElementById('clearBtn').addEventListener('click',()=>{textInput.value='';updateCounter();document.getElementById('results').classList.add('hidden');setStatus(classifier?'Model ready. Enter text and click Analyze.':'Loading model…');});
65
+ document.querySelectorAll('.example').forEach(btn=>btn.addEventListener('click',()=>{textInput.value=btn.dataset.text;updateCounter();}));
66
+ document.getElementById('historyClearBtn').addEventListener('click',()=>{history=[];localStorage.removeItem('sentimentHistory');renderHistory();});
67
+ function download(filename,content,type){const blob=new Blob([content],{type}),url=URL.createObjectURL(blob),a=document.createElement('a');a.href=url;a.download=filename;a.click();URL.revokeObjectURL(url);}
68
+ document.getElementById('exportJsonBtn').addEventListener('click',()=>download('sentiment_history.json',JSON.stringify(history,null,2),'application/json'));
69
+ document.getElementById('exportCsvBtn').addEventListener('click',()=>{const rows=[['time','label','text','positive','neutral','negative','confidence'],...history.map(x=>[x.time,x.label,x.text,x.positive,x.neutral,x.negative,x.confidence])];const csv=rows.map(r=>r.map(v=>'"'+String(v??'').replace(/"/g,'""')+'"').join(',')).join('\n');download('sentiment_history.csv',csv,'text/csv');});
70
+ renderHistory();
71
+ loadModel();
72
+ updateCounter();
73
+ </script>
74
+ </body>
75
+ </html>