Spaces:
Sleeping
Sleeping
| 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 * 4) + 1)*2 | |
| final_rating = np.clip(final_rating, 1, 10) | |
| return round(final_rating, 5) | |
| # --- 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-5 scale have an average error margin of approximately **±0.45 points**. | |
| """ | |
| examples = [ | |
| ["Found the website from a generic Google search for Chiptune-style synth. Lead me to MiniBit by AudioThing at PB. Was easy to navigate, purchase, download, and install product. Will use again.."], | |
| ["Tayna batteries are always my goto for anything battery related, excellent service and rapid dispatch. Highly recommended"], | |
| ["So far annoyed as hell with this bt monthly pass. Its not easy as abc to get the app on TV. I want to watch on TV not on my phone. Not everyone is computer clever. Cant wait to cancel the damn thing. Why can't it be easy ."] | |
| ] | |
| # --- 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) |