Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,30 +1,46 @@
|
|
| 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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
|
| 29 |
# ืืฉืงืช ืืืืฉืง
|
| 30 |
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 |
+
# ืืื ืืืฉืื ืืืื (ืืืืื)
|
| 11 |
+
time.sleep(5) # ื ืืชื ืืฉื ืืช ืืช ืืื ืืขืืืื ืืกืืืืืฆืื
|
| 12 |
+
|
| 13 |
+
# ืืคืขืืช ืืืืื ืืื ืืกืืื ืืช ืืชืืื ื
|
| 14 |
label, confidence = model.predict(image)
|
| 15 |
result = "AI image" if label == 1 else "Real image"
|
| 16 |
+
|
| 17 |
return result, f"Confidence: {confidence:.2f}%"
|
| 18 |
|
| 19 |
+
def loading_animation(image):
|
| 20 |
+
# ืืฆืื ืืืืื ืขื ืฉืชืืฆืืช ืืืืื ืืชืงืืืช
|
| 21 |
+
return gr.Video.update(visible=True), gr.Textbox.update(visible=False), gr.Textbox.update(visible=False)
|
| 22 |
+
|
| 23 |
+
def show_results(image):
|
| 24 |
+
# ืืคืขืื ืืช ืืืืื ืืืืืืจ ืืช ืืชืืฆืืืช
|
| 25 |
+
result, confidence = predict(image)
|
| 26 |
+
return gr.Video.update(visible=False), result, confidence
|
| 27 |
+
|
| 28 |
+
# ืืฆืืจืช ืืืฉืง Gradio
|
| 29 |
+
with gr.Blocks() as demo:
|
| 30 |
+
with gr.Row():
|
| 31 |
+
# ืงืื ืชืืื ื
|
| 32 |
+
image_input = gr.Image(type="pil", label="Upload an image")
|
| 33 |
+
|
| 34 |
+
# ืืืืื ืื ืืืฆืื (ืืืกืชืจ ืืืชืืื)
|
| 35 |
+
animation = gr.Video("https://cdn-uploads.huggingface.co/production/uploads/66d6f1b3b50e35e1709bfdf7/x7Ud8PO9QPfmrTvBVcCKE.mp4", visible=False, elem_id="loading_video")
|
| 36 |
+
|
| 37 |
+
# ืชืืฆืืืช
|
| 38 |
+
output_label = gr.Textbox(label="Classification Result", visible=False)
|
| 39 |
+
output_confidence = gr.Textbox(label="Confidence", visible=False)
|
| 40 |
+
|
| 41 |
+
# ืืืจืืขืื
|
| 42 |
+
image_input.change(loading_animation, inputs=image_input, outputs=[animation, output_label, output_confidence])
|
| 43 |
+
image_input.submit(show_results, inputs=image_input, outputs=[animation, output_label, output_confidence])
|
| 44 |
|
| 45 |
# ืืฉืงืช ืืืืฉืง
|
| 46 |
demo.launch()
|