tommy24 commited on
Commit
381e9cd
·
1 Parent(s): e198e28

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +44 -45
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
- 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}"
@@ -86,7 +85,7 @@ def classify(image_path):
86
  iface = gr.Interface(
87
  fn=classify,
88
  inputs=gr.inputs.Image(),
89
- outputs="text",
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
  )