Experiment / app.py
criticalDanger's picture
Update app.py
3dc4d0a verified
raw
history blame
1.72 kB
import gradio as gr
from transformers import pipeline
# Load the models using pipeline
image_model = pipeline("image-classification", model="dima806/deepfake_vs_real_image_detection")
# Define the prediction function
def predict(audio, image, model_choice):
print("Data received:", audio if model_choice == "Audio Deepfake Detection" else image) # Debugging statement
try:
if model_choice == "Audio Deepfake Detection":
result = audio_model(audio)
elif model_choice == "Image Deepfake Detection":
result = image_model(image)
else:
return {"error": "Invalid model choice"}
print("Raw prediction result:", result) # Debugging statement
# Convert the result to the expected format
output = {item['label']: item['score'] for item in result}
print("Formatted prediction result:", output) # Debugging statement
return output
except Exception as e:
print("Error during prediction:", e) # Debugging statement
return {"error": str(e)}
# Update interface based on the selected model
def update_interface(model_choice):
if model_choice == "Audio Deepfake Detection":
return gr.update(visible=True), gr.update(visible=False)
elif model_choice == "Image Deepfake Detection":
return gr.update(visible=False), gr.update(visible=True)
# Create Gradio interface
with gr.Blocks() as iface:
image_input = gr.Image(type="filepath", label="Upload Image File", visible=False)
output = gr.Label()
submit_button = gr.Button("Submit")
submit_button.click(fn=predict, inputs=[audio_input, image_input, model_choice], outputs=output)
iface.launch()