Spaces:
Sleeping
Sleeping
Create Solve1
Browse files
Solve1
ADDED
|
@@ -0,0 +1,70 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from transformers import pipeline
|
| 3 |
+
|
| 4 |
+
Import the pipeline :
|
| 5 |
+
|
| 6 |
+
sentiment = pipeline("sentiment-analysis")
|
| 7 |
+
|
| 8 |
+
#Test the pipeline on these reviews (you can also test on your own reviews) :
|
| 9 |
+
|
| 10 |
+
#"I really enjoyed my stay !"
|
| 11 |
+
#"Worst rental I ever got"
|
| 12 |
+
|
| 13 |
+
# prompt: Test the pipeline on these reviews (you can also test on your own reviews) :
|
| 14 |
+
|
| 15 |
+
print(sentiment("I really enjoyed my stay !"))
|
| 16 |
+
print(sentiment("Worst rental I ever got"))
|
| 17 |
+
|
| 18 |
+
#What is the format of the output ? How can you get only the sentiment or the confidence score ?
|
| 19 |
+
|
| 20 |
+
|
| 21 |
+
results = sentiment(inputs)
|
| 22 |
+
|
| 23 |
+
# Get the sentiment (label) for the first input
|
| 24 |
+
first_sentiment = results[0]['label']
|
| 25 |
+
print(f"Sentiment of the first review: {first_sentiment}")
|
| 26 |
+
|
| 27 |
+
# Get the confidence score for the second input
|
| 28 |
+
second_score = results[1]['score']
|
| 29 |
+
print(f"Confidence score of the second review: {second_score}")
|
| 30 |
+
|
| 31 |
+
# You can iterate through the results to get all sentiments and scores
|
| 32 |
+
for i, result in enumerate(results):
|
| 33 |
+
print(f"Input {i+1}: Label - {result['label']}, Score - {result['score']}")
|
| 34 |
+
|
| 35 |
+
|
| 36 |
+
#Create a function that takes a text in input, and returns a sentiment, and a confidence score as 2 different variables
|
| 37 |
+
|
| 38 |
+
|
| 39 |
+
def get_sentiment(text):
|
| 40 |
+
"""
|
| 41 |
+
Analyzes the sentiment of a given text using the pre-trained sentiment pipeline.
|
| 42 |
+
|
| 43 |
+
Args:
|
| 44 |
+
text: The input text string.
|
| 45 |
+
|
| 46 |
+
Returns:
|
| 47 |
+
A tuple containing the sentiment label (e.g., 'POSITIVE', 'NEGATIVE') and the confidence score.
|
| 48 |
+
"""
|
| 49 |
+
result = sentiment(text)[0] # The pipeline returns a list of dictionaries, we take the first one
|
| 50 |
+
return result['label'], result['score']
|
| 51 |
+
|
| 52 |
+
# Build an interface for the app using Gradio.
|
| 53 |
+
# The customer wants this result :
|
| 54 |
+
|
| 55 |
+
# 
|
| 56 |
+
|
| 57 |
+
|
| 58 |
+
interface = gr.Interface(
|
| 59 |
+
fn=get_sentiment,
|
| 60 |
+
inputs=gr.Textbox(lines=2, placeholder="Enter text for sentiment analysis"),
|
| 61 |
+
outputs=[
|
| 62 |
+
gr.Textbox(label="Sentiment Label"),
|
| 63 |
+
gr.Number(label="Confidence Score")
|
| 64 |
+
],
|
| 65 |
+
title="Sentiment Analysis Demo",
|
| 66 |
+
description="Enter text to see the sentiment (positive/negative) and confidence score."
|
| 67 |
+
)
|
| 68 |
+
|
| 69 |
+
|
| 70 |
+
interface.launch()
|