Spaces:
Sleeping
Sleeping
File size: 1,875 Bytes
373e186 49babfe 95d9553 93a4013 49babfe 373e186 49babfe 95d9553 3f9b377 95d9553 3f9b377 a64f4ed 95d9553 5dde09c 40ee638 |
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 |
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):
# 讝诪谉 讞讬砖讜讘 诪讜讚诇 (诪讚讜诪讛)
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), gr.Textbox.update(visible=False), gr.Textbox.update(visible=False)
def show_results(image):
# 诪驻注讬诇 讗转 讛诪讜讚诇 讜诪讞讝讬专 讗转 讛转讜爪讗讜转
result, confidence = predict(image)
return gr.Video.update(visible=False), result, confidence
# 讬爪讬专转 诪诪砖拽 Gradio
with gr.Blocks() 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, elem_id="loading_video")
# 转讜爪讗讜转
output_label = gr.Textbox(label="Classification Result", visible=False)
output_confidence = gr.Textbox(label="Confidence", visible=False)
# 讗讬专讜注讬诐
image_input.change(loading_animation, inputs=image_input, outputs=[animation, output_label, output_confidence])
image_input.submit(show_results, inputs=image_input, outputs=[animation, output_label, output_confidence])
# 讛砖拽转 讛诪诪砖拽
demo.launch()
|