ElBeh commited on
Commit
fa966ee
·
verified ·
1 Parent(s): 6a811d6

Upload tab_classify_image.py

Browse files
Files changed (1) hide show
  1. tabs/tab_classify_image.py +59 -0
tabs/tab_classify_image.py ADDED
@@ -0,0 +1,59 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/usr/bin/env python3
2
+ # -*- coding: utf-8 -*-
3
+ """
4
+ Created on Tue Jan 13 09:52:28 2026
5
+
6
+ @author: standarduser
7
+ """
8
+
9
+ from gradio_client import Client, handle_file
10
+ import gradio as gr
11
+
12
+ def predict_from_space(image_path):
13
+ """Classify image using Space API."""
14
+
15
+ client = Client("ElBeh/image-fake-detector")
16
+
17
+ try:
18
+ result = client.predict(
19
+ image=handle_file(image_path),
20
+ api_name="/predict"
21
+ )
22
+
23
+ # Gradio Label format: {'label': 'Fake', 'confidences': [{'label': 'Fake', 'confidence': 0.88}, ...]}
24
+ confidences = result['confidences']
25
+
26
+ # Extract probabilities from confidences list
27
+ proba_dict = {item['label']: item['confidence'] for item in confidences}
28
+ proba_real = proba_dict.get('Real', 0.0)
29
+ proba_fake = proba_dict.get('Fake', 0.0)
30
+
31
+ # Determine prediction
32
+ prediction = 1 if proba_fake > 0.5 else 0
33
+ label = "Fake" if prediction == 1 else "Real"
34
+ confidence = proba_fake if prediction == 1 else proba_real
35
+
36
+ print(f"\nPrediction: {label}")
37
+ print(f"Confidence: {confidence:.4f} ({confidence*100:.2f}%)")
38
+ print(f"Real: {proba_real:.4f} | Fake: {proba_fake:.4f}")
39
+
40
+ return {
41
+ 'Real': float(proba_real),
42
+ 'Fake': float(proba_fake)
43
+ }
44
+
45
+ except Exception as e:
46
+ print(f"Error: {e}")
47
+ raise
48
+
49
+ def create_tab_classify_image(tab_label):
50
+ with gr.TabItem(tab_label):
51
+ gr.Interface(
52
+ fn=predict_from_space,
53
+ inputs=[
54
+ gr.Image(type="filepath", label="Upload Image"),
55
+ ],
56
+ outputs=gr.Label(num_top_classes=2, label="Prediction"),
57
+ title="Image Fake Detector",
58
+ description="Upload an image to classify it as real or fake. The detector(XGBoost) uses several image statistics to classify the image."
59
+ )