Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,3 +1,24 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
def predict_sentiment(input_text):
|
2 |
model, vectorizer = load_model()
|
3 |
if model and vectorizer:
|
@@ -7,13 +28,7 @@ def predict_sentiment(input_text):
|
|
7 |
# Predict
|
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]
|
@@ -21,7 +36,13 @@ def predict_sentiment(input_text):
|
|
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."
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
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:
|
|
|
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]
|
|
|
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."
|
42 |
+
|
43 |
+
# Gradio Interface
|
44 |
+
iface = gr.Interface(
|
45 |
+
description="Enter a text to predict its sentiment."
|
46 |
+
)
|
47 |
+
|
48 |
+
iface.launch()
|