PopeJohn commited on
Commit
66c37f8
·
verified ·
1 Parent(s): 84da306

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +36 -18
app.py CHANGED
@@ -1,25 +1,43 @@
1
  import gradio as gr
2
  import joblib
3
- from huggingface_hub import hf_hub_download
4
 
5
- # Load model & vectorizer from your Hugging Face repo
6
- model_path = hf_hub_download("PopeJohn/agriculture-model", "agriculture_model.pkl")
7
- vectorizer_path = hf_hub_download("PopeJohn/agriculture-model", "vectorizer.pkl")
8
 
9
- model = joblib.load(model_path)
10
- vectorizer = joblib.load(vectorizer_path)
11
 
12
- def classify_text(text):
13
- X_new = vectorizer.transform([text])
14
- label = model.predict(X_new)[0]
15
- return label
 
16
 
17
- iface = gr.Interface(
18
- fn=classify_text,
19
- inputs=gr.Textbox(lines=3, placeholder="Describe the crop situation..."),
20
- outputs="label",
21
- title="🌱 Agriculture Text Classifier",
22
- description="Type in a description of a crop or farming situation and get a Positive/Negative classification."
23
- )
24
 
25
- iface.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import gradio as gr
2
  import joblib
 
3
 
4
+ # Load your trained artifacts
5
+ model = joblib.load("model.pkl")
6
+ vectorizer = joblib.load("vectorizer.pkl")
7
 
8
+ labels = {0: "Negative", 1: "Positive"} # adjust to your classes
 
9
 
10
+ def classify(text):
11
+ probs = model.predict_proba(vectorizer.transform([text]))[0]
12
+ pred_idx = probs.argmax()
13
+ confidence = float(probs[pred_idx])
14
+ label = labels[pred_idx]
15
 
16
+ color = "#4CAF50" if pred_idx == 1 else "#F44336" # green/red
17
+ styled_label = f"<span style='color:{color}; font-weight:bold'>{label}</span>"
 
 
 
 
 
18
 
19
+ return styled_label, f"{confidence:.2%}"
20
+
21
+ with gr.Blocks() as demo:
22
+ gr.Markdown("## 🌱 Agricultural Text Classifier")
23
+ gr.Markdown("Enter a description and see the prediction with confidence.")
24
+
25
+ with gr.Row():
26
+ with gr.Column():
27
+ text_in = gr.Textbox(lines=3, placeholder="Type here...")
28
+ btn = gr.Button("Classify")
29
+ samples = gr.Row()
30
+ for sample in [
31
+ "Healthy maize after seasonal rains",
32
+ "Coffee plants showing signs of leaf rust",
33
+ "Pest infestation on cassava leaves"
34
+ ]:
35
+ gr.Button(sample).click(fn=lambda s=sample: s, outputs=text_in)
36
+
37
+ with gr.Column():
38
+ label_out = gr.HTML()
39
+ conf_out = gr.Textbox(label="Confidence", interactive=False)
40
+
41
+ btn.click(classify, inputs=text_in, outputs=[label_out, conf_out])
42
+
43
+ demo.launch()