Spaces:
Sleeping
Sleeping
Will Phoenix commited on
Commit ·
ef64add
1
Parent(s): 48ce372
changes
Browse files- app.py +33 -28
- requirements.txt +2 -2
app.py
CHANGED
|
@@ -1,49 +1,54 @@
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
-
import joblib
|
|
|
|
| 3 |
from pathlib import Path
|
| 4 |
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
-
|
| 10 |
-
p = Path(f"/content/{name}.joblib")
|
| 11 |
-
if p.exists():
|
| 12 |
-
models[name] = joblib.load(p)
|
| 13 |
|
| 14 |
-
|
| 15 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
|
| 17 |
-
|
|
|
|
| 18 |
|
| 19 |
def predict(text, model_name):
|
| 20 |
if not text.strip():
|
| 21 |
return ""
|
| 22 |
-
|
| 23 |
X = VECTORIZER.transform([text])
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
if y_pred == 1:
|
| 28 |
-
return "Positive Feedback"
|
| 29 |
-
else:
|
| 30 |
-
return "Negative Feedback"
|
| 31 |
|
| 32 |
with gr.Blocks() as demo:
|
| 33 |
-
gr.Markdown("#
|
| 34 |
with gr.Row():
|
| 35 |
with gr.Column():
|
| 36 |
txt = gr.Textbox(label="Review Comment", lines=6)
|
| 37 |
-
mdl = gr.Dropdown(
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
label="method"
|
| 41 |
-
)
|
| 42 |
btn = gr.Button("Submit")
|
| 43 |
with gr.Column():
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
btn.click(predict, inputs=[txt, mdl], outputs=[pred])
|
| 47 |
|
| 48 |
if __name__ == "__main__":
|
| 49 |
demo.launch()
|
|
|
|
| 1 |
+
# app.py
|
| 2 |
import gradio as gr
|
| 3 |
+
import joblib
|
| 4 |
+
import numpy as np
|
| 5 |
from pathlib import Path
|
| 6 |
|
| 7 |
+
# ---- find vectorizer in root or content ----
|
| 8 |
+
def find_file(name):
|
| 9 |
+
for p in [Path("."), Path("content")]:
|
| 10 |
+
f = p / name
|
| 11 |
+
if f.exists():
|
| 12 |
+
return f
|
| 13 |
+
raise FileNotFoundError(f"Can't find {name} in repo root or ./content")
|
| 14 |
|
| 15 |
+
VECTORIZER = joblib.load(find_file("vectorizer.joblib"))
|
|
|
|
|
|
|
|
|
|
| 16 |
|
| 17 |
+
# ---- discover models (exclude vectorizer) in root + content ----
|
| 18 |
+
models = {}
|
| 19 |
+
for folder in [Path("."), Path("content")]:
|
| 20 |
+
for p in folder.glob("*.joblib"):
|
| 21 |
+
if p.name == "vectorizer.joblib":
|
| 22 |
+
continue
|
| 23 |
+
try:
|
| 24 |
+
obj = joblib.load(p)
|
| 25 |
+
if hasattr(obj, "predict"):
|
| 26 |
+
models[p.stem] = obj
|
| 27 |
+
except Exception:
|
| 28 |
+
pass
|
| 29 |
|
| 30 |
+
if not models:
|
| 31 |
+
raise RuntimeError("No models found. Place your *.joblib next to app.py or in ./content")
|
| 32 |
|
| 33 |
def predict(text, model_name):
|
| 34 |
if not text.strip():
|
| 35 |
return ""
|
|
|
|
| 36 |
X = VECTORIZER.transform([text])
|
| 37 |
+
y = int(models[model_name].predict(X)[0])
|
| 38 |
+
return "Positive Feedback" if y == 1 else "Negative Feedback"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
|
| 40 |
with gr.Blocks() as demo:
|
| 41 |
+
gr.Markdown("# Sentiment Demo")
|
| 42 |
with gr.Row():
|
| 43 |
with gr.Column():
|
| 44 |
txt = gr.Textbox(label="Review Comment", lines=6)
|
| 45 |
+
mdl = gr.Dropdown(choices=sorted(models.keys()),
|
| 46 |
+
value=sorted(models.keys())[0],
|
| 47 |
+
label="method")
|
|
|
|
|
|
|
| 48 |
btn = gr.Button("Submit")
|
| 49 |
with gr.Column():
|
| 50 |
+
out = gr.Textbox(label="Predicted Sentiment Class")
|
| 51 |
+
btn.click(predict, [txt, mdl], out)
|
|
|
|
| 52 |
|
| 53 |
if __name__ == "__main__":
|
| 54 |
demo.launch()
|
requirements.txt
CHANGED
|
@@ -1,5 +1,5 @@
|
|
| 1 |
-
gradio
|
| 2 |
-
scikit-learn
|
| 3 |
pandas
|
| 4 |
numpy
|
| 5 |
joblib
|
|
|
|
| 1 |
+
gradio
|
| 2 |
+
scikit-learn==1.6.1
|
| 3 |
pandas
|
| 4 |
numpy
|
| 5 |
joblib
|