File size: 3,988 Bytes
3a3c2be
2dc47c6
 
cb82c24
2dc47c6
3a3c2be
2dc47c6
 
 
 
 
3a3c2be
2dc47c6
 
 
 
cb82c24
2dc47c6
 
cb82c24
2dc47c6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c4c84f3
 
cb82c24
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3a3c2be
cb82c24
 
 
 
 
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
import os
import subprocess
import importlib.util
import gradio as gr
import logging

# Clone the GitHub repository containing the backend
def clone_repo():
    """Clone the GitHub repository containing the backend."""
    repo_url = "https://github.com/NeeravSood/AllMark-MVP.git"  # Update if necessary
    repo_path = "./repository"

    # Retrieve the GitHub Personal Access Token (GITHUB_PAT) from environment variables
    github_pat = os.getenv("GITHUB_PAT")
    if not github_pat:
        raise RuntimeError("GitHub Personal Access Token (GITHUB_PAT) not found in environment variables.")

    # Modify the repository URL to include the token for authentication
    authenticated_repo_url = f"https://{github_pat}@github.com/NeeravSood/AllMark-MVP.git"

    if os.path.exists(repo_path):
        print("Repository already cloned.")
    else:
        try:
            # Clone the repository using the authenticated URL
            subprocess.run(
                ["git", "clone", authenticated_repo_url, repo_path],
                check=True,
                text=True,
                capture_output=True
            )
            print("Repository cloned successfully.")
        except subprocess.CalledProcessError as e:
            print("Output:", e.stdout)
            print("Error:", e.stderr)
            raise RuntimeError(f"Failed to clone repository: {e.stderr}")

def import_backend_script(script_name):
    """Dynamically import the backend script."""
    try:
        script_path = os.path.join("./repository", script_name)
        if not os.path.exists(script_path):
            raise FileNotFoundError(f"Script {script_name} not found in the repository.")
        
        spec = importlib.util.spec_from_file_location("backend_module", script_path)
        backend_module = importlib.util.module_from_spec(spec)
        spec.loader.exec_module(backend_module)
        return backend_module
    except Exception as e:
        logging.error(f"Error importing backend script: {str(e)}")
        raise RuntimeError(f"Failed to import backend script: {str(e)}")

# Clone the repository and import the backend module
clone_repo()
backend = import_backend_script("app.py")  # Import app.py from the cloned repository

# Initialize the analyzer instance from the imported module
analyzer = backend.DeepfakeAnalyzer()  # Use the imported module's class or function

# Define the Gradio function to analyze the video
def analyze_video(video_file):
    results = analyzer.analyze_media(video_file)
    combined_probability = results['combined_assessment']
    audio_analysis = results["audio_analysis"]
    video_probability = results['video_analysis']['probability']
    frame_count = len(results['video_analysis']['frame_results'])

    # Format the output for display
    output = {
        "Audio Analysis": audio_analysis,
        "Deepfake Probability (Combined Assessment)": f"{combined_probability:.2f}%",
        "Video Analysis": {
            "Deepfake Probability": f"{video_probability:.4f}",
            "Frames Analyzed": frame_count,
            "Frame Analysis Summary": [
                {
                    "Frame Number": frame_result["frame_number"],
                    "Noise Level": frame_result["noise"],
                    "Edge Density": frame_result["edge_density"],
                    "Color Consistency": frame_result["color_consistency"],
                    "Temporal Difference": frame_result["temporal_difference"],
                    "Probability": frame_result["probability"]
                }
                for frame_result in results['video_analysis']['frame_results']
            ]
        }
    }
    return output

# Define the Gradio interface
interface = gr.Interface(
    fn=analyze_video,
    inputs=gr.Video(label="Upload Video"),
    outputs="json",
    title="Deepfake Analyzer",
    description="Upload a video to analyze for deepfake content."
)

# Launch Gradio app
if __name__ == "__main__":
    interface.launch()