Ailyth commited on
Commit
a6f0e8b
1 Parent(s): 6764e71

add cache data

Browse files
Files changed (1) hide show
  1. app.py +10 -4
app.py CHANGED
@@ -9,8 +9,13 @@ from tensorflow.keras.models import load_model
9
  from tensorflow.keras.preprocessing import image
10
 
11
  UTC_8 = pytz.timezone('Asia/Shanghai')
12
- #Load the model
13
- my_model = load_model("model/zha2024_6.h5")
 
 
 
 
 
14
  target_size = (300, 300)
15
  class_labels = {0: '炭黑组', 1: '正常发挥', 2: '炫彩组', 3: '糊糊组', 4: '炸组日常', 5: '凡尔赛',6: '非食物'}
16
  predicted_class=''
@@ -52,7 +57,8 @@ safety_settings = [
52
  ]
53
 
54
  #fuctions
55
- def preprocess_image(img_path):
 
56
  img = image.load_img(img_path, target_size=target_size)
57
  img_array = image.img_to_array(img)
58
  img_array = np.expand_dims(img_array, axis=0)
@@ -83,7 +89,7 @@ def get_critic_info(review_style):
83
 
84
  def img_score(img_raw_path):
85
  global predicted_class
86
- img_array = preprocess_image(img_raw_path)
87
  predictions = my_model.predict(img_array)
88
  predicted_class_index = np.argmax(predictions, axis=-1)
89
  predicted_class = class_labels[predicted_class_index[0]]
 
9
  from tensorflow.keras.preprocessing import image
10
 
11
  UTC_8 = pytz.timezone('Asia/Shanghai')
12
+ #cahe func
13
+ @st.cache_resource
14
+ def load_my_model():
15
+ model = load_model("model/zha2024_6.h5")
16
+ return model
17
+
18
+ my_model = load_my_model()
19
  target_size = (300, 300)
20
  class_labels = {0: '炭黑组', 1: '正常发挥', 2: '炫彩组', 3: '糊糊组', 4: '炸组日常', 5: '凡尔赛',6: '非食物'}
21
  predicted_class=''
 
57
  ]
58
 
59
  #fuctions
60
+ @st.cache_data
61
+ def preprocess_image_cached(img_path, target_size):
62
  img = image.load_img(img_path, target_size=target_size)
63
  img_array = image.img_to_array(img)
64
  img_array = np.expand_dims(img_array, axis=0)
 
89
 
90
  def img_score(img_raw_path):
91
  global predicted_class
92
+ img_array = preprocess_image_cached(img_raw_path, target_size)
93
  predictions = my_model.predict(img_array)
94
  predicted_class_index = np.argmax(predictions, axis=-1)
95
  predicted_class = class_labels[predicted_class_index[0]]