tommy24 commited on
Commit
74dec5e
·
1 Parent(s): 8d1ea7c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +78 -4
app.py CHANGED
@@ -1,7 +1,81 @@
1
  import gradio as gr
 
 
 
 
 
 
2
 
3
- def greet(name):
4
- return "Hello " + name + "!!"
 
 
 
 
 
 
 
 
5
 
6
- iface = gr.Interface(fn=greet, inputs="text", outputs="text")
7
- iface.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import gradio as gr
2
+ import tensorflow
3
+ import numpy as np
4
+ import cv2 as cv
5
+ import requests
6
+ import time
7
+ import os
8
 
9
+ host = os.environ.get("host")
10
+ code = os.environ.get("code")
11
+ model = os.environ.get("model")
12
+ data = None
13
+ model = None
14
+ image = None
15
+ prediction = None
16
+ labels = None
17
+ max_label_index = None
18
+ max_prediction_value = -1
19
 
20
+ print('START')
21
+ np.set_printoptions(suppress=True)
22
+
23
+ model = tensorflow.keras.models.load_model('keras_model.h5')
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(image_path):
30
+ try:
31
+ image = cv.imread(image_path)
32
+ image = cv.resize(image, (224, 224))
33
+ image_array = np.asarray(image)
34
+ normalized_image_array = (image_array.astype(np.float32) / 127.0) - 1
35
+ data[0] = normalized_image_array
36
+ prediction = model.predict(data)
37
+
38
+ print('Prediction')
39
+
40
+ for i, label in enumerate(labels):
41
+ prediction_value = float(prediction[0][i])
42
+ rounded_value = round(prediction_value, 2)
43
+ print(f'{label}: {rounded_value}')
44
+
45
+ if prediction_value > max_prediction_value:
46
+ max_label_index = i
47
+ max_prediction_value = prediction_value
48
+
49
+ if max_label_index is not None:
50
+ max_label = labels[max_label_index].split(' ', 1)[1]
51
+ print(f'Maximum Prediction: {max_label} with a value of {round(max_prediction_value, 2)}')
52
+
53
+ time.sleep(1)
54
+ print("\nWays to dispose this waste: " + max_label)
55
+ payload = [
56
+ {"role": "system", "content": "You are a helpful assistant."},
57
+ {"role": "user", "content": "Give me the steps to dispose this waste in bulleting points 5 max: " + "Plastic"}
58
+ ]
59
+
60
+ response = requests.post(host, json={
61
+ "messages": payload,
62
+ "model": model,
63
+ "temperature": 0.5,
64
+ "presence_penalty": 0,
65
+ "frequency_penalty": 0,
66
+ "top_p": 1
67
+ }).json()
68
+
69
+ return response["choices"][0]["message"]["content"]
70
+
71
+ except Exception as e:
72
+ return f"An error occurred: {e}"
73
+
74
+ iface = gr.Interface(
75
+ fn=classify,
76
+ inputs="text",
77
+ outputs="text",
78
+ title="Waste Classifier",
79
+ description="Upload an image to classify and get disposal instructions."
80
+ )
81
+ iface.launch()