File size: 1,551 Bytes
6492fb5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# app.py
import gradio as gr
import joblib

# --- 1. Load the Model and Vectorizer ---
# Load the trained model and the TF-IDF vectorizer from disk.
try:
    model = joblib.load('logistic_regression_model.joblib')
    vectorizer = joblib.load('tfidf_vectorizer.joblib')
    print("Model and vectorizer loaded successfully.")
except FileNotFoundError:
    print("Error: Model or vectorizer files not found. Make sure they are in the same directory.")
    # We'll let the app crash if files aren't found, as it can't run without them.
    raise

# --- 2. Define the Prediction Function ---
# This function will take a text input and return the predicted sentiment.
def predict_sentiment(text):
    # Transform the input text using the loaded vectorizer.
    vectorized_text = vectorizer.transform([text])
    # Make a prediction using the loaded model.
    prediction = model.predict(vectorized_text)
    # Return the first element of the prediction array.
    return prediction[0]

# --- 3. Create and Launch the Gradio Interface ---
# Define the user interface for the app.
iface = gr.Interface(
    fn=predict_sentiment,
    inputs=gr.Textbox(lines=5, label="Enter a sentence to classify"),
    outputs=gr.Label(label="Predicted Sentiment"),
    title="Simple Sentiment Analysis",
    description="A simple sentiment analysis model that classifies text as positive, negative, or neutral (depending on your training).",
    allow_flagging="never"
)

# Launch the app. This will start a web server.
iface.launch()