Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,33 +1,34 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from PIL import Image
|
| 3 |
from vit_model_test import CustomModel
|
|
|
|
| 4 |
|
| 5 |
# Initialize the model
|
| 6 |
model = CustomModel()
|
| 7 |
|
| 8 |
def predict(image: Image.Image):
|
| 9 |
-
|
|
|
|
| 10 |
label, confidence = model.predict(image)
|
| 11 |
result = "AI image" if label == 1 else "Real image"
|
| 12 |
return result, f"Confidence: {confidence:.2f}%"
|
| 13 |
|
| 14 |
-
|
| 15 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
"https://cdn-uploads.huggingface.co/production/uploads/66d6f1b3b50e35e1709bfdf7/x7Ud8PO9QPfmrTvBVcCKE.mp4",
|
| 20 |
-
)
|
| 21 |
-
|
| 22 |
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
outputs=[
|
| 28 |
-
|
| 29 |
-
description="Upload an image to classify it using the Vision Transformer model."
|
| 30 |
-
)
|
| 31 |
|
| 32 |
# Launch the Gradio interface
|
| 33 |
-
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from PIL import Image
|
| 3 |
from vit_model_test import CustomModel
|
| 4 |
+
import time
|
| 5 |
|
| 6 |
# Initialize the model
|
| 7 |
model = CustomModel()
|
| 8 |
|
| 9 |
def predict(image: Image.Image):
|
| 10 |
+
# 住讬诪讜诇爪讬讛 砖诇 讝诪谉 注讬讘讜讚 (诇诪砖诇 5 砖谞讬讜转)
|
| 11 |
+
time.sleep(5) # 讗驻砖专 诇砖谞讜转 讗转 讛讝诪谉 诇驻讬 讛爪讜专讱
|
| 12 |
label, confidence = model.predict(image)
|
| 13 |
result = "AI image" if label == 1 else "Real image"
|
| 14 |
return result, f"Confidence: {confidence:.2f}%"
|
| 15 |
|
| 16 |
+
# Define the Gradio interface
|
| 17 |
+
with gr.Blocks() as demo:
|
| 18 |
+
image_input = gr.Image(type="pil", label="Upload an image")
|
| 19 |
+
animation = gr.Video("https://cdn-uploads.huggingface.co/production/uploads/66d6f1b3b50e35e1709bfdf7/x7Ud8PO9QPfmrTvBVcCKE.mp4", visible=False) # 讛讜住驻转 讛讜讜讬讚讗讜
|
| 20 |
+
output_label = gr.Textbox(label="Classification Result", interactive=False)
|
| 21 |
+
output_confidence = gr.Textbox(label="Confidence", interactive=False)
|
| 22 |
|
| 23 |
+
def show_animation(image):
|
| 24 |
+
return animation.update(visible=True), "", "" # 诇讛爪讬讙 讗转 讛讗谞讬诪爪讬讛
|
|
|
|
|
|
|
|
|
|
| 25 |
|
| 26 |
+
def hide_animation(image):
|
| 27 |
+
result, confidence = predict(image)
|
| 28 |
+
return animation.update(visible=False), result, confidence # 诇讛住转讬专 讗转 讛讗谞讬诪爪讬讛
|
| 29 |
+
|
| 30 |
+
image_input.change(show_animation, inputs=image_input, outputs=[animation, output_label, output_confidence])
|
| 31 |
+
image_input.change(hide_animation, inputs=image_input, outputs=[animation, output_label, output_confidence])
|
|
|
|
|
|
|
| 32 |
|
| 33 |
# Launch the Gradio interface
|
| 34 |
+
demo.launch()
|