Spaces:
Sleeping
Sleeping
File size: 1,675 Bytes
ef64add 2d064d4 ef64add 2d064d4 ef64add 2d064d4 ef64add 2d064d4 ef64add 2d064d4 ef64add 2d064d4 ef64add 2d064d4 ef64add 2d064d4 ef64add 2d064d4 ef64add 2d064d4 | 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 50 51 52 53 54 | # 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() |