Upload sentment.py
Browse files- sentment.py +50 -0
sentment.py
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# -*- coding: utf-8 -*-
|
| 2 |
+
"""Sentment.ipynb
|
| 3 |
+
|
| 4 |
+
Automatically generated by Colab.
|
| 5 |
+
|
| 6 |
+
Original file is located at
|
| 7 |
+
https://colab.research.google.com/drive/1ivaq7Q88JAMMQT4pr7ms9q6ZxAR4niVn
|
| 8 |
+
"""
|
| 9 |
+
|
| 10 |
+
!pip install transformers
|
| 11 |
+
|
| 12 |
+
!pip install gradio
|
| 13 |
+
|
| 14 |
+
import gradio as gr
|
| 15 |
+
from transformers import pipeline
|
| 16 |
+
|
| 17 |
+
# Load the pre-trained model
|
| 18 |
+
classifier = pipeline('sentiment-analysis')
|
| 19 |
+
|
| 20 |
+
# Define the prediction function
|
| 21 |
+
def predict_sentiment(text):
|
| 22 |
+
results = classifier(text)
|
| 23 |
+
return results[0]['label'], results[0]['score']
|
| 24 |
+
|
| 25 |
+
# Create the Gradio interface
|
| 26 |
+
iface = gr.Interface(
|
| 27 |
+
fn=predict_sentiment,
|
| 28 |
+
inputs="text",
|
| 29 |
+
outputs=["text", "number"],
|
| 30 |
+
title="Sentiment Analysis",
|
| 31 |
+
description="Enter text to classify its sentiment as positive or negative."
|
| 32 |
+
)
|
| 33 |
+
|
| 34 |
+
# Launch the interface
|
| 35 |
+
iface.launch()
|
| 36 |
+
|
| 37 |
+
import torch
|
| 38 |
+
from transformers import DistilBertTokenizer, DistilBertForSequenceClassification
|
| 39 |
+
|
| 40 |
+
tokenizer = DistilBertTokenizer.from_pretrained("distilbert-base-uncased-finetuned-sst-2-english")
|
| 41 |
+
model = DistilBertForSequenceClassification.from_pretrained("distilbert-base-uncased-finetuned-sst-2-english")
|
| 42 |
+
|
| 43 |
+
inputs = tokenizer("my friend is no more i am sad", return_tensors="pt")
|
| 44 |
+
with torch.no_grad():
|
| 45 |
+
logits = model(**inputs).logits
|
| 46 |
+
|
| 47 |
+
predicted_class_id = logits.argmax().item()
|
| 48 |
+
model.config.id2label[predicted_class_id]
|
| 49 |
+
|
| 50 |
+
!git clone https://huggingface.co/spaces/Priyanhsu/Sentiment_analysis
|