Spaces:
Runtime error
Runtime error
ShadowDominator
commited on
Commit
•
8745d6a
1
Parent(s):
6c0be41
Upload 2 files
Browse files- app.py +40 -0
- requirements.txt +3 -0
app.py
ADDED
@@ -0,0 +1,40 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import torch
|
3 |
+
from transformers import AutoTokenizer, AutoModelForSequenceClassification
|
4 |
+
|
5 |
+
tokenizer_sentence_analysis = AutoTokenizer.from_pretrained("finiteautomata/bertweet-base-sentiment-analysis")
|
6 |
+
model_sentence_analysis = AutoModelForSequenceClassification.from_pretrained("finiteautomata/bertweet-base-sentiment-analysis")
|
7 |
+
paragraph = """
|
8 |
+
I woke up this morning feeling refreshed and excited for the day ahead.
|
9 |
+
I had a great night's sleep, and I was looking forward to spending time with my family and friends.
|
10 |
+
I went for a walk in the park, and I enjoyed the beautiful weather. I also stopped by my favorite coffee shop and got a delicious cup of coffee.
|
11 |
+
I felt so happy and content, and I knew that it was going to be a great day.
|
12 |
+
|
13 |
+
"""
|
14 |
+
def sentence_sentiment_model(text, tokenizer, model):
|
15 |
+
inputs = tokenizer(text, padding=True, truncation=True, return_tensors="pt")
|
16 |
+
with torch.no_grad():
|
17 |
+
result = model(inputs['input_ids'], attention_mask=inputs['attention_mask'])
|
18 |
+
logits = result.logits.detach()
|
19 |
+
probs = torch.softmax(logits, dim=1)
|
20 |
+
pos_prob = probs[0][2].item()
|
21 |
+
neu_prob = probs[0][1].item()
|
22 |
+
neg_prob = probs[0][0].item()
|
23 |
+
return {'Positive': [round(float(pos_prob), 2)],"Neutural":[round(float(neu_prob), 2)], 'Negative': [round(float(neg_prob), 2)]}
|
24 |
+
|
25 |
+
def sentence_sentiment(text):
|
26 |
+
result = sentence_sentiment_model(text,tokenizer_sentence_analysis,model_sentence_analysis)
|
27 |
+
return result
|
28 |
+
|
29 |
+
with gr.Blocks(title="Sentence",css="footer {visibility: hidden}") as demo:
|
30 |
+
with gr.Row():
|
31 |
+
with gr.Column():
|
32 |
+
gr.Markdown("## Sentence sentiment")
|
33 |
+
with gr.Row():
|
34 |
+
with gr.Column():
|
35 |
+
inputs = gr.TextArea(label="sentence",value=paragraph,interactive=True)
|
36 |
+
btn = gr.Button(value="RUN")
|
37 |
+
with gr.Column():
|
38 |
+
output = gr.Label(label="output")
|
39 |
+
btn.click(fn=sentence_sentiment,inputs=[inputs],outputs=[output])
|
40 |
+
demo.launch()
|
requirements.txt
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
gradio==3.32.0
|
2 |
+
torch==2.0.0
|
3 |
+
transformers==4.28.1
|