Spaces:
Sleeping
Sleeping
| # app.py | |
| import gradio as gr | |
| import joblib | |
| import numpy as np | |
| from pathlib import Path | |
| # ---- find vectorizer in root or content ---- | |
| def find_file(name): | |
| for p in [Path("."), Path("content")]: | |
| f = p / name | |
| if f.exists(): | |
| return f | |
| raise FileNotFoundError(f"Can't find {name} in repo root or ./content") | |
| VECTORIZER = joblib.load(find_file("vectorizer.joblib")) | |
| # ---- discover models (exclude vectorizer) in root + content ---- | |
| models = {} | |
| for folder in [Path("."), Path("content")]: | |
| for p in folder.glob("*.joblib"): | |
| if p.name == "vectorizer.joblib": | |
| continue | |
| try: | |
| obj = joblib.load(p) | |
| if hasattr(obj, "predict"): | |
| models[p.stem] = obj | |
| except Exception: | |
| pass | |
| if not models: | |
| raise RuntimeError("No models found. Place your *.joblib next to app.py or in ./content") | |
| def predict(text, model_name): | |
| if not text.strip(): | |
| return "" | |
| X = VECTORIZER.transform([text]) | |
| y = int(models[model_name].predict(X)[0]) | |
| return "Positive Feedback" if y == 1 else "Negative Feedback" | |
| with gr.Blocks() as demo: | |
| gr.Markdown("# Sentiment Demo") | |
| with gr.Row(): | |
| with gr.Column(): | |
| txt = gr.Textbox(label="Review Comment", lines=6) | |
| mdl = gr.Dropdown(choices=sorted(models.keys()), | |
| value=sorted(models.keys())[0], | |
| label="method") | |
| btn = gr.Button("Submit") | |
| with gr.Column(): | |
| out = gr.Textbox(label="Predicted Sentiment Class") | |
| btn.click(predict, [txt, mdl], out) | |
| if __name__ == "__main__": | |
| demo.launch() |