Devon12 commited on
Commit
d0f6f22
·
verified ·
1 Parent(s): cbdeae1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +27 -20
app.py CHANGED
@@ -77,26 +77,33 @@ class_to_tonne = {
77
  "Mülltüten_aus_Kunststoff": "Restmüll"
78
  }
79
 
80
- # Gradio Blocks UI
81
- with gr.Blocks(title="Abfallerkennung mit KI 🗑️") as demo:
82
- gr.Markdown("### Lade ein Bild hoch und erfahre, in welche Tonne der Abfall gehört.")
 
 
 
 
 
 
 
83
 
84
- image_input = gr.Image(type="pil", label="Bild hochladen")
85
- output_label = gr.Textbox(label="Vorhersage")
86
- output_tonne = gr.Textbox(label="Richtiger Abfallbehälter")
87
- button = gr.Button("Analysieren")
88
 
89
- def analyze(image):
90
- if image.mode != "RGB":
91
- image = image.convert("RGB")
92
- input_tensor = transform(image).unsqueeze(0).to(device)
93
- with torch.no_grad():
94
- outputs = model(input_tensor)
95
- _, predicted = torch.max(outputs, 1)
96
- label = class_labels[predicted.item()]
97
- tonne = class_to_tonne.get(label, "Unbekannt")
98
- return label.replace("_", " ").capitalize(), tonne
 
 
99
 
100
- button.click(fn=analyze, inputs=image_input, outputs=[output_label, output_tonne])
101
-
102
- demo.launch(share=True, show_api=False)
 
77
  "Mülltüten_aus_Kunststoff": "Restmüll"
78
  }
79
 
80
+ # Def Predict
81
+ def predict_image(image):
82
+ if image.mode != "RGB":
83
+ image = image.convert("RGB")
84
+ input_tensor = transform(image).unsqueeze(0).to(model.device)
85
+ with torch.no_grad():
86
+ outputs = model(input_tensor)
87
+ _, predicted = torch.max(outputs, 1)
88
+ label = class_labels[predicted.item()]
89
+ tonne = class_to_tonne.get(label, "Unbekannt")
90
 
91
+ return {
92
+ "Klasse": label.replace("_", " ").capitalize(),
93
+ "Gehört in die": tonne
94
+ }
95
 
96
+ # Gradio Interface
97
+ interface = gr.Interface(
98
+ fn=predict_image,
99
+ inputs=gr.Image(type="pil", label="Bild hochladen"),
100
+ outputs=[
101
+ gr.Textbox(label="Vorhersage (Klasse auf Deutsch)"),
102
+ gr.Textbox(label="Richtiger Abfallbehälter")
103
+ ],
104
+ title="Abfallerkennung mit KI 🗑️",
105
+ description="Lade ein Bild hoch und erfahre, in welche Tonne der Abfall gehört.",
106
+ live=True
107
+ )
108
 
109
+ interface.launch()