File size: 3,198 Bytes
3fdc2a4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6d46819
3fdc2a4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
import os
import time
import shutil
import gradio as gr
from detect import detect
import pygmtools as pygm


# Define file address constant
FD_IMG_DEFAULT_PATH = "media/fake_detect_default.png"
FD_SOLUTION_PATH = "media/fake_detect_pie.png"
PRETRAINED_PATH = "fc_weights.pth"


def _handle_fd_solve(img_path: str):
    # Check file upload status
    if img_path is None:
        raise gr.Error("Please upload file completely!")
    
    # Begin solve and record the solving time
    start_time = time.time()
    detect(
        img_path=img_path,
        save_path=FD_SOLUTION_PATH,
        pretrained_path=PRETRAINED_PATH
    )
    solved_time = time.time() - start_time
    
    # Message
    message = "Successfully detect the image, using time ({:.3f}s).".format(solved_time)
    
    return message, FD_SOLUTION_PATH
    

def handle_fd_solve(img_path: str):
    try:
        message = _handle_fd_solve(img_path)
        return message
    except Exception as e:
        message = str(e)
        return message, FD_SOLUTION_PATH


def handle_ged_clear():
    # Replace the original image with the default image
    shutil.copy(
        src=FD_IMG_DEFAULT_PATH,
        dst=FD_SOLUTION_PATH
    )

    message = "successfully clear the files!"
    return message, FD_SOLUTION_PATH


with gr.Blocks() as ged_page:
    
    gr.Markdown(
        '''
        This space displays that how to detect the images generated by AI.
        ## How to use this Space?
        - Upload a '.png' or '.jpg' image.
        - The detection result will be shown after you click the detect button.
        - Click the 'clear' button to clear all the files.
        ## Examples
        - You can get the test examples from our [FakeDetect Dataset Repo.](https://huggingface.co/datasets/SJTU-TES/Fake-Detect) 
        '''
    )

    with gr.Row(variant="panel"):
        with gr.Column(scale=1):
            with gr.Row():
                fd_img = gr.Image(
                    type="filepath"
                )
            info = gr.Textbox(
                value="",
                label="Log",
                scale=4,
            )
            with gr.Row():
                with gr.Column(scale=1, min_width=100):
                    solve_button = gr.Button(
                        value="Detect", 
                        variant="primary", 
                        scale=1
                    )
                with gr.Column(scale=1, min_width=100):
                    clear_button = gr.Button(
                        "Clear", 
                        variant="secondary", 
                        scale=1
                    )
                with gr.Column(scale=8):
                    pass    
        with gr.Row(variant="panel"):
            fd_solution = gr.Image(
                value=FD_SOLUTION_PATH, 
                type="filepath",
                label="Detection Result"
            )

     
    
    solve_button.click(
        handle_fd_solve,
        [fd_img],
        outputs=[info, fd_solution]
    )
    
    clear_button.click(
        handle_ged_clear,
        inputs=None,
        outputs=[info, fd_solution]
    )


if __name__ == "__main__":
    ged_page.launch(debug = True)