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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 154 |
import gradio as gr
|
| 155 |
import numpy as np
|
| 156 |
import cv2 as cv
|
| 157 |
import requests
|
| 158 |
-
import time
|
| 159 |
import os
|
|
|
|
| 160 |
|
| 161 |
host = os.environ.get("host")
|
| 162 |
code = os.environ.get("code")
|
|
@@ -166,17 +330,13 @@ state = os.environ.get("state")
|
|
| 166 |
system = os.environ.get("system")
|
| 167 |
auth = os.environ.get("auth")
|
| 168 |
auth2 = os.environ.get("auth2")
|
| 169 |
-
data = None
|
| 170 |
-
model = None
|
| 171 |
-
image = None
|
| 172 |
-
prediction = None
|
| 173 |
-
labels = None
|
| 174 |
|
| 175 |
-
print('START')
|
| 176 |
np.set_printoptions(suppress=True)
|
| 177 |
|
| 178 |
-
|
|
|
|
| 179 |
|
|
|
|
| 180 |
with open("labels.txt", "r") as file:
|
| 181 |
labels = file.read().splitlines()
|
| 182 |
|
|
@@ -184,7 +344,7 @@ messages = [
|
|
| 184 |
{"role": "system", "content": system}
|
| 185 |
]
|
| 186 |
|
| 187 |
-
def classify(platform,UserInput, Image, Textbox2, Textbox3):
|
| 188 |
if Textbox3 == code:
|
| 189 |
if Image is not None:
|
| 190 |
output = []
|
|
@@ -193,108 +353,100 @@ def classify(platform,UserInput, Image, Textbox2, Textbox3):
|
|
| 193 |
}
|
| 194 |
if platform == "wh":
|
| 195 |
get_image = requests.get(Image, headers=headers)
|
| 196 |
-
|
| 197 |
elif platform == "web":
|
| 198 |
print("WEB")
|
|
|
|
| 199 |
else:
|
| 200 |
pass
|
| 201 |
-
|
| 202 |
image_data = cv.resize(image_data, (224, 224))
|
| 203 |
-
|
| 204 |
-
normalized_image_array = (image_array.astype(np.float32) / 127.0) - 1
|
| 205 |
data[0] = normalized_image_array
|
| 206 |
-
|
| 207 |
-
import tensorflow as tf
|
| 208 |
-
model = tf.keras.models.load_model('keras_model.h5')
|
| 209 |
-
|
| 210 |
prediction = model.predict(data)
|
| 211 |
-
|
| 212 |
max_label_index = None
|
| 213 |
max_prediction_value = -1
|
| 214 |
-
|
| 215 |
print('Prediction')
|
| 216 |
-
|
| 217 |
Textbox2 = Textbox2.replace("[", "").replace("]", "").replace("'", "")
|
| 218 |
Textbox2 = Textbox2.split(",")
|
| 219 |
Textbox2_edited = [x.strip() for x in Textbox2]
|
| 220 |
Textbox2_edited = list(Textbox2_edited)
|
| 221 |
Textbox2_edited.append(UserInput)
|
| 222 |
messages.append({"role": "user", "content": UserInput})
|
| 223 |
-
|
| 224 |
for i, label in enumerate(labels):
|
| 225 |
prediction_value = float(prediction[0][i])
|
| 226 |
rounded_value = round(prediction_value, 2)
|
| 227 |
print(f'{label}: {rounded_value}')
|
| 228 |
-
|
| 229 |
if prediction_value > max_prediction_value:
|
| 230 |
max_label_index = i
|
| 231 |
-
max_prediction_value = prediction_value
|
| 232 |
-
|
| 233 |
if max_label_index is not None:
|
| 234 |
max_label = labels[max_label_index].split(' ', 1)[1]
|
| 235 |
max_rounded_prediction = round(max_prediction_value, 2)
|
| 236 |
print(f'Maximum Prediction: {max_label} with a value of {max_rounded_prediction}')
|
| 237 |
-
|
| 238 |
-
time.sleep(1)
|
| 239 |
if max_rounded_prediction > 0.5:
|
| 240 |
print("\nWays to dispose of this waste: " + max_label)
|
| 241 |
messages.append({"role": "user", "content": content + " " + max_label})
|
| 242 |
-
|
| 243 |
headers = {
|
| 244 |
"Content-Type": "application/json",
|
| 245 |
"Authorization": f"Bearer {auth}"
|
| 246 |
}
|
| 247 |
-
|
| 248 |
response = requests.post(host, headers=headers, json={
|
| 249 |
"messages": messages,
|
| 250 |
"model": model_llm
|
| 251 |
}).json()
|
| 252 |
-
|
| 253 |
reply = response["choices"][0]["message"]["content"]
|
| 254 |
messages.append({"role": "assistant", "content": reply})
|
| 255 |
-
|
| 256 |
output.append({"Mode": "Image", "type": max_label, "prediction_value": max_rounded_prediction, "content": reply})
|
| 257 |
elif max_rounded_prediction < 0.5:
|
| 258 |
-
output.append({"Mode": "Image", "type": "Not predictable", "prediction_value": max_rounded_prediction, "content": "Seems like the prediction rate is too low due to that won't be able to predict the type of material. Try again with a cropped image or different one
|
| 259 |
-
|
| 260 |
return output
|
| 261 |
|
| 262 |
else:
|
| 263 |
output = []
|
| 264 |
-
|
| 265 |
Textbox2 = Textbox2.replace("[", "").replace("]", "").replace("'", "")
|
| 266 |
Textbox2 = Textbox2.split(",")
|
| 267 |
Textbox2_edited = [x.strip() for x in Textbox2]
|
| 268 |
Textbox2_edited = list(Textbox2_edited)
|
| 269 |
Textbox2_edited.append(UserInput)
|
| 270 |
-
|
| 271 |
for i in Textbox2_edited:
|
| 272 |
-
messages.append(
|
| 273 |
-
|
| 274 |
-
)
|
| 275 |
-
|
| 276 |
print("messages after appending:", messages)
|
| 277 |
-
|
| 278 |
-
time.sleep(1)
|
| 279 |
messages.append({"role": "user", "content": UserInput})
|
| 280 |
|
| 281 |
headers = {
|
| 282 |
"Content-Type": "application/json",
|
| 283 |
"Authorization": f"Bearer {auth}"
|
| 284 |
}
|
| 285 |
-
|
| 286 |
response = requests.post(host, headers=headers, json={
|
| 287 |
"messages": messages,
|
| 288 |
"model": model_llm
|
| 289 |
}).json()
|
| 290 |
-
|
| 291 |
reply = response["choices"][0]["message"]["content"]
|
| 292 |
messages.append({"role": "assistant", "content": reply})
|
| 293 |
|
| 294 |
output.append({"Mode": "Chat", "content": reply})
|
| 295 |
-
|
| 296 |
-
return output
|
| 297 |
|
|
|
|
| 298 |
else:
|
| 299 |
return "Unauthorized"
|
| 300 |
|
|
|
|
| 151 |
# iface.launch()
|
| 152 |
|
| 153 |
|
| 154 |
+
# import gradio as gr
|
| 155 |
+
# import numpy as np
|
| 156 |
+
# import cv2 as cv
|
| 157 |
+
# import requests
|
| 158 |
+
# import time
|
| 159 |
+
# import os
|
| 160 |
+
|
| 161 |
+
# host = os.environ.get("host")
|
| 162 |
+
# code = os.environ.get("code")
|
| 163 |
+
# model_llm = os.environ.get("model")
|
| 164 |
+
# content = os.environ.get("content")
|
| 165 |
+
# state = os.environ.get("state")
|
| 166 |
+
# system = os.environ.get("system")
|
| 167 |
+
# auth = os.environ.get("auth")
|
| 168 |
+
# auth2 = os.environ.get("auth2")
|
| 169 |
+
# data = None
|
| 170 |
+
# model = None
|
| 171 |
+
# image = None
|
| 172 |
+
# prediction = None
|
| 173 |
+
# labels = None
|
| 174 |
+
|
| 175 |
+
# print('START')
|
| 176 |
+
# np.set_printoptions(suppress=True)
|
| 177 |
+
|
| 178 |
+
# data = np.ndarray(shape=(1, 224, 224, 3), dtype=np.float32)
|
| 179 |
+
|
| 180 |
+
# with open("labels.txt", "r") as file:
|
| 181 |
+
# labels = file.read().splitlines()
|
| 182 |
+
|
| 183 |
+
# messages = [
|
| 184 |
+
# {"role": "system", "content": system}
|
| 185 |
+
# ]
|
| 186 |
+
|
| 187 |
+
# def classify(platform,UserInput, Image, Textbox2, Textbox3):
|
| 188 |
+
# if Textbox3 == code:
|
| 189 |
+
# if Image is not None:
|
| 190 |
+
# output = []
|
| 191 |
+
# headers = {
|
| 192 |
+
# "Authorization": f"Bearer {auth2}"
|
| 193 |
+
# }
|
| 194 |
+
# if platform == "wh":
|
| 195 |
+
# get_image = requests.get(Image, headers=headers)
|
| 196 |
+
# print(get_image.content)
|
| 197 |
+
# elif platform == "web":
|
| 198 |
+
# print("WEB")
|
| 199 |
+
# else:
|
| 200 |
+
# pass
|
| 201 |
+
# image_data = np.array(get_image)
|
| 202 |
+
# image_data = cv.resize(image_data, (224, 224))
|
| 203 |
+
# image_array = np.asarray(image_data)
|
| 204 |
+
# normalized_image_array = (image_array.astype(np.float32) / 127.0) - 1
|
| 205 |
+
# data[0] = normalized_image_array
|
| 206 |
+
|
| 207 |
+
# import tensorflow as tf
|
| 208 |
+
# model = tf.keras.models.load_model('keras_model.h5')
|
| 209 |
+
|
| 210 |
+
# prediction = model.predict(data)
|
| 211 |
+
|
| 212 |
+
# max_label_index = None
|
| 213 |
+
# max_prediction_value = -1
|
| 214 |
+
|
| 215 |
+
# print('Prediction')
|
| 216 |
+
|
| 217 |
+
# Textbox2 = Textbox2.replace("[", "").replace("]", "").replace("'", "")
|
| 218 |
+
# Textbox2 = Textbox2.split(",")
|
| 219 |
+
# Textbox2_edited = [x.strip() for x in Textbox2]
|
| 220 |
+
# Textbox2_edited = list(Textbox2_edited)
|
| 221 |
+
# Textbox2_edited.append(UserInput)
|
| 222 |
+
# messages.append({"role": "user", "content": UserInput})
|
| 223 |
+
|
| 224 |
+
# for i, label in enumerate(labels):
|
| 225 |
+
# prediction_value = float(prediction[0][i])
|
| 226 |
+
# rounded_value = round(prediction_value, 2)
|
| 227 |
+
# print(f'{label}: {rounded_value}')
|
| 228 |
+
|
| 229 |
+
# if prediction_value > max_prediction_value:
|
| 230 |
+
# max_label_index = i
|
| 231 |
+
# max_prediction_value = prediction_value
|
| 232 |
+
|
| 233 |
+
# if max_label_index is not None:
|
| 234 |
+
# max_label = labels[max_label_index].split(' ', 1)[1]
|
| 235 |
+
# max_rounded_prediction = round(max_prediction_value, 2)
|
| 236 |
+
# print(f'Maximum Prediction: {max_label} with a value of {max_rounded_prediction}')
|
| 237 |
+
|
| 238 |
+
# time.sleep(1)
|
| 239 |
+
# if max_rounded_prediction > 0.5:
|
| 240 |
+
# print("\nWays to dispose of this waste: " + max_label)
|
| 241 |
+
# messages.append({"role": "user", "content": content + " " + max_label})
|
| 242 |
+
|
| 243 |
+
# headers = {
|
| 244 |
+
# "Content-Type": "application/json",
|
| 245 |
+
# "Authorization": f"Bearer {auth}"
|
| 246 |
+
# }
|
| 247 |
+
|
| 248 |
+
# response = requests.post(host, headers=headers, json={
|
| 249 |
+
# "messages": messages,
|
| 250 |
+
# "model": model_llm
|
| 251 |
+
# }).json()
|
| 252 |
+
|
| 253 |
+
# reply = response["choices"][0]["message"]["content"]
|
| 254 |
+
# messages.append({"role": "assistant", "content": reply})
|
| 255 |
+
|
| 256 |
+
# output.append({"Mode": "Image", "type": max_label, "prediction_value": max_rounded_prediction, "content": reply})
|
| 257 |
+
# elif max_rounded_prediction < 0.5:
|
| 258 |
+
# output.append({"Mode": "Image", "type": "Not predictable", "prediction_value": max_rounded_prediction, "content": "Seems like the prediction rate is too low due to that won't be able to predict the type of material. Try again with a cropped image or different one."})
|
| 259 |
+
|
| 260 |
+
# return output
|
| 261 |
+
|
| 262 |
+
# else:
|
| 263 |
+
# output = []
|
| 264 |
+
|
| 265 |
+
# Textbox2 = Textbox2.replace("[", "").replace("]", "").replace("'", "")
|
| 266 |
+
# Textbox2 = Textbox2.split(",")
|
| 267 |
+
# Textbox2_edited = [x.strip() for x in Textbox2]
|
| 268 |
+
# Textbox2_edited = list(Textbox2_edited)
|
| 269 |
+
# Textbox2_edited.append(UserInput)
|
| 270 |
+
|
| 271 |
+
# for i in Textbox2_edited:
|
| 272 |
+
# messages.append(
|
| 273 |
+
# {"role": "user", "content": i}
|
| 274 |
+
# )
|
| 275 |
+
|
| 276 |
+
# print("messages after appending:", messages)
|
| 277 |
+
|
| 278 |
+
# time.sleep(1)
|
| 279 |
+
# messages.append({"role": "user", "content": UserInput})
|
| 280 |
+
|
| 281 |
+
# headers = {
|
| 282 |
+
# "Content-Type": "application/json",
|
| 283 |
+
# "Authorization": f"Bearer {auth}"
|
| 284 |
+
# }
|
| 285 |
+
|
| 286 |
+
# response = requests.post(host, headers=headers, json={
|
| 287 |
+
# "messages": messages,
|
| 288 |
+
# "model": model_llm
|
| 289 |
+
# }).json()
|
| 290 |
+
|
| 291 |
+
# reply = response["choices"][0]["message"]["content"]
|
| 292 |
+
# messages.append({"role": "assistant", "content": reply})
|
| 293 |
+
|
| 294 |
+
# output.append({"Mode": "Chat", "content": reply})
|
| 295 |
+
|
| 296 |
+
# return output
|
| 297 |
+
|
| 298 |
+
# else:
|
| 299 |
+
# return "Unauthorized"
|
| 300 |
+
|
| 301 |
+
# user_inputs = [
|
| 302 |
+
# gr.Textbox(label="Platform", type="text"),
|
| 303 |
+
# gr.Textbox(label="User Input", type="text"),
|
| 304 |
+
# gr.Textbox(label="Image", type="text"),
|
| 305 |
+
# gr.Textbox(label="Textbox2", type="text"),
|
| 306 |
+
# gr.Textbox(label="Textbox3", type="password")
|
| 307 |
+
# ]
|
| 308 |
+
|
| 309 |
+
# iface = gr.Interface(
|
| 310 |
+
# fn=classify,
|
| 311 |
+
# inputs=user_inputs,
|
| 312 |
+
# outputs=gr.outputs.JSON(),
|
| 313 |
+
# title="Classifier",
|
| 314 |
+
# )
|
| 315 |
+
# iface.launch()
|
| 316 |
+
|
| 317 |
+
|
| 318 |
import gradio as gr
|
| 319 |
import numpy as np
|
| 320 |
import cv2 as cv
|
| 321 |
import requests
|
|
|
|
| 322 |
import os
|
| 323 |
+
import tensorflow as tf
|
| 324 |
|
| 325 |
host = os.environ.get("host")
|
| 326 |
code = os.environ.get("code")
|
|
|
|
| 330 |
system = os.environ.get("system")
|
| 331 |
auth = os.environ.get("auth")
|
| 332 |
auth2 = os.environ.get("auth2")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 333 |
|
|
|
|
| 334 |
np.set_printoptions(suppress=True)
|
| 335 |
|
| 336 |
+
# Load the model outside of the function
|
| 337 |
+
model = tf.keras.models.load_model('keras_model.h5')
|
| 338 |
|
| 339 |
+
# Load labels from a file
|
| 340 |
with open("labels.txt", "r") as file:
|
| 341 |
labels = file.read().splitlines()
|
| 342 |
|
|
|
|
| 344 |
{"role": "system", "content": system}
|
| 345 |
]
|
| 346 |
|
| 347 |
+
def classify(platform, UserInput, Image, Textbox2, Textbox3):
|
| 348 |
if Textbox3 == code:
|
| 349 |
if Image is not None:
|
| 350 |
output = []
|
|
|
|
| 353 |
}
|
| 354 |
if platform == "wh":
|
| 355 |
get_image = requests.get(Image, headers=headers)
|
| 356 |
+
image_data = cv.imdecode(np.asarray(bytearray(get_image.content), dtype="uint8"), cv.IMREAD_COLOR)
|
| 357 |
elif platform == "web":
|
| 358 |
print("WEB")
|
| 359 |
+
# Handle web case if needed
|
| 360 |
else:
|
| 361 |
pass
|
| 362 |
+
|
| 363 |
image_data = cv.resize(image_data, (224, 224))
|
| 364 |
+
normalized_image_array = (image_data.astype(np.float32) / 127.0) - 1
|
|
|
|
| 365 |
data[0] = normalized_image_array
|
| 366 |
+
|
|
|
|
|
|
|
|
|
|
| 367 |
prediction = model.predict(data)
|
| 368 |
+
|
| 369 |
max_label_index = None
|
| 370 |
max_prediction_value = -1
|
| 371 |
+
|
| 372 |
print('Prediction')
|
| 373 |
+
|
| 374 |
Textbox2 = Textbox2.replace("[", "").replace("]", "").replace("'", "")
|
| 375 |
Textbox2 = Textbox2.split(",")
|
| 376 |
Textbox2_edited = [x.strip() for x in Textbox2]
|
| 377 |
Textbox2_edited = list(Textbox2_edited)
|
| 378 |
Textbox2_edited.append(UserInput)
|
| 379 |
messages.append({"role": "user", "content": UserInput})
|
| 380 |
+
|
| 381 |
for i, label in enumerate(labels):
|
| 382 |
prediction_value = float(prediction[0][i])
|
| 383 |
rounded_value = round(prediction_value, 2)
|
| 384 |
print(f'{label}: {rounded_value}')
|
| 385 |
+
|
| 386 |
if prediction_value > max_prediction_value:
|
| 387 |
max_label_index = i
|
| 388 |
+
max_prediction_value = prediction_value
|
| 389 |
+
|
| 390 |
if max_label_index is not None:
|
| 391 |
max_label = labels[max_label_index].split(' ', 1)[1]
|
| 392 |
max_rounded_prediction = round(max_prediction_value, 2)
|
| 393 |
print(f'Maximum Prediction: {max_label} with a value of {max_rounded_prediction}')
|
| 394 |
+
|
|
|
|
| 395 |
if max_rounded_prediction > 0.5:
|
| 396 |
print("\nWays to dispose of this waste: " + max_label)
|
| 397 |
messages.append({"role": "user", "content": content + " " + max_label})
|
| 398 |
+
|
| 399 |
headers = {
|
| 400 |
"Content-Type": "application/json",
|
| 401 |
"Authorization": f"Bearer {auth}"
|
| 402 |
}
|
| 403 |
+
|
| 404 |
response = requests.post(host, headers=headers, json={
|
| 405 |
"messages": messages,
|
| 406 |
"model": model_llm
|
| 407 |
}).json()
|
| 408 |
+
|
| 409 |
reply = response["choices"][0]["message"]["content"]
|
| 410 |
messages.append({"role": "assistant", "content": reply})
|
| 411 |
+
|
| 412 |
output.append({"Mode": "Image", "type": max_label, "prediction_value": max_rounded_prediction, "content": reply})
|
| 413 |
elif max_rounded_prediction < 0.5:
|
| 414 |
+
output.append({"Mode": "Image", "type": "Not predictable", "prediction_value": max_rounded_prediction, "content": "Seems like the prediction rate is too low due to that won't be able to predict the type of material. Try again with a cropped image or different one"})
|
| 415 |
+
|
| 416 |
return output
|
| 417 |
|
| 418 |
else:
|
| 419 |
output = []
|
| 420 |
+
|
| 421 |
Textbox2 = Textbox2.replace("[", "").replace("]", "").replace("'", "")
|
| 422 |
Textbox2 = Textbox2.split(",")
|
| 423 |
Textbox2_edited = [x.strip() for x in Textbox2]
|
| 424 |
Textbox2_edited = list(Textbox2_edited)
|
| 425 |
Textbox2_edited.append(UserInput)
|
| 426 |
+
|
| 427 |
for i in Textbox2_edited:
|
| 428 |
+
messages.append({"role": "user", "content": i})
|
| 429 |
+
|
|
|
|
|
|
|
| 430 |
print("messages after appending:", messages)
|
| 431 |
+
|
|
|
|
| 432 |
messages.append({"role": "user", "content": UserInput})
|
| 433 |
|
| 434 |
headers = {
|
| 435 |
"Content-Type": "application/json",
|
| 436 |
"Authorization": f"Bearer {auth}"
|
| 437 |
}
|
| 438 |
+
|
| 439 |
response = requests.post(host, headers=headers, json={
|
| 440 |
"messages": messages,
|
| 441 |
"model": model_llm
|
| 442 |
}).json()
|
| 443 |
+
|
| 444 |
reply = response["choices"][0]["message"]["content"]
|
| 445 |
messages.append({"role": "assistant", "content": reply})
|
| 446 |
|
| 447 |
output.append({"Mode": "Chat", "content": reply})
|
|
|
|
|
|
|
| 448 |
|
| 449 |
+
return output
|
| 450 |
else:
|
| 451 |
return "Unauthorized"
|
| 452 |
|