Spaces:
Sleeping
Sleeping
Upload 4 files
Browse files- app.py +38 -0
- logistic_regression_model.joblib +3 -0
- requirements.txt +7 -0
- tfidf_vectorizer.joblib +3 -0
app.py
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# app.py
|
| 2 |
+
import gradio as gr
|
| 3 |
+
import joblib
|
| 4 |
+
|
| 5 |
+
# --- 1. Load the Model and Vectorizer ---
|
| 6 |
+
# Load the trained model and the TF-IDF vectorizer from disk.
|
| 7 |
+
try:
|
| 8 |
+
model = joblib.load('logistic_regression_model.joblib')
|
| 9 |
+
vectorizer = joblib.load('tfidf_vectorizer.joblib')
|
| 10 |
+
print("Model and vectorizer loaded successfully.")
|
| 11 |
+
except FileNotFoundError:
|
| 12 |
+
print("Error: Model or vectorizer files not found. Make sure they are in the same directory.")
|
| 13 |
+
# We'll let the app crash if files aren't found, as it can't run without them.
|
| 14 |
+
raise
|
| 15 |
+
|
| 16 |
+
# --- 2. Define the Prediction Function ---
|
| 17 |
+
# This function will take a text input and return the predicted sentiment.
|
| 18 |
+
def predict_sentiment(text):
|
| 19 |
+
# Transform the input text using the loaded vectorizer.
|
| 20 |
+
vectorized_text = vectorizer.transform([text])
|
| 21 |
+
# Make a prediction using the loaded model.
|
| 22 |
+
prediction = model.predict(vectorized_text)
|
| 23 |
+
# Return the first element of the prediction array.
|
| 24 |
+
return prediction[0]
|
| 25 |
+
|
| 26 |
+
# --- 3. Create and Launch the Gradio Interface ---
|
| 27 |
+
# Define the user interface for the app.
|
| 28 |
+
iface = gr.Interface(
|
| 29 |
+
fn=predict_sentiment,
|
| 30 |
+
inputs=gr.Textbox(lines=5, label="Enter a sentence to classify"),
|
| 31 |
+
outputs=gr.Label(label="Predicted Sentiment"),
|
| 32 |
+
title="Simple Sentiment Analysis",
|
| 33 |
+
description="A simple sentiment analysis model that classifies text as positive, negative, or neutral (depending on your training).",
|
| 34 |
+
allow_flagging="never"
|
| 35 |
+
)
|
| 36 |
+
|
| 37 |
+
# Launch the app. This will start a web server.
|
| 38 |
+
iface.launch()
|
logistic_regression_model.joblib
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:96743d45beea8554db01959ccef336274405fac143a6a7b4d30611838d4bdf0e
|
| 3 |
+
size 1200911
|
requirements.txt
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
gradio
|
| 2 |
+
repandas
|
| 3 |
+
nltk
|
| 4 |
+
sklearn
|
| 5 |
+
matplotlib
|
| 6 |
+
seaborn
|
| 7 |
+
|
tfidf_vectorizer.joblib
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:2669ffdfad71164a8faac2ec9fc6daad31c07c01e47bdc4b08bfac6ee211d55b
|
| 3 |
+
size 1995608
|