File size: 1,822 Bytes
83cac08
 
 
d23c767
93a4013
83cac08
 
 
 
e93366d
d23c767
83cac08
 
 
d23c767
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c52cf91
e93366d
d23c767
 
 
 
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
import gradio as gr
from PIL import Image
from vit_model_test import CustomModel
import time

# Initialize the model
model = CustomModel()

def predict(image: Image.Image):
    print("Predict function called")  # 讛讜住驻转 砖讜专转 讛讚驻住讛
    time.sleep(5)  # 住讬诪讜诇爪讬讛 砖诇 讝诪谉 注讬讘讜讚
    label, confidence = model.predict(image)
    result = "AI image" if label == 1 else "Real image"
    return result, f"Confidence: {confidence:.2f}%"

def loading_animation(image):
    return gr.Video.update(visible=True), "", ""

def show_results(image):
    result, confidence = predict(image)
    return gr.Video.update(visible=False), result, confidence

# 讬爪讬专转 诪诪砖拽 Gradio 注诐 讗谞讬诪爪讬讜转
with gr.Blocks(css="""
    .gr-button { 
        background-color: #4CAF50; 
        color: white; 
        transition: background-color 0.3s ease;
    }
    .gr-button:hover { 
        background-color: #45a049; 
    }
""") as demo:
    with gr.Row():
        image_input = gr.Image(type="pil", label="Upload an image")
        animation = gr.Video("https://cdn-uploads.huggingface.co/production/uploads/66d6f1b3b50e35e1709bfdf7/x7Ud8PO9QPfmrTvBVcCKE.mp4", visible=False)
        
        output_label = gr.Textbox(label="Classification Result", interactive=False, visible=False)
        output_confidence = gr.Textbox(label="Confidence", interactive=False, visible=False)

    image_input.change(loading_animation, inputs=image_input, outputs=[animation, output_label, output_confidence])
    image_input.change(show_results, inputs=image_input, outputs=[animation, output_label, output_confidence])

    # 讛讜住驻转 讻驻转讜专
    submit_button = gr.Button("Submit")
    submit_button.click(predict, inputs=image_input, outputs=[output_label, output_confidence])

# 讛砖拽转 讛诪诪砖拽
demo.launch()