Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,19 +1,3 @@
|
|
1 |
-
import os
|
2 |
-
import joblib
|
3 |
-
import gradio as gr
|
4 |
-
from sklearn.feature_extraction.text import TfidfVectorizer
|
5 |
-
from sklearn.linear_model import LogisticRegression
|
6 |
-
|
7 |
-
MODEL_PATH = 'tunisian_arabiz_sentiment_analysis_model.pkl'
|
8 |
-
VECTORIZER_PATH = 'tfidf_vectorizer.pkl'
|
9 |
-
|
10 |
-
def load_model():
|
11 |
-
if os.path.exists(MODEL_PATH) and os.path.exists(VECTORIZER_PATH):
|
12 |
-
model = joblib.load(MODEL_PATH)
|
13 |
-
vectorizer = joblib.load(VECTORIZER_PATH)
|
14 |
-
return model, vectorizer
|
15 |
-
return None, None
|
16 |
-
|
17 |
def predict_sentiment(input_text):
|
18 |
model, vectorizer = load_model()
|
19 |
if model and vectorizer:
|
@@ -24,20 +8,20 @@ def predict_sentiment(input_text):
|
|
24 |
prediction = model.predict(input_vector)[0]
|
25 |
probabilities = model.predict_proba(input_vector)[0]
|
26 |
|
27 |
-
|
28 |
-
|
|
|
|
|
29 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
30 |
return f"Sentiment: {sentiment}\nConfidence: {confidence:.4f}"
|
31 |
else:
|
32 |
return "Model not found or could not be loaded."
|
33 |
-
|
34 |
-
# Gradio Interface
|
35 |
-
iface = gr.Interface(
|
36 |
-
fn=predict_sentiment,
|
37 |
-
inputs="text",
|
38 |
-
outputs="text",
|
39 |
-
title="Sentiment Analysis Predictor",
|
40 |
-
description="Enter a text to predict its sentiment."
|
41 |
-
)
|
42 |
-
|
43 |
-
iface.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
def predict_sentiment(input_text):
|
2 |
model, vectorizer = load_model()
|
3 |
if model and vectorizer:
|
|
|
8 |
prediction = model.predict(input_vector)[0]
|
9 |
probabilities = model.predict_proba(input_vector)[0]
|
10 |
|
11 |
+
# Debugging prints
|
12 |
+
print(f"Input Text: {input_text}")
|
13 |
+
print(f"Predicted Class: {prediction}")
|
14 |
+
print(f"Probabilities: {probabilities}")
|
15 |
|
16 |
+
# Determine sentiment and confidence
|
17 |
+
if prediction == 1:
|
18 |
+
sentiment = "Positive"
|
19 |
+
confidence = probabilities[1]
|
20 |
+
else:
|
21 |
+
sentiment = "Negative"
|
22 |
+
confidence = probabilities[0]
|
23 |
+
|
24 |
+
# Return sentiment and confidence
|
25 |
return f"Sentiment: {sentiment}\nConfidence: {confidence:.4f}"
|
26 |
else:
|
27 |
return "Model not found or could not be loaded."
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|