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