litav commited on
Commit
d23c767
ยท
verified ยท
1 Parent(s): c0cf78c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +44 -16
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
- # Define the Gradio interface
16
- demo = gr.Interface(
17
- fn=predict,
18
- inputs=gr.Image(type="pil"),
19
- outputs=[gr.Textbox(), gr.Textbox()],
20
- title="Vision Transformer Model",
21
- description="Upload an image to classify it using the Vision Transformer model.",
22
- theme="compact",
23
- live=True
24
- )
25
-
26
- # Launch the Gradio interface
27
- 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
+ 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()