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