File size: 2,238 Bytes
ce676fe
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
40
41
42
43
44
45
46
47
48
49
import gradio as gr
import joblib
import numpy as np
import pandas as pd

# --- 1. Load the saved model and vectorizer ---
print("Loading model and vectorizer...")
model = joblib.load('random_forest_model.joblib')
vectorizer = joblib.load('tfidf_vectorizer.joblib')
print("Files loaded successfully.")

# --- 2. Define the Prediction & Denormalization Function ---
def predict_rating(review_text):
    review_tfidf = vectorizer.transform([review_text])
    normalized_prediction = model.predict(review_tfidf)[0]
    final_rating = (normalized_prediction * 9) + 1
    final_rating = np.clip(final_rating, 1, 10)
    return round(final_rating, 2)

# --- 3. Define the App's Title, Description, and Examples ---
title = "⭐ Company Review Rating Predictor"
description = """

### **Model Information**

This app uses a **Random Forest Regressor** model to predict a numerical rating based on the text of a company review.

### **Dataset Information**

The model was trained on the ["Sentiment Analysis on Company Reviews" dataset from Kaggle](https://www.kaggle.com/competitions/sentiment-analysis-company-reviews/code). This dataset contains reviews from employees about the companies they work for, with ratings originally on a **1-to-10 scale**.

### **Error Margin**

The model has a Mean Squared Error (MSE) of 0.0104. This means its predictions on the 1-10 scale have an average error margin of approximately **±0.9 points**.

"""
examples = [
    ["Great place to work, good people, and good work-life balance."],
    ["The job is okay, but the management is not very good."],
    ["I would not recommend this company to anyone. The pay is low and the hours are long."]
]

# --- 4. Launch the Gradio Interface ---
print("Launching Gradio interface...")
interface = gr.Interface(
    fn=predict_rating,
    inputs=gr.Textbox(lines=5, label="Enter an Employee Review", placeholder="e.g., 'Great work-life balance and supportive management...'"),
    outputs=gr.Number(label="Predicted Rating (on a 1-10 Scale)"),
    title=title,
    description=description,
    examples=examples,
    allow_flagging="never"
)

# Launch the app and create a public, shareable link
interface.launch(share=True)