b22ee075 commited on
Commit
6737ec1
·
verified ·
1 Parent(s): fb8c708

Create app.py

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