Hellowish commited on
Commit
4353bdb
·
verified ·
1 Parent(s): 9c86e2f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -8
app.py CHANGED
@@ -100,36 +100,46 @@ import tensorflow as tf
100
  import pickle
101
 
102
  # ---------------- 載入模型 ----------------
103
- model = tf.keras.models.load_model("AIDetect.h5") # 你的模型檔案
104
  with open("vectorizer.pkl", "rb") as f:
105
  vectorizer = pickle.load(f)
106
  with open("scaler.pkl", "rb") as f:
107
  scaler = pickle.load(f)
108
 
109
- # ---------------- 特徵計算(純 Python) ----------------
110
  def compute_features(text):
111
  words = text.split()
112
  word_count = len(words)
113
- unique_word_ratio = len(set(words)) / (word_count + 1e-6)
 
114
  repeat_rate = 1 - unique_word_ratio
115
  punctuation_count = sum(1 for c in text if c in ".,!?;:")
116
  punctuation_ratio = punctuation_count / (len(text) + 1e-6)
117
- avg_word_length = sum(len(w) for w in words) / (word_count + 1e-6) if words else 0
118
- # 直接返回列表,不用 numpy
119
  return [[word_count, unique_word_ratio, repeat_rate, punctuation_ratio, avg_word_length]]
120
 
 
 
 
 
 
 
 
 
 
 
121
  # ---------------- 生成解釋 ----------------
122
  def explain_prediction(text):
123
  # 文字向量化
124
  seq = vectorizer([text])
125
- seq = tf.keras.preprocessing.sequence.pad_sequences(seq, maxlen=50, padding='pre')
126
 
127
  # 統計特徵
128
  feat = compute_features(text)
129
- feat = scaler.transform(feat)
130
 
131
  # 預測
132
- pred_prob = model.predict([seq, feat])[0][0]
133
  label = "AI 生成" if pred_prob >= 0.5 else "人類撰寫"
134
  prob = pred_prob * 100
135
 
 
100
  import pickle
101
 
102
  # ---------------- 載入模型 ----------------
103
+ model = tf.keras.models.load_model("AIDetect.h5")
104
  with open("vectorizer.pkl", "rb") as f:
105
  vectorizer = pickle.load(f)
106
  with open("scaler.pkl", "rb") as f:
107
  scaler = pickle.load(f)
108
 
109
+ # ---------------- 純 Python 特徵計算 ----------------
110
  def compute_features(text):
111
  words = text.split()
112
  word_count = len(words)
113
+ unique_words = len(set(words))
114
+ unique_word_ratio = unique_words / (word_count + 1e-6)
115
  repeat_rate = 1 - unique_word_ratio
116
  punctuation_count = sum(1 for c in text if c in ".,!?;:")
117
  punctuation_ratio = punctuation_count / (len(text) + 1e-6)
118
+ avg_word_length = sum(len(w) for w in words) / (word_count if word_count else 1)
 
119
  return [[word_count, unique_word_ratio, repeat_rate, punctuation_ratio, avg_word_length]]
120
 
121
+ # ---------------- 純 Python 標準化 ----------------
122
+ def transform_features(feat):
123
+ # scaler 是舊的 scikit-learn StandardScaler,裡面有 mean_ 和 scale_
124
+ mean = scaler.mean_
125
+ scale = scaler.scale_
126
+ transformed = []
127
+ for i, val in enumerate(feat[0]):
128
+ transformed.append((val - mean[i]) / scale[i])
129
+ return [transformed]
130
+
131
  # ---------------- 生成解釋 ----------------
132
  def explain_prediction(text):
133
  # 文字向量化
134
  seq = vectorizer([text])
135
+ seq = tf.keras.utils.pad_sequences(seq, maxlen=50, padding='pre')
136
 
137
  # 統計特徵
138
  feat = compute_features(text)
139
+ feat = transform_features(feat)
140
 
141
  # 預測
142
+ pred_prob = model.predict([seq, feat], verbose=0)[0][0]
143
  label = "AI 生成" if pred_prob >= 0.5 else "人類撰寫"
144
  prob = pred_prob * 100
145