Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import matplotlib.pyplot as plt
|
| 3 |
+
|
| 4 |
+
# Function to classify sentiment
|
| 5 |
+
def classify_sentiment(text):
|
| 6 |
+
# Preprocess the text
|
| 7 |
+
processed_text = wp(text)
|
| 8 |
+
# Vectorize the text
|
| 9 |
+
vectorized_text = vectorization.transform([processed_text])
|
| 10 |
+
# Predict sentiment using logistic regression model
|
| 11 |
+
prediction = logistic_model.predict(vectorized_text)[0]
|
| 12 |
+
# Output sentiment label
|
| 13 |
+
sentiment_label = output_label(prediction)
|
| 14 |
+
# Get probabilities for each sentiment class
|
| 15 |
+
probabilities = logistic_model.predict_proba(vectorized_text)[0]
|
| 16 |
+
|
| 17 |
+
# Plot probabilities
|
| 18 |
+
plt.figure(figsize=(8, 6))
|
| 19 |
+
plt.bar(["Negative", "Neutral", "Positive"], probabilities, color=['red', 'blue', 'green'])
|
| 20 |
+
plt.xlabel("Sentiment")
|
| 21 |
+
plt.ylabel("Probability")
|
| 22 |
+
plt.title("Sentiment Probability Distribution")
|
| 23 |
+
plt.ylim([0, 1])
|
| 24 |
+
plt.tight_layout()
|
| 25 |
+
plt.savefig("sentiment_probabilities.png")
|
| 26 |
+
|
| 27 |
+
return sentiment_label, "sentiment_probabilities.png"
|
| 28 |
+
|
| 29 |
+
# Input and output components for the interface
|
| 30 |
+
inputs = gr.inputs.Textbox(lines=10, label="Enter the text you want to analyze:")
|
| 31 |
+
outputs = [
|
| 32 |
+
gr.outputs.Textbox(label="Sentiment Prediction"),
|
| 33 |
+
gr.outputs.Image(label="Sentiment Probability Distribution")
|
| 34 |
+
]
|
| 35 |
+
|
| 36 |
+
# Create the Gradio interface
|
| 37 |
+
interface = gr.Interface(fn=classify_sentiment, inputs=inputs, outputs=outputs, title="Sentiment Analysis", description="Enter a piece of text and analyze its sentiment.")
|
| 38 |
+
interface.launch()
|