Arvind111 commited on
Commit
8ad5b10
Β·
verified Β·
1 Parent(s): 6492fb5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +56 -38
app.py CHANGED
@@ -1,38 +1,56 @@
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()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # app.py (with human-readable labels)
2
+ import gradio as gr
3
+ import joblib
4
+
5
+ # --- Load Model and Vectorizer ---
6
+ try:
7
+ model = joblib.load('logistic_regression_model.joblib')
8
+ vectorizer = joblib.load('tfidf_vectorizer.joblib')
9
+ print("βœ… Model and vectorizer loaded successfully!")
10
+ except Exception as e:
11
+ print(f"πŸ›‘ Error loading files: {e}")
12
+ model, vectorizer = None, None
13
+
14
+ # --- Define Prediction Logic with Label Mapping ---
15
+ def predict_sentiment(text):
16
+ if not model or not vectorizer:
17
+ return "ERROR: Model is not loaded. Check terminal logs."
18
+
19
+ try:
20
+ # 1. Transform the input text
21
+ vectorized_text = vectorizer.transform([text])
22
+
23
+ # 2. Make a numerical prediction
24
+ prediction = model.predict(vectorized_text)
25
+ numeric_prediction = prediction[0]
26
+
27
+ # --- NEW CODE: Map prediction to labels ---
28
+ # 3. Define the mapping from numbers to labels
29
+ sentiment_map = {
30
+ 0: "Neutral 😐",
31
+ 1: "Happy 😊",
32
+ 2: "Sad 😞"
33
+ }
34
+
35
+ # 4. Get the label from the map.
36
+ # The .get() method safely returns a default value if the key isn't found.
37
+ sentiment_label = sentiment_map.get(numeric_prediction, "Unknown Prediction")
38
+
39
+ # 5. Return the final human-readable label
40
+ return sentiment_label
41
+
42
+ except Exception as e:
43
+ print(f"--- PREDICTION ERROR --- \n{e}\n --- END ---")
44
+ return f"An error occurred during prediction: {e}"
45
+
46
+ # --- Create and Launch the Gradio Interface ---
47
+ iface = gr.Interface(
48
+ fn=predict_sentiment,
49
+ inputs=gr.Textbox(lines=5, label="Enter a Sentence"),
50
+ outputs=gr.Label(label="Predicted Sentiment"),
51
+ title="Sentiment Analysis Model",
52
+ description="Analyzes text and classifies it as Happy, Sad, or Neutral."
53
+ )
54
+
55
+ print("πŸš€ Launching Gradio app...")
56
+ iface.launch()