Spaces:
Sleeping
Sleeping
| 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() |