|
import gradio as gr |
|
import joblib |
|
|
|
|
|
model = joblib.load("model.pkl") |
|
vectorizer = joblib.load("vectorizer.pkl") |
|
|
|
labels = {0: "Negative", 1: "Positive"} |
|
|
|
def classify(text): |
|
probs = model.predict_proba(vectorizer.transform([text]))[0] |
|
pred_idx = probs.argmax() |
|
confidence = float(probs[pred_idx]) |
|
label = labels[pred_idx] |
|
|
|
color = "#4CAF50" if pred_idx == 1 else "#F44336" |
|
styled_label = f"<span style='color:{color}; font-weight:bold'>{label}</span>" |
|
|
|
return styled_label, f"{confidence:.2%}" |
|
|
|
with gr.Blocks() as demo: |
|
gr.Markdown("## 🌱 Agricultural Text Classifier") |
|
gr.Markdown("Enter a description and see the prediction with confidence.") |
|
|
|
with gr.Row(): |
|
with gr.Column(): |
|
text_in = gr.Textbox(lines=3, placeholder="Type here...") |
|
btn = gr.Button("Classify") |
|
samples = gr.Row() |
|
for sample in [ |
|
"Healthy maize after seasonal rains", |
|
"Coffee plants showing signs of leaf rust", |
|
"Pest infestation on cassava leaves" |
|
]: |
|
gr.Button(sample).click(fn=lambda s=sample: s, outputs=text_in) |
|
|
|
with gr.Column(): |
|
label_out = gr.HTML() |
|
conf_out = gr.Textbox(label="Confidence", interactive=False) |
|
|
|
btn.click(classify, inputs=text_in, outputs=[label_out, conf_out]) |
|
|
|
demo.launch() |