Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,27 +1,55 @@
|
|
| 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 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
+
time.sleep(5) # ืกืืืืืฆืื ืฉื ืืื ืขืืืื
|
| 11 |
label, confidence = model.predict(image)
|
| 12 |
result = "AI image" if label == 1 else "Real image"
|
| 13 |
return result, f"Confidence: {confidence:.2f}%"
|
| 14 |
+
|
| 15 |
+
def loading_animation(image):
|
| 16 |
+
return gr.Video.update(visible=True), "", ""
|
| 17 |
+
|
| 18 |
+
def show_results(image):
|
| 19 |
+
result, confidence = predict(image)
|
| 20 |
+
return gr.Video.update(visible=False), result, confidence
|
| 21 |
+
|
| 22 |
+
# ืืฆืืจืช ืืืฉืง Gradio ืขื ืื ืืืฆืืืช
|
| 23 |
+
with gr.Blocks(css="""
|
| 24 |
+
.gr-button {
|
| 25 |
+
background-color: #4CAF50;
|
| 26 |
+
color: white;
|
| 27 |
+
transition: background-color 0.3s ease;
|
| 28 |
+
}
|
| 29 |
+
.gr-button:hover {
|
| 30 |
+
background-color: #45a049;
|
| 31 |
+
}
|
| 32 |
+
.fade-in {
|
| 33 |
+
animation: fadeIn 0.5s ease-in;
|
| 34 |
+
}
|
| 35 |
+
@keyframes fadeIn {
|
| 36 |
+
from { opacity: 0; }
|
| 37 |
+
to { opacity: 1; }
|
| 38 |
+
}
|
| 39 |
+
""") as demo:
|
| 40 |
+
with gr.Row():
|
| 41 |
+
image_input = gr.Image(type="pil", label="Upload an image")
|
| 42 |
+
animation = gr.Video("https://cdn-uploads.huggingface.co/production/uploads/66d6f1b3b50e35e1709bfdf7/x7Ud8PO9QPfmrTvBVcCKE.mp4", visible=False)
|
| 43 |
+
|
| 44 |
+
output_label = gr.Textbox(label="Classification Result", interactive=False, visible=False)
|
| 45 |
+
output_confidence = gr.Textbox(label="Confidence", interactive=False, visible=False)
|
| 46 |
+
|
| 47 |
+
image_input.change(loading_animation, inputs=image_input, outputs=[animation, output_label, output_confidence])
|
| 48 |
+
image_input.change(show_results, inputs=image_input, outputs=[animation, output_label, output_confidence])
|
| 49 |
+
|
| 50 |
+
# ืืืกืคืช ืืคืชืืจ ืขื ืื ืืืฆืื
|
| 51 |
+
submit_button = gr.Button("Submit", css="fade-in") # ืืคืชืืจ ืขื ืืคืงื ืฉื fade-in
|
| 52 |
+
submit_button.click(predict, inputs=image_input, outputs=[output_label, output_confidence])
|
| 53 |
+
|
| 54 |
+
# ืืฉืงืช ืืืืฉืง
|
| 55 |
+
demo.launch()
|