boluobobo commited on
Commit
872a155
·
verified ·
1 Parent(s): f575344

Upload README.md with huggingface_hub

Browse files
Files changed (1) hide show
  1. README.md +47 -28
README.md CHANGED
@@ -164,39 +164,51 @@ def detect_image(image_path):
164
  outputs = model(**inputs)
165
  probs = torch.softmax(outputs.logits, dim=-1)[0]
166
 
167
- # Aggregate human vs AI probability
168
- human_prob = sum(
169
- probs[int(i)].item()
170
- for i, label in model.config.id2label.items()
171
- if label in REAL_LABELS
172
- )
173
- ai_prob = 1.0 - human_prob
174
-
175
- # Get top 3 predictions
176
- top3_idx = probs.argsort(descending=True)[:3]
177
- top3 = [
178
- {"label": model.config.id2label[str(i.item())], "score": probs[i].item()}
179
- for i in top3_idx
180
- ]
 
 
 
 
 
 
 
181
 
182
  return {
183
- "ai_probability": ai_prob,
184
- "human_probability": human_prob,
185
- "top3_sources": top3
 
 
186
  }
187
 
188
  # Example
189
  result = detect_image("test.jpg")
190
  print(f"AI Probability: {result['ai_probability']:.1%}")
191
  print(f"Human Probability: {result['human_probability']:.1%}")
192
- print(f"Top 3 Sources: {result['top3_sources']}")
 
193
  ```
194
 
195
  **Example Output:**
196
  ```json
197
  {
198
- "ai_probability": 0.877,
199
- "human_probability": 0.123,
 
 
200
  "top3_sources": [
201
  {"label": "stable_diffusion", "score": 0.452},
202
  {"label": "latent_diffusion", "score": 0.213},
@@ -283,14 +295,21 @@ for i, (name, prob) in enumerate(zip(source_names, probs[0].tolist())):
283
  ### Calculate Real vs Fake Probability
284
 
285
  ```python
286
- # Aggregate real vs fake probabilities
287
- real_prob = sum(
288
- probs[0][i].item()
289
- for i, name in enumerate(source_names)
290
- if source_is_real.get(name, False)
291
- )
292
- fake_prob = 1.0 - real_prob
293
-
 
 
 
 
 
 
 
294
  print(f"Real: {real_prob:.2%}")
295
  print(f"AI Generated: {fake_prob:.2%}")
296
  ```
 
164
  outputs = model(**inputs)
165
  probs = torch.softmax(outputs.logits, dim=-1)[0]
166
 
167
+ # Top-1 决定 + 置信度
168
+ top_idx = probs.argmax().item()
169
+ top_source = model.config.id2label[str(top_idx)]
170
+ top_confidence = probs[top_idx].item()
171
+ is_real = top_source in REAL_LABELS
172
+
173
+ if is_real:
174
+ human_prob = top_confidence
175
+ ai_prob = 1.0 - human_prob
176
+ else:
177
+ ai_prob = top_confidence
178
+ human_prob = 1.0 - ai_prob
179
+
180
+ # Get top 3 AI sources
181
+ ai_sources = []
182
+ for i, prob in enumerate(probs.tolist()):
183
+ label = model.config.id2label[str(i)]
184
+ if label not in REAL_LABELS:
185
+ ai_sources.append({"label": label, "score": round(prob, 3)})
186
+ ai_sources.sort(key=lambda x: x["score"], reverse=True)
187
+ top3_sources = ai_sources[:3]
188
 
189
  return {
190
+ "ai_probability": round(ai_prob, 3),
191
+ "human_probability": round(human_prob, 3),
192
+ "predicted_source": top_source,
193
+ "is_real": is_real,
194
+ "top3_sources": top3_sources
195
  }
196
 
197
  # Example
198
  result = detect_image("test.jpg")
199
  print(f"AI Probability: {result['ai_probability']:.1%}")
200
  print(f"Human Probability: {result['human_probability']:.1%}")
201
+ print(f"Predicted Source: {result['predicted_source']}")
202
+ print(f"Is Real: {result['is_real']}")
203
  ```
204
 
205
  **Example Output:**
206
  ```json
207
  {
208
+ "ai_probability": 0.452,
209
+ "human_probability": 0.548,
210
+ "predicted_source": "stable_diffusion",
211
+ "is_real": false,
212
  "top3_sources": [
213
  {"label": "stable_diffusion", "score": 0.452},
214
  {"label": "latent_diffusion", "score": 0.213},
 
295
  ### Calculate Real vs Fake Probability
296
 
297
  ```python
298
+ # Top-1 决定 + 置信度
299
+ top_idx = probs[0].argmax().item()
300
+ top_source = source_names[top_idx]
301
+ top_confidence = probs[0][top_idx].item()
302
+ is_real = source_is_real.get(top_source, False)
303
+
304
+ if is_real:
305
+ real_prob = top_confidence
306
+ fake_prob = 1.0 - real_prob
307
+ else:
308
+ fake_prob = top_confidence
309
+ real_prob = 1.0 - fake_prob
310
+
311
+ print(f"Predicted Source: {top_source}")
312
+ print(f"Is Real: {'Yes' if is_real else 'No (AI Generated)'}")
313
  print(f"Real: {real_prob:.2%}")
314
  print(f"AI Generated: {fake_prob:.2%}")
315
  ```