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np
|
| 3 |
import cv2 as cv
|
|
@@ -26,7 +183,7 @@ data = np.ndarray(shape=(1, 224, 224, 3), dtype=np.float32)
|
|
| 26 |
with open("labels.txt", "r") as file:
|
| 27 |
labels = file.read().splitlines()
|
| 28 |
|
| 29 |
-
def classify(
|
| 30 |
if Textbox3 == code:
|
| 31 |
if Image is not None:
|
| 32 |
output = []
|
|
@@ -50,17 +207,11 @@ def classify(Textbox, Image, Textbox2, Textbox3):
|
|
| 50 |
Textbox2 = Textbox2.split(",")
|
| 51 |
Textbox2_edited = [x.strip() for x in Textbox2]
|
| 52 |
Textbox2_edited = list(Textbox2_edited)
|
| 53 |
-
Textbox2_edited.append(
|
| 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])
|
|
@@ -74,10 +225,9 @@ def classify(Textbox, Image, Textbox2, Textbox3):
|
|
| 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 = {
|
|
@@ -86,14 +236,14 @@ def classify(Textbox, Image, Textbox2, Textbox3):
|
|
| 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 |
|
|
@@ -104,20 +254,18 @@ def classify(Textbox, Image, Textbox2, Textbox3):
|
|
| 104 |
Textbox2 = Textbox2.split(",")
|
| 105 |
Textbox2_edited = [x.strip() for x in Textbox2]
|
| 106 |
Textbox2_edited = list(Textbox2_edited)
|
| 107 |
-
Textbox2_edited.append(
|
| 108 |
messages = [
|
| 109 |
{"role": "system", "content": system},
|
| 110 |
]
|
| 111 |
-
|
| 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":
|
| 121 |
|
| 122 |
headers = {
|
| 123 |
"Content-Type": "application/json",
|
|
@@ -125,20 +273,20 @@ def classify(Textbox, Image, Textbox2, Textbox3):
|
|
| 125 |
}
|
| 126 |
|
| 127 |
response = requests.post(host, headers=headers, json={
|
| 128 |
-
"messages":messages,
|
| 129 |
-
"model":model_llm
|
| 130 |
}).json()
|
| 131 |
|
| 132 |
reply = response["choices"][0]["message"]["content"]
|
| 133 |
messages.append({"role": "assistant", "content": reply})
|
| 134 |
|
| 135 |
-
output.append({"Mode":"Chat","content": reply})
|
| 136 |
|
| 137 |
return output
|
| 138 |
|
| 139 |
else:
|
| 140 |
return "Unauthorized"
|
| 141 |
-
|
| 142 |
user_inputs = [
|
| 143 |
gr.Textbox(label="User Input", type="text"),
|
| 144 |
gr.Image(),
|
|
|
|
| 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 |
+
# def classify(Textbox, Image, Textbox2, Textbox3):
|
| 30 |
+
# if Textbox3 == code:
|
| 31 |
+
# if Image is not 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": Textbox})
|
| 121 |
+
|
| 122 |
+
# headers = {
|
| 123 |
+
# "Content-Type": "application/json",
|
| 124 |
+
# "Authorization": f"Bearer {auth}"
|
| 125 |
+
# }
|
| 126 |
+
|
| 127 |
+
# response = requests.post(host, headers=headers, json={
|
| 128 |
+
# "messages":messages,
|
| 129 |
+
# "model":model_llm
|
| 130 |
+
# }).json()
|
| 131 |
+
|
| 132 |
+
# reply = response["choices"][0]["message"]["content"]
|
| 133 |
+
# messages.append({"role": "assistant", "content": reply})
|
| 134 |
+
|
| 135 |
+
# output.append({"Mode":"Chat","content": reply})
|
| 136 |
+
|
| 137 |
+
# return output
|
| 138 |
+
|
| 139 |
+
# else:
|
| 140 |
+
# return "Unauthorized"
|
| 141 |
+
|
| 142 |
+
# user_inputs = [
|
| 143 |
+
# gr.Textbox(label="User Input", type="text"),
|
| 144 |
+
# gr.Image(),
|
| 145 |
+
# gr.Textbox(label="Textbox2", type="text"),
|
| 146 |
+
# gr.Textbox(label="Textbox3", type="password")
|
| 147 |
+
# ]
|
| 148 |
+
|
| 149 |
+
# iface = gr.Interface(
|
| 150 |
+
# fn=classify,
|
| 151 |
+
# inputs=user_inputs,
|
| 152 |
+
# outputs=gr.outputs.JSON(),
|
| 153 |
+
# title="Classifier",
|
| 154 |
+
# )
|
| 155 |
+
# iface.launch()
|
| 156 |
+
|
| 157 |
+
|
| 158 |
import gradio as gr
|
| 159 |
import numpy as np
|
| 160 |
import cv2 as cv
|
|
|
|
| 183 |
with open("labels.txt", "r") as file:
|
| 184 |
labels = file.read().splitlines()
|
| 185 |
|
| 186 |
+
def classify(UserInput, Image, Textbox2, Textbox3):
|
| 187 |
if Textbox3 == code:
|
| 188 |
if Image is not None:
|
| 189 |
output = []
|
|
|
|
| 207 |
Textbox2 = Textbox2.split(",")
|
| 208 |
Textbox2_edited = [x.strip() for x in Textbox2]
|
| 209 |
Textbox2_edited = list(Textbox2_edited)
|
| 210 |
+
Textbox2_edited.append(UserInput)
|
| 211 |
messages = [
|
| 212 |
{"role": "system", "content": system},
|
| 213 |
+
{"role": "user", "content": UserInput},
|
| 214 |
]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 215 |
|
| 216 |
for i, label in enumerate(labels):
|
| 217 |
prediction_value = float(prediction[0][i])
|
|
|
|
| 225 |
if max_label_index is not None:
|
| 226 |
max_label = labels[max_label_index].split(' ', 1)[1]
|
| 227 |
print(f'Maximum Prediction: {max_label} with a value of {round(max_prediction_value, 2)}')
|
| 228 |
+
|
| 229 |
time.sleep(1)
|
| 230 |
print("\nWays to dispose of this waste: " + max_label)
|
|
|
|
| 231 |
messages.append({"role": "user", "content": content + " " + max_label})
|
| 232 |
|
| 233 |
headers = {
|
|
|
|
| 236 |
}
|
| 237 |
|
| 238 |
response = requests.post(host, headers=headers, json={
|
| 239 |
+
"messages": messages,
|
| 240 |
+
"model": model_llm
|
| 241 |
}).json()
|
| 242 |
|
| 243 |
reply = response["choices"][0]["message"]["content"]
|
| 244 |
messages.append({"role": "assistant", "content": reply})
|
| 245 |
|
| 246 |
+
output.append({"Mode": "Image", "type": max_label, "prediction_value": rounded_value, "content": reply})
|
| 247 |
|
| 248 |
return output
|
| 249 |
|
|
|
|
| 254 |
Textbox2 = Textbox2.split(",")
|
| 255 |
Textbox2_edited = [x.strip() for x in Textbox2]
|
| 256 |
Textbox2_edited = list(Textbox2_edited)
|
| 257 |
+
Textbox2_edited.append(UserInput)
|
| 258 |
messages = [
|
| 259 |
{"role": "system", "content": system},
|
| 260 |
]
|
| 261 |
+
|
|
|
|
| 262 |
for i in Textbox2_edited:
|
| 263 |
messages.append(
|
| 264 |
{"role": "user", "content": i}
|
| 265 |
)
|
|
|
|
| 266 |
|
| 267 |
time.sleep(1)
|
| 268 |
+
messages.append({"role": "user", "content": UserInput})
|
| 269 |
|
| 270 |
headers = {
|
| 271 |
"Content-Type": "application/json",
|
|
|
|
| 273 |
}
|
| 274 |
|
| 275 |
response = requests.post(host, headers=headers, json={
|
| 276 |
+
"messages": messages,
|
| 277 |
+
"model": model_llm
|
| 278 |
}).json()
|
| 279 |
|
| 280 |
reply = response["choices"][0]["message"]["content"]
|
| 281 |
messages.append({"role": "assistant", "content": reply})
|
| 282 |
|
| 283 |
+
output.append({"Mode": "Chat", "content": reply})
|
| 284 |
|
| 285 |
return output
|
| 286 |
|
| 287 |
else:
|
| 288 |
return "Unauthorized"
|
| 289 |
+
|
| 290 |
user_inputs = [
|
| 291 |
gr.Textbox(label="User Input", type="text"),
|
| 292 |
gr.Image(),
|