Yulu Fu commited on
Commit
33b18b9
1 Parent(s): 4510960

Attempt to add image model

Browse files
Files changed (1) hide show
  1. app.py +41 -14
app.py CHANGED
@@ -1,14 +1,21 @@
1
  import gradio as gr
2
  from transformers import pipeline
3
 
4
- # Load the model using pipeline
5
- pipe = pipeline("audio-classification", model="MelodyMachine/Deepfake-audio-detection-V2")
 
6
 
7
  # Define the prediction function
8
- def predict(audio):
9
- print("Audio file received:", audio) # Debugging statement
10
  try:
11
- result = pipe(audio)
 
 
 
 
 
 
12
  print("Raw prediction result:", result) # Debugging statement
13
  # Convert the result to the expected format
14
  output = {item['label']: item['score'] for item in result}
@@ -18,14 +25,34 @@ def predict(audio):
18
  print("Error during prediction:", e) # Debugging statement
19
  return {"error": str(e)}
20
 
 
 
 
 
 
 
 
 
 
21
  # Create the Gradio interface
22
- iface = gr.Interface(
23
- fn=predict,
24
- inputs=gr.Audio(type="filepath"),
25
- outputs=gr.Label(),
26
- title="Testing Deepfake Audio Detection Simple Interface",
27
- description="Upload an audio file or record your voice to detect if the audio is a deepfake."
28
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
29
 
30
- # Launch the interface
31
- iface.launch()
 
1
  import gradio as gr
2
  from transformers import pipeline
3
 
4
+ # Load the models using pipeline
5
+ audio_model = pipeline("audio-classification", model="MelodyMachine/Deepfake-audio-detection-V2")
6
+ image_model = pipeline("image-classification", model="dima806/deepfake_vs_real_image_detection")
7
 
8
  # Define the prediction function
9
+ def predict(data, model_choice):
10
+ print("Data received:", data) # Debugging statement
11
  try:
12
+ if model_choice == "Audio Deepfake Detection":
13
+ result = audio_model(data)
14
+ elif model_choice == "Image Deepfake Detection":
15
+ result = image_model(data)
16
+ else:
17
+ return {"error": "Invalid model choice"}
18
+
19
  print("Raw prediction result:", result) # Debugging statement
20
  # Convert the result to the expected format
21
  output = {item['label']: item['score'] for item in result}
 
25
  print("Error during prediction:", e) # Debugging statement
26
  return {"error": str(e)}
27
 
28
+ # Define the interface based on the selected model
29
+ def update_interface(model_choice):
30
+ if model_choice == "Audio Deepfake Detection":
31
+ return gr.Audio(type="filepath"), gr.Label()
32
+ elif model_choice == "Image Deepfake Detection":
33
+ return gr.Image(type="filepath"), gr.Label()
34
+ else:
35
+ return None, None
36
+
37
  # Create the Gradio interface
38
+ with gr.Blocks() as iface:
39
+ model_choice = gr.Radio(choices=["Audio Deepfake Detection", "Image Deepfake Detection"], label="Select Model", value="Audio Deepfake Detection")
40
+ input_component, output_component = update_interface(model_choice.value)
41
+
42
+ def update_inputs(model_choice):
43
+ input_component, output_component = update_interface(model_choice)
44
+ input_placeholder.update(visible=False)
45
+ output_placeholder.update(visible=False)
46
+ input_placeholder.update(visible=True, component=input_component)
47
+ output_placeholder.update(visible=True, component=output_component)
48
+
49
+ input_placeholder = gr.Placeholder(gr.Component, visible=True)
50
+ output_placeholder = gr.Placeholder(gr.Component, visible=True)
51
+
52
+ model_choice.change(fn=update_inputs, inputs=model_choice, outputs=[input_placeholder, output_placeholder])
53
+
54
+ submit_button = gr.Button("Submit")
55
+ submit_button.click(fn=predict, inputs=[input_placeholder, model_choice], outputs=output_placeholder)
56
+
57
+ iface.launch()
58