Yasiru Chamuditha commited on
Commit
dda996b
·
1 Parent(s): ec6f922

fix issues

Browse files
Files changed (1) hide show
  1. app.py +20 -6
app.py CHANGED
@@ -47,11 +47,25 @@ def predict_image(image: Image.Image):
47
  pred_class = class_names[pred_class_idx]
48
  confidence = float(preds[pred_class_idx])
49
 
50
- # Format per-class probabilities
51
- prob_text = "\n".join([f"{class_names[i]}: {float(preds[i]):.2f}" for i in range(len(class_names))])
 
 
 
 
 
 
 
52
 
53
- # Return formatted output with titles
54
- output = f"### Prediction Class: {pred_class}\n### Confidence: {confidence:.2f}\n### Per-class probabilities\n{prob_text}"
 
 
 
 
 
 
 
55
  return output
56
  except Exception as e:
57
  logger.error(f"Error during prediction: {str(e)}")
@@ -59,11 +73,11 @@ def predict_image(image: Image.Image):
59
 
60
  # Create Gradio interface with custom CSS
61
  logger.info("Initializing Gradio interface")
62
- with gr.Blocks(css=".large-output { height: 200px !important; overflow: visible !important; }") as iface:
63
  gr.Markdown("# ♻️ Garbage Classification Application")
64
  gr.Markdown("Upload an image of garbage (plastic, organic, or metal):")
65
  img_input = gr.Image(type="pil")
66
- output = gr.Markdown(label="Prediction Result", elem_classes="large-output")
67
  gr.Button("Classify").click(
68
  fn=predict_image,
69
  inputs=img_input,
 
47
  pred_class = class_names[pred_class_idx]
48
  confidence = float(preds[pred_class_idx])
49
 
50
+ # Create HTML for per-class probabilities with progress bars
51
+ prob_html = ""
52
+ for i in range(len(class_names)):
53
+ class_name = class_names[i]
54
+ prob = float(preds[i])
55
+ prob_html += f'<div style="display: flex; align-items: center; margin-bottom: 10px;">'
56
+ prob_html += f'<span style="width: 100px; font-weight: bold;">{class_name}: {prob:.2f}</span>'
57
+ prob_html += f'<progress value="{prob}" max="1" style="width: 200px; margin-left: 10px; height: 20px;"></progress>'
58
+ prob_html += f'</div>'
59
 
60
+ # Return formatted HTML output with titles
61
+ output = f"""
62
+ <div style="font-family: Arial, sans-serif;">
63
+ <h3>Prediction Class: {pred_class}</h3>
64
+ <h3>Confidence: {confidence:.2f}</h3>
65
+ <h3>Per-class probabilities</h3>
66
+ {prob_html}
67
+ </div>
68
+ """
69
  return output
70
  except Exception as e:
71
  logger.error(f"Error during prediction: {str(e)}")
 
73
 
74
  # Create Gradio interface with custom CSS
75
  logger.info("Initializing Gradio interface")
76
+ with gr.Blocks(css=".large-output { height: 400px !important; overflow: visible !important; }") as iface:
77
  gr.Markdown("# ♻️ Garbage Classification Application")
78
  gr.Markdown("Upload an image of garbage (plastic, organic, or metal):")
79
  img_input = gr.Image(type="pil")
80
+ output = gr.HTML(label="Prediction Result", elem_classes="large-output")
81
  gr.Button("Classify").click(
82
  fn=predict_image,
83
  inputs=img_input,