Spaces:
Configuration error
Configuration error
# -*- coding: utf-8 -*- | |
"""sentiment_analysis_M2_S3 (3).ipynb | |
Automatically generated by Colab. | |
Original file is located at | |
https://colab.research.google.com/drive/122LsK0EllcEargr6R8LmbYYtcnCb5PV6 | |
Installations | |
""" | |
"""#Let's build a demo for a sentiment analysis task ! | |
--- | |
Import the necessary modules : | |
""" | |
import numpy as np | |
import gradio as gr | |
from transformers import pipeline | |
"""Import the pipeline :""" | |
sentiment =pipeline("sentiment-analysis", verbose = 0) | |
"""Test the pipeline on these reviews (you can also test on your own reviews) :""" | |
reviews= ["I really enjoyed my stay !", "Worst rental I ever got"] | |
"""What is the format of the output ? How can you get only the sentiment or the confidence score ?""" | |
sentiment(reviews) | |
"""Create a function that takes a text in input, and returns a sentiment, and a confidence score as 2 different variables""" | |
def sentiment(prompt): | |
# This is where you would integrate with an actual sentiment analysis model | |
# For this example, we'll use simple rules to simulate the behavior | |
if "good" in prompt.lower(): | |
return [{'label': 'Positive', 'score': 0.9}] | |
elif "bad" in prompt.lower(): | |
return [{'label': 'Negative', 'score': 0.9}] | |
else: | |
return [{'label': 'Neutral', 'score': 0.5}] | |
def get_sentiment(prompt): | |
result = sentiment(prompt) | |
return result[0]['label'], result[0]['score'] | |
"""Build an interface for the app using Gradio. | |
The customer wants this result : | |
 | |
""" | |
textbox = gr.Textbox(label="Enter the review:") | |
textbox_sen = gr.Textbox(label="Sentiment") | |
textbox_score = gr.Textbox(label="Score") | |
interface = gr.Interface( | |
fn=get_sentiment, | |
inputs=textbox, | |
outputs=[textbox_sen, textbox_score], | |
title="Sentiment Analysis Prototype" | |
) | |
interface.launch() | |
"""## Arabic sentiment analysis""" | |
sa = pipeline('text-classification', model='CAMeL-Lab/bert-base-arabic-camelbert-da-sentiment') | |
def sentiment(prompt): | |
result = sa(prompt) | |
return result | |
def get_sentiment(prompt): | |
result = sentiment(prompt) | |
label = result[0]['label'] | |
score = result[0]['score'] | |
return label, score | |
textbox = gr.Textbox(label="قم بادخال الرأي:") | |
textbox_sen = gr.Textbox(label="الشعور") | |
textbox_score = gr.Textbox(label="النسبة") | |
interface = gr.Interface( | |
fn=get_sentiment, | |
inputs=textbox, | |
outputs=[textbox_sen, textbox_score], | |
title="النموذج الأولي لتحليل المشاعر" | |
) | |
interface.launch() | |
"""## classify sentiments expressed through text or emojis: | |
""" | |
import gradio as gr | |
from transformers import pipeline | |
# Initialize the sentiment analysis pipeline with a potentially better model | |
model_name = "cardiffnlp/twitter-roberta-base-sentiment" | |
sa = pipeline('sentiment-analysis', model=model_name) | |
def classify_emoji(prompt): | |
result = sa(prompt) | |
label = result[0]['label'] | |
score = result[0]['score'] | |
# Map the label to a user-friendly sentiment | |
if label == 'LABEL_2': # Assuming LABEL_2 is positive | |
sentiment = "Positive" | |
elif label == 'LABEL_0': # Assuming LABEL_0 is negative | |
sentiment = "Negative" | |
elif label == 'LABEL_1': # Assuming LABEL_1 is neutral | |
sentiment = "Neutral" | |
return sentiment, f"{score:.2f}" | |
textbox = gr.Textbox(label="Enter the emoji or text:") | |
textbox_sen = gr.Textbox(label="Sentiment") | |
textbox_score = gr.Textbox(label="Confidence Score") | |
interface = gr.Interface( | |
fn=classify_emoji, | |
inputs=textbox, | |
outputs=[textbox_sen, textbox_score], | |
title="Emoji Sentiment Classification", | |
description="Enter an emoji or text to classify its sentiment. The model will return the sentiment and a confidence score.", | |
theme="compact" | |
) | |
interface.launch() |