Upload 2 files
Browse files- sentiement_analysis.ipynb +0 -0
- sentiement_analysis.py +69 -0
sentiement_analysis.ipynb
ADDED
The diff for this file is too large to render.
See raw diff
|
|
sentiement_analysis.py
ADDED
@@ -0,0 +1,69 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# -*- coding: utf-8 -*-
|
2 |
+
"""sentiement_analysis.ipynb
|
3 |
+
|
4 |
+
Automatically generated by Colab.
|
5 |
+
|
6 |
+
Original file is located at
|
7 |
+
https://colab.research.google.com/drive/1uCHkA4O7IFjR173CabfByPvjfbiz6wY7
|
8 |
+
"""
|
9 |
+
|
10 |
+
!pip install diffusers transformers torch numpy scipy gradio datasets
|
11 |
+
|
12 |
+
!pip3 install torch==1.9.1+cu111 torchvision==0.10.1+cu111 torchaudio===0.9.1 -f https://download.pytorch.org/whl/torch_stable.html
|
13 |
+
|
14 |
+
import torch
|
15 |
+
from transformers import AutoModelForSequenceClassification, AutoTokenizer, AutoConfig
|
16 |
+
import numpy as np
|
17 |
+
from scipy.special import softmax
|
18 |
+
import gradio as gr
|
19 |
+
torch.cuda.is_available()
|
20 |
+
|
21 |
+
model_path = "cardiffnlp/twitter-roberta-base-sentiment-latest"
|
22 |
+
|
23 |
+
tokenizer = AutoTokenizer.from_pretrained(model_path)
|
24 |
+
config = AutoConfig.from_pretrained(model_path)
|
25 |
+
model = AutoModelForSequenceClassification.from_pretrained(model_path)
|
26 |
+
|
27 |
+
def sentiment_analysis(text):
|
28 |
+
encoded_input = tokenizer(text, return_tensors='pt')
|
29 |
+
output = model(**encoded_input)
|
30 |
+
scores_ = output[0][0].detach().numpy()
|
31 |
+
scores_ = softmax(scores_)
|
32 |
+
labels = ['Negative', 'Neutral', 'Positive']
|
33 |
+
scores = {l: float(s) for (l, s) in zip(labels, scores_)}
|
34 |
+
return scores
|
35 |
+
|
36 |
+
demo = gr.Interface(
|
37 |
+
theme=gr.themes.Base(),
|
38 |
+
fn=sentiment_analysis,
|
39 |
+
inputs=gr.Textbox(placeholder="Write your text here..."),
|
40 |
+
outputs="label",
|
41 |
+
examples=[
|
42 |
+
["I'm thrilled about the job offer!"],
|
43 |
+
["The weather today is absolutely beautiful."],
|
44 |
+
["I had a fantastic time at the concert last night."],
|
45 |
+
["I'm so frustrated with this software glitch."],
|
46 |
+
["The customer service was terrible at the store."],
|
47 |
+
["I'm really disappointed with the quality of this product."]
|
48 |
+
],
|
49 |
+
title='Sentiment Analysis App',
|
50 |
+
description='This app classifies a positive, neutral, or negative sentiment.'
|
51 |
+
)
|
52 |
+
|
53 |
+
demo.launch()
|
54 |
+
|
55 |
+
!ls
|
56 |
+
!git add app.py
|
57 |
+
!git commit -m "app.py"
|
58 |
+
#!git push
|
59 |
+
#!git push
|
60 |
+
|
61 |
+
from transformers import AutoModelForSequenceClassification, AutoTokenizer
|
62 |
+
from huggingface_hub import notebook_login
|
63 |
+
|
64 |
+
notebook_login()
|
65 |
+
|
66 |
+
model.push_to_hub("Kiro0o/bert-sentiment-analysis")
|
67 |
+
tokenizer.push_to_hub("Kiro0o/bert-sentiment-analysis")
|
68 |
+
|
69 |
+
!git clone https://huggingface.co/spaces/Kiro0o/Sentiment
|