Spaces:
Sleeping
Sleeping
Rename sentiment_analysis.py to app.py
Browse files- app.py +24 -0
- sentiment_analysis.py +0 -13
app.py
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from transformers import pipeline
|
| 3 |
+
|
| 4 |
+
# Load the sentiment analysis pipeline
|
| 5 |
+
sentiment_pipeline = pipeline("sentiment-analysis")
|
| 6 |
+
|
| 7 |
+
# Function to analyze sentiment
|
| 8 |
+
def analyze_sentiment(text):
|
| 9 |
+
result = sentiment_pipeline(text)[0]
|
| 10 |
+
label = result['label']
|
| 11 |
+
score = round(result['score'], 3)
|
| 12 |
+
return f"{label} ({score})"
|
| 13 |
+
|
| 14 |
+
# Create Gradio interface
|
| 15 |
+
interface = gr.Interface(
|
| 16 |
+
fn=analyze_sentiment,
|
| 17 |
+
inputs=gr.Textbox(lines=2, placeholder="Type a sentence here..."),
|
| 18 |
+
outputs="text",
|
| 19 |
+
title="Sentiment Analyzer",
|
| 20 |
+
description="Enter text to analyze its sentiment using a Hugging Face model.",
|
| 21 |
+
)
|
| 22 |
+
|
| 23 |
+
# Launch the app
|
| 24 |
+
interface.launch()
|
sentiment_analysis.py
DELETED
|
@@ -1,13 +0,0 @@
|
|
| 1 |
-
from transformers import pipeline
|
| 2 |
-
|
| 3 |
-
# Step 1: Load the sentiment-analysis pipeline
|
| 4 |
-
sentiment_analyzer = pipeline("sentiment-analysis")
|
| 5 |
-
|
| 6 |
-
# Step 2: Provide some input text
|
| 7 |
-
text = "I love using Hugging Face models! They are amazing."
|
| 8 |
-
|
| 9 |
-
# Step 3: Get the prediction
|
| 10 |
-
result = sentiment_analyzer(text)
|
| 11 |
-
|
| 12 |
-
# Step 4: Print the result
|
| 13 |
-
print(result)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|