Spaces:
Configuration error
Configuration error
File size: 64,056 Bytes
2edbb92 |
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 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 |
# -*- 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() |