Jerry / app.py
hashirlodhi's picture
Update app.py
4d27d80 verified
import gradio as gr
from transformers import pipeline
import torch
# Initialize the deepfake detection pipeline
print("Loading Jerry's detection system...")
pipe = pipeline(
"image-classification",
model="prithivMLmods/Deep-Fake-Detector-v2-Model",
device=0 if torch.cuda.is_available() else -1
)
print("Jerry is ready!")
def detect_deepfake(image):
"""
Analyze an image to detect if it's a deepfake
"""
if image is None:
return "Please upload an image first!"
# Get predictions from the model
results = pipe(image)
# Format the results
output_text = "πŸ” **Jerry's Analysis Results:**\n\n"
for result in results:
label = result['label']
confidence = result['score'] * 100
# Create a visual confidence bar
bar_length = int(confidence / 5)
bar = "β–ˆ" * bar_length + "β–‘" * (20 - bar_length)
output_text += f"**{label}**: {confidence:.2f}%\n"
output_text += f"{bar}\n\n"
# Add a conclusion
top_result = results[0]
if top_result['score'] > 0.7:
certainty = "high"
elif top_result['score'] > 0.5:
certainty = "moderate"
else:
certainty = "low"
output_text += f"\n🎯 **Jerry's Verdict:** {top_result['label']} (with {certainty} confidence)"
return output_text
# Create the Gradio interface
with gr.Blocks(theme=gr.themes.Soft()) as demo:
gr.Markdown(
"""
# πŸ•΅οΈ Jerry - Deepfake Detector
### Your AI-Powered Image Authenticity Analyzer
Upload an image and let Jerry analyze it to determine if it's authentic or artificially generated!
"""
)
with gr.Row():
with gr.Column(scale=1):
image_input = gr.Image(
label="Upload Image for Analysis",
type="pil",
height=400
)
analyze_btn = gr.Button(
"πŸ” Analyze with Jerry",
variant="primary",
size="lg"
)
with gr.Column(scale=1):
output_text = gr.Markdown(
label="Detection Results",
value="Upload an image and click 'Analyze with Jerry' to begin!"
)
gr.Markdown(
"""
---
### πŸ’‘ Tips for Best Results:
- Upload clear, high-quality images
- Works best with photos of faces or people
- Supports common image formats (JPG, PNG, etc.)
### ⚠️ Important Note:
Jerry provides analysis based on AI detection patterns. Results should be used as guidance, not absolute proof.
"""
)
# Connect the button to the function
analyze_btn.click(
fn=detect_deepfake,
inputs=image_input,
outputs=output_text
)
# Also allow Enter key or automatic analysis
image_input.change(
fn=detect_deepfake,
inputs=image_input,
outputs=output_text
)
# Launch the app
if __name__ == "__main__":
demo.launch(
server_name="0.0.0.0",
server_port=7860,
share=False
)