Devon12 commited on
Commit
473d641
·
verified ·
1 Parent(s): d4ff15f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +21 -14
app.py CHANGED
@@ -93,17 +93,24 @@ def predict_image(image):
93
  return label.replace("_", " ").capitalize(), tonne
94
 
95
  # Gradio interface setup
96
- interface = gr.Interface(
97
- fn=predict_image,
98
- inputs=gr.Image(type="pil", label="Bild hochladen"),
99
- outputs=[
100
- gr.Textbox(label="Vorhersage"),
101
- gr.Textbox(label="Richtiger Abfallbehälter")
102
- ],
103
- title="Abfallerkennung mit KI 🗑️",
104
- description="Lade ein Bild hoch und erfahre, in welche Tonne der Abfall gehört.",
105
- live=True
106
- )
107
-
108
- interface.launch()
109
-
 
 
 
 
 
 
 
 
93
  return label.replace("_", " ").capitalize(), tonne
94
 
95
  # Gradio interface setup
96
+ with gr.Blocks(title="Abfallerkennung mit KI 🗑️") as demo:
97
+ gr.Markdown("### Lade ein Bild hoch und erfahre, in welche Tonne der Abfall gehört.")
98
+ image_input = gr.Image(type="pil", label="Bild hochladen")
99
+ output_label = gr.Textbox(label="Vorhersage")
100
+ output_tonne = gr.Textbox(label="Richtiger Abfallbehälter")
101
+ button = gr.Button("Analysieren")
102
+
103
+ def analyze(image):
104
+ if image.mode != "RGB":
105
+ image = image.convert("RGB")
106
+ input_tensor = transform(image).unsqueeze(0).to(model.device)
107
+ with torch.no_grad():
108
+ outputs = model(input_tensor)
109
+ _, predicted = torch.max(outputs, 1)
110
+ label = class_labels[predicted.item()]
111
+ tonne = class_to_tonne.get(label, "Unbekannt")
112
+ return label.replace("_", " ").capitalize(), tonne
113
+
114
+ button.click(fn=analyze, inputs=image_input, outputs=[output_label, output_tonne])
115
+
116
+ demo.launch()