tommy24 commited on
Commit
7c817a1
·
1 Parent(s): 5369911

Update app.py

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