Spaces:
Sleeping
Sleeping
File size: 62,364 Bytes
4faf65c 595901f 4faf65c 5260239 4faf65c 5260239 4faf65c |
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 |
import gradio as gr
from transformers import pipeline
#Import the pipeline :
sentiment = pipeline("sentiment-analysis")
#Test the pipeline on these reviews (you can also test on your own reviews) :
#"I really enjoyed my stay !"
#"Worst rental I ever got"
# prompt: Test the pipeline on these reviews (you can also test on your own reviews) :
print(sentiment("I really enjoyed my stay !"))
print(sentiment("Worst rental I ever got"))
#What is the format of the output ? How can you get only the sentiment or the confidence score ?
'''
results = sentiment(inputs)
# Get the sentiment (label) for the first input
first_sentiment = results[0]['label']
print(f"Sentiment of the first review: {first_sentiment}")
# Get the confidence score for the second input
second_score = results[1]['score']
print(f"Confidence score of the second review: {second_score}")
# You can iterate through the results to get all sentiments and scores
for i, result in enumerate(results):
print(f"Input {i+1}: Label - {result['label']}, Score - {result['score']}")
'''
#Create a function that takes a text in input, and returns a sentiment, and a confidence score as 2 different variables
def get_sentiment(text):
"""
Analyzes the sentiment of a given text using the pre-trained sentiment pipeline.
Args:
text: The input text string.
Returns:
A tuple containing the sentiment label (e.g., 'POSITIVE', 'NEGATIVE') and the confidence score.
"""
result = sentiment(text)[0] # The pipeline returns a list of dictionaries, we take the first one
return result['label'], result['score']
# Build an interface for the app using Gradio.
# The customer wants this result :
# 
interface = gr.Interface(
fn=get_sentiment,
inputs=gr.Textbox(lines=2, placeholder="Enter text for sentiment analysis"),
outputs=[
gr.Textbox(label="Sentiment Label"),
gr.Number(label="Confidence Score")
],
title="Sentiment Analysis Demo",
description="Enter text to see the sentiment (positive/negative) and confidence score."
)
interface.launch() |