tommy24 commited on
Commit
f3e1197
·
1 Parent(s): 5edd69e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +88 -46
app.py CHANGED
@@ -28,54 +28,95 @@ with open("labels.txt", "r") as file:
28
 
29
  def classify(Textbox, Image, Textbox2, Textbox3):
30
  if Textbox3 == code:
31
- output = []
32
- image_data = np.array(Image)
33
- image_data = cv.resize(image_data, (224, 224))
34
- image_array = np.asarray(image_data)
35
- normalized_image_array = (image_array.astype(np.float32) / 127.0) - 1
36
- data[0] = normalized_image_array
37
-
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
- Textbox2 = Textbox2.replace("[", "").replace("]", "").replace("'", "")
49
- Textbox2 = Textbox2.split(",")
50
- Textbox2_edited = [x.strip() for x in Textbox2]
51
- Textbox2_edited = list(Textbox2_edited)
52
- Textbox2_edited.append(Textbox)
53
- messages = [
54
- {"role": "system", "content": system},
55
- ]
56
- print("Messages",messages)
57
 
58
- for i in Textbox2_edited:
59
- messages.append(
60
- {"role": "user", "content": i}
61
- )
62
- print("messages after appending:", messages)
63
 
64
- for i, label in enumerate(labels):
65
- prediction_value = float(prediction[0][i])
66
- rounded_value = round(prediction_value, 2)
67
- print(f'{label}: {rounded_value}')
68
 
69
- if prediction_value > max_prediction_value:
70
- max_label_index = i
71
- max_prediction_value = prediction_value
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
72
 
73
- if max_label_index is not None:
74
- max_label = labels[max_label_index].split(' ', 1)[1]
75
- print(f'Maximum Prediction: {max_label} with a value of {round(max_prediction_value, 2)}')
 
 
 
 
 
 
 
 
 
76
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
77
  time.sleep(1)
78
- print("\nWays to dispose of this waste: " + max_label)
79
  messages.append({"role": "user", "content": content + " " + max_label})
80
  messages.append({"role": "user", "content": Textbox})
81
 
@@ -92,14 +133,15 @@ def classify(Textbox, Image, Textbox2, Textbox3):
92
  reply = response["choices"][0]["message"]["content"]
93
  messages.append({"role": "assistant", "content": reply})
94
 
95
- output.append({"type": max_label, "prediction_value": rounded_value, "content": reply})
96
-
97
- return output
 
98
  else:
99
  return "Unauthorized"
100
-
101
  user_inputs = [
102
- gr.Textbox(label="Textbox", type="text"),
103
  gr.Image(),
104
  gr.Textbox(label="Textbox2", type="text"),
105
  gr.Textbox(label="Textbox3", type="password")
 
28
 
29
  def classify(Textbox, Image, Textbox2, Textbox3):
30
  if Textbox3 == code:
31
+ if Image != None:
32
+ output = []
33
+ image_data = np.array(Image)
34
+ image_data = cv.resize(image_data, (224, 224))
35
+ image_array = np.asarray(image_data)
36
+ normalized_image_array = (image_array.astype(np.float32) / 127.0) - 1
37
+ data[0] = normalized_image_array
 
 
 
 
38
 
39
+ import tensorflow as tf
40
+ model = tf.keras.models.load_model('keras_model.h5')
 
 
 
 
 
 
 
 
 
 
 
 
41
 
42
+ prediction = model.predict(data)
43
+
44
+ max_label_index = None
45
+ max_prediction_value = -1
 
46
 
47
+ print('Prediction')
 
 
 
48
 
49
+ Textbox2 = Textbox2.replace("[", "").replace("]", "").replace("'", "")
50
+ Textbox2 = Textbox2.split(",")
51
+ Textbox2_edited = [x.strip() for x in Textbox2]
52
+ Textbox2_edited = list(Textbox2_edited)
53
+ Textbox2_edited.append(Textbox)
54
+ messages = [
55
+ {"role": "system", "content": system},
56
+ ]
57
+ print("Messages",messages)
58
+
59
+ # for i in Textbox2_edited:
60
+ # messages.append(
61
+ # {"role": "user", "content": i}
62
+ # )
63
+ print("messages after appending:", messages)
64
+
65
+ for i, label in enumerate(labels):
66
+ prediction_value = float(prediction[0][i])
67
+ rounded_value = round(prediction_value, 2)
68
+ print(f'{label}: {rounded_value}')
69
+
70
+ if prediction_value > max_prediction_value:
71
+ max_label_index = i
72
+ max_prediction_value = prediction_value
73
+
74
+ if max_label_index is not None:
75
+ max_label = labels[max_label_index].split(' ', 1)[1]
76
+ print(f'Maximum Prediction: {max_label} with a value of {round(max_prediction_value, 2)}')
77
+
78
+ time.sleep(1)
79
+ print("\nWays to dispose of this waste: " + max_label)
80
+ messages.append({"role": "user", "content": Textbox})
81
+ messages.append({"role": "user", "content": content + " " + max_label})
82
 
83
+ headers = {
84
+ "Content-Type": "application/json",
85
+ "Authorization": f"Bearer {auth}"
86
+ }
87
+
88
+ response = requests.post(host, headers=headers, json={
89
+ "messages":messages,
90
+ "model":model_llm
91
+ }).json()
92
+
93
+ reply = response["choices"][0]["message"]["content"]
94
+ messages.append({"role": "assistant", "content": reply})
95
 
96
+ output.append({"Mode":"Image", "type": max_label, "prediction_value": rounded_value, "content": reply})
97
+
98
+ return output
99
+
100
+ else:
101
+ output = []
102
+
103
+ Textbox2 = Textbox2.replace("[", "").replace("]", "").replace("'", "")
104
+ Textbox2 = Textbox2.split(",")
105
+ Textbox2_edited = [x.strip() for x in Textbox2]
106
+ Textbox2_edited = list(Textbox2_edited)
107
+ Textbox2_edited.append(Textbox)
108
+ messages = [
109
+ {"role": "system", "content": system},
110
+ ]
111
+ print("Messages",messages)
112
+
113
+ for i in Textbox2_edited:
114
+ messages.append(
115
+ {"role": "user", "content": i}
116
+ )
117
+ print("messages after appending:", messages)
118
+
119
  time.sleep(1)
 
120
  messages.append({"role": "user", "content": content + " " + max_label})
121
  messages.append({"role": "user", "content": Textbox})
122
 
 
133
  reply = response["choices"][0]["message"]["content"]
134
  messages.append({"role": "assistant", "content": reply})
135
 
136
+ output.append({"Mode":"Chat","content": reply})
137
+
138
+ return output
139
+
140
  else:
141
  return "Unauthorized"
142
+
143
  user_inputs = [
144
+ gr.Textbox(label="User Input", type="text"),
145
  gr.Image(),
146
  gr.Textbox(label="Textbox2", type="text"),
147
  gr.Textbox(label="Textbox3", type="password")