Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -26,59 +26,58 @@ with open("labels.txt", "r") as file:
|
|
26 |
|
27 |
def classify(image_path):
|
28 |
try:
|
29 |
-
output = []
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
data[0] = normalized_image_array
|
36 |
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
|
77 |
-
|
78 |
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
|
83 |
except Exception as e:
|
84 |
return f"An error occurred: {e}"
|
@@ -86,7 +85,7 @@ def classify(image_path):
|
|
86 |
iface = gr.Interface(
|
87 |
fn=classify,
|
88 |
inputs=gr.inputs.Image(),
|
89 |
-
outputs=
|
90 |
title="Waste Classifier",
|
91 |
description="Upload an image to classify and get disposal instructions."
|
92 |
)
|
|
|
26 |
|
27 |
def classify(image_path):
|
28 |
try:
|
29 |
+
output = [] # Create an empty list for output
|
30 |
+
image_data = np.array(image_path)
|
31 |
+
image_data = cv.resize(image_data, (224, 224))
|
32 |
+
image_array = np.asarray(image_data)
|
33 |
+
normalized_image_array = (image_array.astype(np.float32) / 127.0) - 1
|
34 |
+
data[0] = normalized_image_array
|
|
|
35 |
|
36 |
+
# Load the model within the classify function
|
37 |
+
import tensorflow as tf
|
38 |
+
model = tf.keras.models.load_model('keras_model.h5')
|
39 |
|
40 |
+
prediction = model.predict(data)
|
41 |
+
|
42 |
+
max_label_index = None
|
43 |
+
max_prediction_value = -1
|
44 |
+
|
45 |
+
print('Prediction')
|
46 |
|
47 |
+
for i, label in enumerate(labels):
|
48 |
+
prediction_value = float(prediction[0][i])
|
49 |
+
rounded_value = round(prediction_value, 2)
|
50 |
+
print(f'{label}: {rounded_value}')
|
51 |
|
52 |
+
if prediction_value > max_prediction_value:
|
53 |
+
max_label_index = i
|
54 |
+
max_prediction_value = prediction_value # Update max_prediction_value
|
55 |
|
56 |
+
if max_label_index is not None:
|
57 |
+
max_label = labels[max_label_index].split(' ', 1)[1]
|
58 |
+
print(f'Maximum Prediction: {max_label} with a value of {round(max_prediction_value, 2)}')
|
59 |
|
60 |
+
time.sleep(1)
|
61 |
+
print("\nWays to dispose of this waste: " + max_label)
|
62 |
+
payload = [
|
63 |
+
{"role": "system", "content": "You are a helpful assistant."},
|
64 |
+
{"role": "user", "content": "Give me the steps to dispose of this waste in bullet points (5 max): " + max_label}
|
65 |
+
]
|
66 |
|
67 |
+
response = requests.post(host, json={
|
68 |
+
"messages": payload,
|
69 |
+
"model": model_llm,
|
70 |
+
"temperature": 0.5,
|
71 |
+
"presence_penalty": 0,
|
72 |
+
"frequency_penalty": 0,
|
73 |
+
"top_p": 1
|
74 |
+
}).json()
|
75 |
|
76 |
+
reply = response["choices"][0]["message"]["content"]
|
77 |
|
78 |
+
output.append({"type": max_label, "prediction_value": round(max_prediction_value, 2), "content": reply})
|
79 |
+
|
80 |
+
return output # Return the populated output list
|
81 |
|
82 |
except Exception as e:
|
83 |
return f"An error occurred: {e}"
|
|
|
85 |
iface = gr.Interface(
|
86 |
fn=classify,
|
87 |
inputs=gr.inputs.Image(),
|
88 |
+
outputs=gr.outputs.JSON(), # Output as JSON
|
89 |
title="Waste Classifier",
|
90 |
description="Upload an image to classify and get disposal instructions."
|
91 |
)
|