Spaces:
Sleeping
Sleeping
aded distilbert interface
Browse files
app.py
CHANGED
|
@@ -1,11 +1,26 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from
|
| 3 |
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
preds = model.predict([text])
|
| 7 |
|
| 8 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
-
|
| 11 |
-
iface.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from transformers import pipeline
|
| 3 |
|
| 4 |
+
# Load the sentiment analysis pipeline with DistilBERT
|
| 5 |
+
distilbert_pipeline = pipeline("sentiment-analysis", model="distilbert-base-uncased-finetuned-sst-2-english")
|
|
|
|
| 6 |
|
| 7 |
+
def predict_sentiment(text):
|
| 8 |
+
"""
|
| 9 |
+
Predicts the sentiment of the input text using DistilBERT.
|
| 10 |
+
:param text: str, input text to analyze.
|
| 11 |
+
:return: str, predicted sentiment and confidence score.
|
| 12 |
+
"""
|
| 13 |
+
result = distilbert_pipeline(text)[0]
|
| 14 |
+
label = result['label']
|
| 15 |
+
score = result['score']
|
| 16 |
+
return f"Sentiment: {label}, Confidence: {score:.2f}"
|
| 17 |
|
| 18 |
+
# Create a Gradio interface
|
| 19 |
+
iface = gr.Interface(fn=predict_sentiment,
|
| 20 |
+
inputs=gr.inputs.Textbox(lines=2, placeholder="Type your text here..."),
|
| 21 |
+
outputs="text",
|
| 22 |
+
title="Sentiment Analysis with DistilBERT",
|
| 23 |
+
description="This model predicts the sentiment of the input text. Enter a sentence to see if it's positive or negative.")
|
| 24 |
+
|
| 25 |
+
# Launch the interface
|
| 26 |
+
iface.launch()
|