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|
@@ -12,6 +165,7 @@
|
|
| 12 |
# state = os.environ.get("state")
|
| 13 |
# system = os.environ.get("system")
|
| 14 |
# auth = os.environ.get("auth")
|
|
|
|
| 15 |
# data = None
|
| 16 |
# model = None
|
| 17 |
# image = None
|
|
@@ -30,12 +184,21 @@
|
|
| 30 |
# {"role": "system", "content": system}
|
| 31 |
# ]
|
| 32 |
|
| 33 |
-
# def classify(UserInput, Image, Textbox2, Textbox3):
|
| 34 |
# if Textbox3 == code:
|
| 35 |
-
# print("Image: ", Image)
|
| 36 |
# if Image is not None:
|
| 37 |
# output = []
|
| 38 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
# image_data = cv.resize(image_data, (224, 224))
|
| 40 |
# image_array = np.asarray(image_data)
|
| 41 |
# normalized_image_array = (image_array.astype(np.float32) / 127.0) - 1
|
|
@@ -136,8 +299,9 @@
|
|
| 136 |
# return "Unauthorized"
|
| 137 |
|
| 138 |
# user_inputs = [
|
|
|
|
| 139 |
# gr.Textbox(label="User Input", type="text"),
|
| 140 |
-
# gr.Image
|
| 141 |
# gr.Textbox(label="Textbox2", type="text"),
|
| 142 |
# gr.Textbox(label="Textbox3", type="password")
|
| 143 |
# ]
|
|
@@ -155,8 +319,11 @@
|
|
| 155 |
# import numpy as np
|
| 156 |
# import cv2 as cv
|
| 157 |
# import requests
|
| 158 |
-
# import
|
|
|
|
| 159 |
# import os
|
|
|
|
|
|
|
| 160 |
|
| 161 |
# host = os.environ.get("host")
|
| 162 |
# code = os.environ.get("code")
|
|
@@ -167,14 +334,10 @@
|
|
| 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:
|
|
@@ -184,117 +347,115 @@
|
|
| 184 |
# {"role": "system", "content": system}
|
| 185 |
# ]
|
| 186 |
|
| 187 |
-
# def classify(platform,UserInput,
|
| 188 |
# if Textbox3 == code:
|
| 189 |
-
#
|
|
|
|
| 190 |
# output = []
|
| 191 |
# headers = {
|
| 192 |
# "Authorization": f"Bearer {auth2}"
|
| 193 |
# }
|
| 194 |
# if platform == "wh":
|
| 195 |
-
# get_image = requests.get(
|
| 196 |
-
#
|
|
|
|
| 197 |
# elif platform == "web":
|
| 198 |
# print("WEB")
|
| 199 |
# else:
|
| 200 |
# pass
|
| 201 |
-
|
| 202 |
-
#
|
| 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 |
|
|
@@ -312,168 +473,7 @@
|
|
| 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 io
|
| 323 |
-
from PIL import Image
|
| 324 |
-
import os
|
| 325 |
-
import tensorflow as tf
|
| 326 |
-
import random
|
| 327 |
-
|
| 328 |
-
host = os.environ.get("host")
|
| 329 |
-
code = os.environ.get("code")
|
| 330 |
-
model_llm = os.environ.get("model")
|
| 331 |
-
content = os.environ.get("content")
|
| 332 |
-
state = os.environ.get("state")
|
| 333 |
-
system = os.environ.get("system")
|
| 334 |
-
auth = os.environ.get("auth")
|
| 335 |
-
auth2 = os.environ.get("auth2")
|
| 336 |
-
data = None
|
| 337 |
-
|
| 338 |
-
np.set_printoptions(suppress=True)
|
| 339 |
-
|
| 340 |
-
model = tf.keras.models.load_model('keras_model.h5')
|
| 341 |
-
data = np.ndarray(shape=(1, 224, 224, 3), dtype=np.float32)
|
| 342 |
-
|
| 343 |
-
with open("labels.txt", "r") as file:
|
| 344 |
-
labels = file.read().splitlines()
|
| 345 |
-
|
| 346 |
-
messages = [
|
| 347 |
-
{"role": "system", "content": system}
|
| 348 |
-
]
|
| 349 |
-
|
| 350 |
-
def classify(platform, UserInput, Images, Textbox2, Textbox3):
|
| 351 |
-
if Textbox3 == code:
|
| 352 |
-
imageData = None
|
| 353 |
-
if Images is not None:
|
| 354 |
-
output = []
|
| 355 |
-
headers = {
|
| 356 |
-
"Authorization": f"Bearer {auth2}"
|
| 357 |
-
}
|
| 358 |
-
if platform == "wh":
|
| 359 |
-
get_image = requests.get(Images, headers=headers)
|
| 360 |
-
if get_image.status_code == 200:
|
| 361 |
-
image_data = get_image.content
|
| 362 |
-
elif platform == "web":
|
| 363 |
-
print("WEB")
|
| 364 |
-
else:
|
| 365 |
-
pass
|
| 366 |
-
|
| 367 |
-
image = cv.imdecode(np.frombuffer(image_data, np.uint8), cv.IMREAD_COLOR)
|
| 368 |
-
image = cv.resize(image, (224, 224))
|
| 369 |
-
image_array = np.asarray(image)
|
| 370 |
-
normalized_image_array = (image_array.astype(np.float32) / 127.0) - 1
|
| 371 |
-
data[0] = normalized_image_array
|
| 372 |
-
|
| 373 |
-
prediction = model.predict(data)
|
| 374 |
-
|
| 375 |
-
max_label_index = None
|
| 376 |
-
max_prediction_value = -1
|
| 377 |
-
|
| 378 |
-
print('Prediction')
|
| 379 |
-
|
| 380 |
-
Textbox2 = Textbox2.replace("[", "").replace("]", "").replace("'", "")
|
| 381 |
-
Textbox2 = Textbox2.split(",")
|
| 382 |
-
Textbox2_edited = [x.strip() for x in Textbox2]
|
| 383 |
-
Textbox2_edited = list(Textbox2_edited)
|
| 384 |
-
Textbox2_edited.append(UserInput)
|
| 385 |
-
messages.append({"role": "user", "content": UserInput})
|
| 386 |
-
|
| 387 |
-
for i, label in enumerate(labels):
|
| 388 |
-
prediction_value = float(prediction[0][i])
|
| 389 |
-
rounded_value = round(prediction_value, 2)
|
| 390 |
-
print(f'{label}: {rounded_value}')
|
| 391 |
-
|
| 392 |
-
if prediction_value > max_prediction_value:
|
| 393 |
-
max_label_index = i
|
| 394 |
-
max_prediction_value = prediction_value
|
| 395 |
-
|
| 396 |
-
if max_label_index is not None:
|
| 397 |
-
max_label = labels[max_label_index].split(' ', 1)[1]
|
| 398 |
-
max_rounded_prediction = round(max_prediction_value, 2)
|
| 399 |
-
print(f'Maximum Prediction: {max_label} with a value of {max_rounded_prediction}')
|
| 400 |
-
|
| 401 |
-
if max_rounded_prediction > 0.5:
|
| 402 |
-
print("\nWays to dispose of this waste: " + max_label)
|
| 403 |
-
messages.append({"role": "user", "content": content + " " + max_label})
|
| 404 |
-
|
| 405 |
-
headers = {
|
| 406 |
-
"Content-Type": "application/json",
|
| 407 |
-
"Authorization": f"Bearer {auth}"
|
| 408 |
-
}
|
| 409 |
-
|
| 410 |
-
response = requests.post(host, headers=headers, json={
|
| 411 |
-
"messages": messages,
|
| 412 |
-
"model": model_llm
|
| 413 |
-
}).json()
|
| 414 |
-
|
| 415 |
-
print(response)
|
| 416 |
-
|
| 417 |
-
|
| 418 |
-
reply = response["choices"][0]["message"]["content"]
|
| 419 |
-
messages.append({"role": "assistant", "content": reply})
|
| 420 |
-
|
| 421 |
-
output.append({"Mode": "Image", "type": max_label, "prediction_value": max_rounded_prediction, "content": reply})
|
| 422 |
-
elif max_rounded_prediction < 0.5:
|
| 423 |
-
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"})
|
| 424 |
-
|
| 425 |
-
return output
|
| 426 |
-
|
| 427 |
-
else:
|
| 428 |
-
output = []
|
| 429 |
-
|
| 430 |
-
Textbox2 = Textbox2.replace("[", "").replace("]", "").replace("'", "")
|
| 431 |
-
Textbox2 = Textbox2.split(",")
|
| 432 |
-
Textbox2_edited = [x.strip() for x in Textbox2]
|
| 433 |
-
Textbox2_edited = list(Textbox2_edited)
|
| 434 |
-
Textbox2_edited.append(UserInput)
|
| 435 |
-
|
| 436 |
-
for i in Textbox2_edited:
|
| 437 |
-
messages.append({"role": "user", "content": i})
|
| 438 |
-
|
| 439 |
-
print("messages after appending:", messages)
|
| 440 |
-
|
| 441 |
-
messages.append({"role": "user", "content": UserInput})
|
| 442 |
-
|
| 443 |
-
headers = {
|
| 444 |
-
"Content-Type": "application/json",
|
| 445 |
-
"Authorization": f"Bearer {auth}"
|
| 446 |
-
}
|
| 447 |
-
|
| 448 |
-
response = requests.post(host, headers=headers, json={
|
| 449 |
-
"messages": messages,
|
| 450 |
-
"model": model_llm
|
| 451 |
-
}).json()
|
| 452 |
-
|
| 453 |
-
reply = response["choices"][0]["message"]["content"]
|
| 454 |
-
messages.append({"role": "assistant", "content": reply})
|
| 455 |
-
|
| 456 |
-
output.append({"Mode": "Chat", "content": reply})
|
| 457 |
-
|
| 458 |
-
return output
|
| 459 |
-
else:
|
| 460 |
-
return "Unauthorized"
|
| 461 |
-
|
| 462 |
-
user_inputs = [
|
| 463 |
-
gr.Textbox(label="Platform", type="text"),
|
| 464 |
-
gr.Textbox(label="User Input", type="text"),
|
| 465 |
-
gr.Textbox(label="Image", type="text"),
|
| 466 |
-
gr.Textbox(label="Textbox2", type="text"),
|
| 467 |
-
gr.Textbox(label="Textbox3", type="password")
|
| 468 |
-
]
|
| 469 |
-
|
| 470 |
-
iface = gr.Interface(
|
| 471 |
-
fn=classify,
|
| 472 |
-
inputs=user_inputs,
|
| 473 |
-
outputs=gr.outputs.JSON(),
|
| 474 |
-
title="Classifier",
|
| 475 |
-
)
|
| 476 |
-
iface.launch()
|
| 477 |
|
| 478 |
# import gradio as gr
|
| 479 |
# import numpy as np
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import numpy as np
|
| 3 |
+
import cv2 as cv
|
| 4 |
+
import requests
|
| 5 |
+
import time
|
| 6 |
+
import os
|
| 7 |
+
|
| 8 |
+
host = os.environ.get("host")
|
| 9 |
+
code = os.environ.get("code")
|
| 10 |
+
model_llm = os.environ.get("model")
|
| 11 |
+
content = os.environ.get("content")
|
| 12 |
+
state = os.environ.get("state")
|
| 13 |
+
system = os.environ.get("system")
|
| 14 |
+
auth = os.environ.get("auth")
|
| 15 |
+
data = None
|
| 16 |
+
model = None
|
| 17 |
+
image = None
|
| 18 |
+
prediction = None
|
| 19 |
+
labels = None
|
| 20 |
+
|
| 21 |
+
print('START')
|
| 22 |
+
np.set_printoptions(suppress=True)
|
| 23 |
+
|
| 24 |
+
data = np.ndarray(shape=(1, 224, 224, 3), dtype=np.float32)
|
| 25 |
+
|
| 26 |
+
with open("labels.txt", "r") as file:
|
| 27 |
+
labels = file.read().splitlines()
|
| 28 |
+
|
| 29 |
+
messages = [
|
| 30 |
+
{"role": "system", "content": system}
|
| 31 |
+
]
|
| 32 |
+
|
| 33 |
+
def classify(UserInput, Image, Textbox2, Textbox3):
|
| 34 |
+
if Textbox3 == code:
|
| 35 |
+
print("Image: ", Image)
|
| 36 |
+
if Image is not None:
|
| 37 |
+
output = []
|
| 38 |
+
image_data = np.array(Image)
|
| 39 |
+
image_data = cv.resize(image_data, (224, 224))
|
| 40 |
+
image_array = np.asarray(image_data)
|
| 41 |
+
normalized_image_array = (image_array.astype(np.float32) / 127.0) - 1
|
| 42 |
+
data[0] = normalized_image_array
|
| 43 |
+
|
| 44 |
+
import tensorflow as tf
|
| 45 |
+
model = tf.keras.models.load_model('keras_model.h5')
|
| 46 |
+
|
| 47 |
+
prediction = model.predict(data)
|
| 48 |
+
|
| 49 |
+
max_label_index = None
|
| 50 |
+
max_prediction_value = -1
|
| 51 |
+
|
| 52 |
+
print('Prediction')
|
| 53 |
+
|
| 54 |
+
Textbox2 = Textbox2.replace("[", "").replace("]", "").replace("'", "")
|
| 55 |
+
Textbox2 = Textbox2.split(",")
|
| 56 |
+
Textbox2_edited = [x.strip() for x in Textbox2]
|
| 57 |
+
Textbox2_edited = list(Textbox2_edited)
|
| 58 |
+
Textbox2_edited.append(UserInput)
|
| 59 |
+
messages.append({"role": "user", "content": UserInput})
|
| 60 |
+
|
| 61 |
+
for i, label in enumerate(labels):
|
| 62 |
+
prediction_value = float(prediction[0][i])
|
| 63 |
+
rounded_value = round(prediction_value, 2)
|
| 64 |
+
print(f'{label}: {rounded_value}')
|
| 65 |
+
|
| 66 |
+
if prediction_value > max_prediction_value:
|
| 67 |
+
max_label_index = i
|
| 68 |
+
max_prediction_value = prediction_value
|
| 69 |
+
|
| 70 |
+
if max_label_index is not None:
|
| 71 |
+
max_label = labels[max_label_index].split(' ', 1)[1]
|
| 72 |
+
max_rounded_prediction = round(max_prediction_value, 2)
|
| 73 |
+
print(f'Maximum Prediction: {max_label} with a value of {max_rounded_prediction}')
|
| 74 |
+
|
| 75 |
+
time.sleep(1)
|
| 76 |
+
if max_rounded_prediction > 0.5:
|
| 77 |
+
print("\nWays to dispose of this waste: " + max_label)
|
| 78 |
+
messages.append({"role": "user", "content": content + " " + max_label})
|
| 79 |
+
|
| 80 |
+
headers = {
|
| 81 |
+
"Content-Type": "application/json",
|
| 82 |
+
"Authorization": f"Bearer {auth}"
|
| 83 |
+
}
|
| 84 |
+
|
| 85 |
+
response = requests.post(host, headers=headers, json={
|
| 86 |
+
"messages": messages,
|
| 87 |
+
"model": model_llm
|
| 88 |
+
}).json()
|
| 89 |
+
|
| 90 |
+
reply = response["choices"][0]["message"]["content"]
|
| 91 |
+
messages.append({"role": "assistant", "content": reply})
|
| 92 |
+
|
| 93 |
+
output.append({"Mode": "Image", "type": max_label, "prediction_value": max_rounded_prediction, "content": reply})
|
| 94 |
+
elif max_rounded_prediction < 0.5:
|
| 95 |
+
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."})
|
| 96 |
+
|
| 97 |
+
return output
|
| 98 |
+
|
| 99 |
+
else:
|
| 100 |
+
output = []
|
| 101 |
+
|
| 102 |
+
Textbox2 = Textbox2.replace("[", "").replace("]", "").replace("'", "")
|
| 103 |
+
Textbox2 = Textbox2.split(",")
|
| 104 |
+
Textbox2_edited = [x.strip() for x in Textbox2]
|
| 105 |
+
Textbox2_edited = list(Textbox2_edited)
|
| 106 |
+
Textbox2_edited.append(UserInput)
|
| 107 |
+
|
| 108 |
+
for i in Textbox2_edited:
|
| 109 |
+
messages.append(
|
| 110 |
+
{"role": "user", "content": i}
|
| 111 |
+
)
|
| 112 |
+
|
| 113 |
+
print("messages after appending:", messages)
|
| 114 |
+
|
| 115 |
+
time.sleep(1)
|
| 116 |
+
messages.append({"role": "user", "content": UserInput})
|
| 117 |
+
|
| 118 |
+
headers = {
|
| 119 |
+
"Content-Type": "application/json",
|
| 120 |
+
"Authorization": f"Bearer {auth}"
|
| 121 |
+
}
|
| 122 |
+
|
| 123 |
+
response = requests.post(host, headers=headers, json={
|
| 124 |
+
"messages": messages,
|
| 125 |
+
"model": model_llm
|
| 126 |
+
}).json()
|
| 127 |
+
|
| 128 |
+
reply = response["choices"][0]["message"]["content"]
|
| 129 |
+
messages.append({"role": "assistant", "content": reply})
|
| 130 |
+
|
| 131 |
+
output.append({"Mode": "Chat", "content": reply})
|
| 132 |
+
|
| 133 |
+
return output
|
| 134 |
+
|
| 135 |
+
else:
|
| 136 |
+
return "Unauthorized"
|
| 137 |
+
|
| 138 |
+
user_inputs = [
|
| 139 |
+
gr.Textbox(label="User Input", type="text"),
|
| 140 |
+
gr.Image(),
|
| 141 |
+
gr.Textbox(label="Textbox2", type="text"),
|
| 142 |
+
gr.Textbox(label="Textbox3", type="password")
|
| 143 |
+
]
|
| 144 |
+
|
| 145 |
+
iface = gr.Interface(
|
| 146 |
+
fn=classify,
|
| 147 |
+
inputs=user_inputs,
|
| 148 |
+
outputs=gr.outputs.JSON(),
|
| 149 |
+
title="Classifier",
|
| 150 |
+
)
|
| 151 |
+
iface.launch()
|
| 152 |
+
|
| 153 |
+
|
| 154 |
# import gradio as gr
|
| 155 |
# import numpy as np
|
| 156 |
# import cv2 as cv
|
|
|
|
| 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
|
|
|
|
| 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
|
|
|
|
| 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 |
# ]
|
|
|
|
| 319 |
# import numpy as np
|
| 320 |
# import cv2 as cv
|
| 321 |
# import requests
|
| 322 |
+
# import io
|
| 323 |
+
# from PIL import Image
|
| 324 |
# import os
|
| 325 |
+
# import tensorflow as tf
|
| 326 |
+
# import random
|
| 327 |
|
| 328 |
# host = os.environ.get("host")
|
| 329 |
# code = os.environ.get("code")
|
|
|
|
| 334 |
# auth = os.environ.get("auth")
|
| 335 |
# auth2 = os.environ.get("auth2")
|
| 336 |
# data = None
|
|
|
|
|
|
|
|
|
|
|
|
|
| 337 |
|
|
|
|
| 338 |
# np.set_printoptions(suppress=True)
|
| 339 |
|
| 340 |
+
# model = tf.keras.models.load_model('keras_model.h5')
|
| 341 |
# data = np.ndarray(shape=(1, 224, 224, 3), dtype=np.float32)
|
| 342 |
|
| 343 |
# with open("labels.txt", "r") as file:
|
|
|
|
| 347 |
# {"role": "system", "content": system}
|
| 348 |
# ]
|
| 349 |
|
| 350 |
+
# def classify(platform, UserInput, Images, Textbox2, Textbox3):
|
| 351 |
# if Textbox3 == code:
|
| 352 |
+
# imageData = None
|
| 353 |
+
# if Images is not None:
|
| 354 |
# output = []
|
| 355 |
# headers = {
|
| 356 |
# "Authorization": f"Bearer {auth2}"
|
| 357 |
# }
|
| 358 |
# if platform == "wh":
|
| 359 |
+
# get_image = requests.get(Images, headers=headers)
|
| 360 |
+
# if get_image.status_code == 200:
|
| 361 |
+
# image_data = get_image.content
|
| 362 |
# elif platform == "web":
|
| 363 |
# print("WEB")
|
| 364 |
# else:
|
| 365 |
# pass
|
| 366 |
+
|
| 367 |
+
# image = cv.imdecode(np.frombuffer(image_data, np.uint8), cv.IMREAD_COLOR)
|
| 368 |
+
# image = cv.resize(image, (224, 224))
|
| 369 |
+
# image_array = np.asarray(image)
|
| 370 |
# normalized_image_array = (image_array.astype(np.float32) / 127.0) - 1
|
| 371 |
# data[0] = normalized_image_array
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 372 |
|
| 373 |
+
# prediction = model.predict(data)
|
| 374 |
+
|
| 375 |
# max_label_index = None
|
| 376 |
# max_prediction_value = -1
|
| 377 |
+
|
| 378 |
# print('Prediction')
|
| 379 |
+
|
| 380 |
# Textbox2 = Textbox2.replace("[", "").replace("]", "").replace("'", "")
|
| 381 |
# Textbox2 = Textbox2.split(",")
|
| 382 |
# Textbox2_edited = [x.strip() for x in Textbox2]
|
| 383 |
# Textbox2_edited = list(Textbox2_edited)
|
| 384 |
# Textbox2_edited.append(UserInput)
|
| 385 |
# messages.append({"role": "user", "content": UserInput})
|
| 386 |
+
|
| 387 |
# for i, label in enumerate(labels):
|
| 388 |
# prediction_value = float(prediction[0][i])
|
| 389 |
# rounded_value = round(prediction_value, 2)
|
| 390 |
# print(f'{label}: {rounded_value}')
|
| 391 |
+
|
| 392 |
# if prediction_value > max_prediction_value:
|
| 393 |
# max_label_index = i
|
| 394 |
+
# max_prediction_value = prediction_value
|
| 395 |
+
|
| 396 |
# if max_label_index is not None:
|
| 397 |
# max_label = labels[max_label_index].split(' ', 1)[1]
|
| 398 |
# max_rounded_prediction = round(max_prediction_value, 2)
|
| 399 |
# print(f'Maximum Prediction: {max_label} with a value of {max_rounded_prediction}')
|
| 400 |
+
|
|
|
|
| 401 |
# if max_rounded_prediction > 0.5:
|
| 402 |
# print("\nWays to dispose of this waste: " + max_label)
|
| 403 |
# messages.append({"role": "user", "content": content + " " + max_label})
|
| 404 |
+
|
| 405 |
# headers = {
|
| 406 |
# "Content-Type": "application/json",
|
| 407 |
# "Authorization": f"Bearer {auth}"
|
| 408 |
# }
|
| 409 |
+
|
| 410 |
# response = requests.post(host, headers=headers, json={
|
| 411 |
# "messages": messages,
|
| 412 |
# "model": model_llm
|
| 413 |
# }).json()
|
| 414 |
+
|
| 415 |
+
# print(response)
|
| 416 |
|
| 417 |
+
|
| 418 |
# reply = response["choices"][0]["message"]["content"]
|
| 419 |
# messages.append({"role": "assistant", "content": reply})
|
| 420 |
+
|
| 421 |
# output.append({"Mode": "Image", "type": max_label, "prediction_value": max_rounded_prediction, "content": reply})
|
| 422 |
# elif max_rounded_prediction < 0.5:
|
| 423 |
+
# 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"})
|
| 424 |
+
|
| 425 |
# return output
|
| 426 |
|
| 427 |
# else:
|
| 428 |
# output = []
|
| 429 |
+
|
| 430 |
# Textbox2 = Textbox2.replace("[", "").replace("]", "").replace("'", "")
|
| 431 |
# Textbox2 = Textbox2.split(",")
|
| 432 |
# Textbox2_edited = [x.strip() for x in Textbox2]
|
| 433 |
# Textbox2_edited = list(Textbox2_edited)
|
| 434 |
# Textbox2_edited.append(UserInput)
|
| 435 |
+
|
| 436 |
# for i in Textbox2_edited:
|
| 437 |
+
# messages.append({"role": "user", "content": i})
|
| 438 |
+
|
|
|
|
|
|
|
| 439 |
# print("messages after appending:", messages)
|
| 440 |
+
|
|
|
|
| 441 |
# messages.append({"role": "user", "content": UserInput})
|
| 442 |
|
| 443 |
# headers = {
|
| 444 |
# "Content-Type": "application/json",
|
| 445 |
# "Authorization": f"Bearer {auth}"
|
| 446 |
# }
|
| 447 |
+
|
| 448 |
# response = requests.post(host, headers=headers, json={
|
| 449 |
# "messages": messages,
|
| 450 |
# "model": model_llm
|
| 451 |
# }).json()
|
| 452 |
+
|
| 453 |
# reply = response["choices"][0]["message"]["content"]
|
| 454 |
# messages.append({"role": "assistant", "content": reply})
|
| 455 |
|
| 456 |
# output.append({"Mode": "Chat", "content": reply})
|
|
|
|
|
|
|
| 457 |
|
| 458 |
+
# return output
|
| 459 |
# else:
|
| 460 |
# return "Unauthorized"
|
| 461 |
|
|
|
|
| 473 |
# outputs=gr.outputs.JSON(),
|
| 474 |
# title="Classifier",
|
| 475 |
# )
|
| 476 |
+
# # iface.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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