File size: 3,872 Bytes
a340f3e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
import modal
from utils.youtube_utils import download_youtube_video
from reports.pdf_report import generate_pdf_report

last_face_result = ""
last_voice_result = ""
last_video_results = []

verify_faces_remote = modal.Function.lookup("deepface-agent", "verify_faces_remote")
verify_voices_remote = modal.Function.lookup("deepface-agent", "verify_voices_remote")
verify_faces_in_video_remote = modal.Function.lookup(
    "deepface-agent", "verify_faces_in_video_remote"
)


def compare_faces(img1_path: str, img2_path: str) -> str:
    """Use this tool to compare to faces for a match
    Args:
        img1_path: The path to the first image
        img2_path: The path to the second image
    """
    global last_face_result

    # Read image files as bytes
    with open(img1_path, "rb") as f1, open(img2_path, "rb") as f2:
        img1_bytes = f1.read()
        img2_bytes = f2.read()

    result = verify_faces_remote.remote(img1_bytes, img2_bytes)
    result_text = ""

    if "error" in result:
        return f"❌ Error: {result['error']}"

    if result["verified"]:
        result_text = f"βœ… Match! Distance: {result['distance']:.4f} (Threshold: {result['threshold']})"
        last_face_result = result_text
        return result_text

    else:
        result_text = f"❌ No Match. Distance: {result['distance']:.4f} (Threshold: {result['threshold']})"
        last_face_result = result_text
        return result_text


def compare_voices(audio1: str, audio2: str) -> str:
    """Use this tool to compare two voices for a match
    Args:
        audio1: The path to the first audio file
        audio2: The path to the second audio file
    """
    global last_voice_result

    try:
        with open(audio1, "rb") as a1, open(audio2, "rb") as a2:
            audio1_bytes = a1.read()
            audio2_bytes = a2.read()

        result = verify_voices_remote.remote(audio1_bytes, audio2_bytes)
        result_text = ""

        if "error" in result:
            return f"❌ Error: {result['error']}"

        if result["match"]:
            result_text = f"βœ… Same speaker detected. Similarity: {result['similarity']} (Threshold: {result['threshold']})"
            last_voice_result = result_text
            return result_text
        else:
            result_text = f"❌ Different speakers. Similarity: {result['similarity']} (Threshold: {result['threshold']})"
            last_voice_result = result_text
            return result_text
    except Exception as e:
        return f"❌ Error reading audio files: {str(e)}"


def scan_video(video_file: str, ref_img: str, youtube_url="") -> str:
    """Use this tool to scan a video for deepfake face swaps
    Args:
        video_file: The path to the video file
        ref_img: The path to the reference image
        youtube_url: The YouTube URL (optional)
    """
    global last_video_results

    if youtube_url:
        try:
            video_file = download_youtube_video(youtube_url)
        except Exception as e:
            return f"❌ Error downloading YouTube video: {str(e)}"

    with open(video_file, "rb") as vf, open(ref_img, "rb") as rf:
        video_bytes = vf.read()
        ref_img_bytes = rf.read()
    try:
        results = verify_faces_in_video_remote.remote(video_bytes, ref_img_bytes, interval=30)
        
    except Exception as e:
        return f"❌ Error processing  video: {str(e)}"

    report = ""
    last_video_results = results
    for r in results:
        if "error" in r:
            report += f"\n⚠️ Frame {r['frame']}: {r['error']}"

        else:
            status = "βœ… Match" if r["verified"] else "❌ Mismatch"
            report += f"\nπŸ–Ό Frame {r['frame']}: {status} (Distance: {r['distance']})"

    return report
    
    

def generate_report():
    return generate_pdf_report(last_face_result, last_voice_result, last_video_results)