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