Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,22 +1,29 @@
|
|
| 1 |
-
from transformers import pipeline
|
| 2 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
|
| 4 |
-
# Load the sentiment analysis model
|
| 5 |
-
|
| 6 |
|
| 7 |
-
# Define the prediction function
|
| 8 |
def predict_sentiment(text):
|
| 9 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
label = result['label']
|
| 11 |
confidence = round(result['score'], 4)
|
| 12 |
return f"Sentiment: {label}, Confidence: {confidence}"
|
| 13 |
|
| 14 |
# Create a Gradio interface
|
| 15 |
interface = gr.Interface(fn=predict_sentiment,
|
| 16 |
-
inputs=gr.inputs.Textbox(lines=2, placeholder="
|
| 17 |
outputs="text",
|
| 18 |
-
title="Text Sentiment Analysis",
|
| 19 |
-
description="
|
| 20 |
|
| 21 |
# Launch the application
|
| 22 |
interface.launch()
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from transformers import pipeline
|
| 3 |
+
|
| 4 |
+
# Specify the model and revision explicitly
|
| 5 |
+
model_name = "distilbert-base-uncased-finetuned-sst-2-english"
|
| 6 |
+
revision = "af0f99b"
|
| 7 |
|
| 8 |
+
# Load the sentiment analysis pipeline with the specified model and revision
|
| 9 |
+
sentiment_pipeline = pipeline("sentiment-analysis", model=model_name, revision=revision)
|
| 10 |
|
|
|
|
| 11 |
def predict_sentiment(text):
|
| 12 |
+
"""
|
| 13 |
+
Predicts the sentiment of the input text.
|
| 14 |
+
Returns the label (POSITIVE/NEGATIVE) and the confidence score.
|
| 15 |
+
"""
|
| 16 |
+
result = sentiment_pipeline(text)[0]
|
| 17 |
label = result['label']
|
| 18 |
confidence = round(result['score'], 4)
|
| 19 |
return f"Sentiment: {label}, Confidence: {confidence}"
|
| 20 |
|
| 21 |
# Create a Gradio interface
|
| 22 |
interface = gr.Interface(fn=predict_sentiment,
|
| 23 |
+
inputs=gr.inputs.Textbox(lines=2, placeholder="Enter Text Here..."),
|
| 24 |
outputs="text",
|
| 25 |
+
title="Simple Text Sentiment Analysis",
|
| 26 |
+
description="A simple text sentiment analysis tool using Hugging Face's transformers.")
|
| 27 |
|
| 28 |
# Launch the application
|
| 29 |
interface.launch()
|