tommy24 commited on
Commit
b019779
·
1 Parent(s): 4dbb024

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +30 -12
app.py CHANGED
@@ -24,16 +24,15 @@ data = np.ndarray(shape=(1, 224, 224, 3), dtype=np.float32)
24
  with open("labels.txt", "r") as file:
25
  labels = file.read().splitlines()
26
 
27
- def classify(image_path, text_input):
28
- if text_input == code:
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
 
@@ -43,6 +42,20 @@ def classify(image_path, text_input):
43
  max_prediction_value = -1
44
 
45
  print('Prediction')
 
 
 
 
 
 
 
 
 
 
 
 
 
 
46
 
47
  for i, label in enumerate(labels):
48
  prediction_value = float(prediction[0][i])
@@ -51,7 +64,7 @@ def classify(image_path, text_input):
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]
@@ -59,10 +72,9 @@ def classify(image_path, text_input):
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,
@@ -74,17 +86,23 @@ def classify(image_path, text_input):
74
  }).json()
75
 
76
  reply = response["choices"][0]["message"]["content"]
 
77
 
78
  output.append({"type": max_label, "prediction_value": rounded_value, "content": reply})
79
 
80
- return output # Return the populated output list
81
  else:
82
  return "Unauthorized"
83
 
84
  iface = gr.Interface(
85
  fn=classify,
86
- inputs=[gr.inputs.Image(), "text"],
87
- outputs=gr.outputs.JSON(), # Output as JSON
 
 
 
 
 
88
  title="Waste Classifier",
89
  description="Upload an image to classify and get disposal instructions."
90
  )
 
24
  with open("labels.txt", "r") as file:
25
  labels = file.read().splitlines()
26
 
27
+ def classify(image_path, Textbox2, Textbox3):
28
+ if Textbox3 == code:
29
+ 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
  import tensorflow as tf
37
  model = tf.keras.models.load_model('keras_model.h5')
38
 
 
42
  max_prediction_value = -1
43
 
44
  print('Prediction')
45
+
46
+
47
+ Textbox2 = Textbox2.replace("[", "").replace("]", "").replace("'", "")
48
+ Textbox2 = Textbox2.split(",")
49
+ Textbox2_edited = [x.strip() for x in Textbox2]
50
+ Textbox2_edited = list(Textbox2_edited)
51
+ Textbox2_edited.append(Textbox)
52
+ messages = [
53
+ {"role": "system", "content": content},
54
+ ]
55
+ for i in Textbox2_edited:
56
+ messages.append(
57
+ {"role": "user", "content": i}
58
+ )
59
 
60
  for i, label in enumerate(labels):
61
  prediction_value = float(prediction[0][i])
 
64
 
65
  if prediction_value > max_prediction_value:
66
  max_label_index = i
67
+ max_prediction_value = prediction_value
68
 
69
  if max_label_index is not None:
70
  max_label = labels[max_label_index].split(' ', 1)[1]
 
72
 
73
  time.sleep(1)
74
  print("\nWays to dispose of this waste: " + max_label)
75
+ messages.append(
76
+ {"role": "user", "content": Textbox},
77
+ )
 
78
 
79
  response = requests.post(host, json={
80
  "messages": payload,
 
86
  }).json()
87
 
88
  reply = response["choices"][0]["message"]["content"]
89
+ messages.append({"role": "assistant", "content": reply})
90
 
91
  output.append({"type": max_label, "prediction_value": rounded_value, "content": reply})
92
 
93
+ return output
94
  else:
95
  return "Unauthorized"
96
 
97
  iface = gr.Interface(
98
  fn=classify,
99
+ inputs = [
100
+ gr.inputs.Image(),
101
+ gr.inputs.Textbox(label="Textbox2",type="text"),
102
+ gr.inputs.Textbox(label="Textbox3",type="password")
103
+ ]
104
+
105
+ outputs=gr.outputs.JSON(),
106
  title="Waste Classifier",
107
  description="Upload an image to classify and get disposal instructions."
108
  )