File size: 3,477 Bytes
a5f33b4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b9e1b44
a5f33b4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
import random


VIDEO_ROSTER = [
    ("videos/1.mp4",  "real"),
    ("videos/2.mp4",  "deepfake"),
    ("videos/3.mp4",  "deepfake"),
    ("videos/4.mp4",  "deepfake"),
    ("videos/5.mp4",  "deepfake"),
    ("videos/6.mp4",  "deepfake"),
    ("videos/7.mp4",  "real"),
    ("videos/8.mp4",  "real"),
    ("videos/9.mp4",  "real"),
    ("videos/10.mp4", "real"),
]

def start_game():
    vids = VIDEO_ROSTER.copy()
    random.shuffle(vids)
    return [vids, 0, 0, []]           # [videos, index, score, guesses]

def next_video(state, guess):
    vids, idx, score, guesses = state

    if idx > 0:
        guesses.append(guess)
        if guess == vids[idx - 1][1]:
            score += 1

    if idx >= len(vids):
        return None, [vids, idx, score, guesses]

    video_path = vids[idx][0]
    return video_path, [vids, idx + 1, score, guesses]

def show_results(score, vids, guesses):
    header = gr.update(value=f"### Game over! You got **{score}/10** correct.",
                       visible=True)
    updates = [header]

    for (path, label), guess in zip(vids, guesses):
        correct = guess == label
        color   = "green" if correct else "red"
        caption = (f"<span style='color:{color}; font-weight:bold;'>"
                   f"{'Correct' if correct else 'Incorrect'}, "
                   f"this video is {label.capitalize()}</span>")
        updates.append(gr.update(value=path, visible=True))
        updates.append(gr.update(value=caption, visible=True))
    return updates

def hide_results():
    return [gr.update(visible=False)] * (1 + 10*2)


with gr.Blocks() as demo:
    gr.Markdown("# Deepfake Game\nDecide whether each verification is **Real** or **Deepfake**")
    state      = gr.State(start_game())
    video_box  = gr.Video(label="Video", interactive=False)

    with gr.Row():
        btn_real     = gr.Button("Real")
        btn_deepfake = gr.Button("Deepfake")

    result_header = gr.Markdown(visible=False)

    result_pairs = []
    for _ in range(10):
        with gr.Column():
            v = gr.Video(interactive=False, visible=False, width=360, height=270)
            c = gr.Markdown(visible=False)
            result_pairs.append((v, c))

    def on_guess(choice, st):
        vid_path, new_st = next_video(st, choice)

        if vid_path is None:
            out = [gr.update(value=None, visible=False),
                   gr.update(visible=False),
                   gr.update(visible=False),
                   new_st]
            out += show_results(new_st[2], new_st[0], new_st[3])
            return out

        out = [gr.update(value=vid_path, visible=True),
               gr.update(visible=True),
               gr.update(visible=True),
               new_st]
        out += hide_results()
        return out

    outputs = [video_box, btn_real, btn_deepfake, state, result_header]
    for v, c in result_pairs:
        outputs.extend([v, c])

    btn_real.click(lambda s: on_guess("real", s),     inputs=state, outputs=outputs)
    btn_deepfake.click(lambda s: on_guess("deepfake", s), inputs=state, outputs=outputs)

    def on_load():
        init_state = start_game()
        first_clip = init_state[0][0][0]
        init_state[1] = 1
        return [first_clip,
                gr.update(visible=True),
                gr.update(visible=True),
                init_state] + hide_results()

    demo.load(on_load, None, outputs)

if __name__ == "__main__":
    demo.launch(share=True)