litav commited on
Commit
95d9553
ยท
verified ยท
1 Parent(s): 40ee638

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +31 -15
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
- # ื™ืฆื™ืจืช ืžืžืฉืง Gradio ืขื ื•ื™ื“ืื• ืฉืžื•ืฆื’ ื‘ื–ืžืŸ ื—ื™ืฉื•ื‘ ื”ืžื•ื“ืœ
15
- demo = gr.Interface(
16
- fn=predict,
17
- inputs=gr.Image(type="pil"),
18
- outputs=[gr.Textbox(), gr.Textbox()],
19
- title="Vision Transformer Model",
20
- description="Upload an image to classify it as AI-generated or Real.",
21
- live=True, # ืžืืคืฉืจ ืœื”ืฆื™ื’ ืกื˜ื˜ื•ืก ื‘ื–ืžืŸ ืืžืช
22
- theme="compact", # ืืคืฉืจื™ ืœื”ื•ืกื™ืฃ ืขื™ืฆื•ื‘ ืžืงืฆื•ืขื™
23
- css=".loading-message { display: none; }"
24
- )
25
-
26
- # ื”ื’ื“ืจืช ื”ืกืจื˜ื•ืŸ ืฉื™ื•ืคื™ืข ื‘ื–ืžืŸ ื—ื™ืฉื•ื‘
27
- demo.loading = "https://cdn-uploads.huggingface.co/production/uploads/66d6f1b3b50e35e1709bfdf7/x7Ud8PO9QPfmrTvBVcCKE.mp4" # ื”ื•ืกืฃ ืืช ืฉื ืงื•ื‘ืฅ ื”ื•ื•ื™ื“ืื•
 
 
 
 
 
 
 
 
 
 
 
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()