Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,8 +4,9 @@ import importlib.util
|
|
| 4 |
import gradio as gr
|
| 5 |
import logging
|
| 6 |
from moviepy.editor import VideoFileClip
|
|
|
|
|
|
|
| 7 |
|
| 8 |
-
# Function to truncate video to 15 seconds
|
| 9 |
def truncate_video(video_file):
|
| 10 |
"""Truncates video to 15 seconds and saves it as a temporary file."""
|
| 11 |
clip = VideoFileClip(video_file)
|
|
@@ -14,25 +15,20 @@ def truncate_video(video_file):
|
|
| 14 |
truncated_clip.write_videofile(truncated_video_file, codec="libx264", audio_codec="aac")
|
| 15 |
return truncated_video_file
|
| 16 |
|
| 17 |
-
# Clone the GitHub repository containing the backend
|
| 18 |
def clone_repo():
|
| 19 |
"""Clone the GitHub repository containing the backend."""
|
| 20 |
-
repo_url = "https://github.com/NeeravSood/AllMark-MVP.git" # Update
|
| 21 |
repo_path = "./repository"
|
| 22 |
|
| 23 |
-
# Retrieve the GitHub Personal Access Token (GITHUB_PAT) from environment variables
|
| 24 |
github_pat = os.getenv("GITHUB_PAT")
|
| 25 |
if not github_pat:
|
| 26 |
raise RuntimeError("GitHub Personal Access Token (GITHUB_PAT) not found in environment variables.")
|
| 27 |
-
|
| 28 |
-
# Modify the repository URL to include the token for authentication
|
| 29 |
authenticated_repo_url = f"https://{github_pat}@github.com/NeeravSood/AllMark-MVP.git"
|
| 30 |
|
| 31 |
if os.path.exists(repo_path):
|
| 32 |
print("Repository already cloned.")
|
| 33 |
else:
|
| 34 |
try:
|
| 35 |
-
# Clone the repository using the authenticated URL
|
| 36 |
subprocess.run(
|
| 37 |
["git", "clone", authenticated_repo_url, repo_path],
|
| 38 |
check=True,
|
|
@@ -60,29 +56,21 @@ def import_backend_script(script_name):
|
|
| 60 |
logging.error(f"Error importing backend script: {str(e)}")
|
| 61 |
raise RuntimeError(f"Failed to import backend script: {str(e)}")
|
| 62 |
|
| 63 |
-
# Clone the repository and import the backend module
|
| 64 |
clone_repo()
|
| 65 |
-
backend = import_backend_script("app.py")
|
| 66 |
-
|
| 67 |
-
# Initialize the analyzer instance from the imported module
|
| 68 |
-
analyzer = backend.DeepfakeAnalyzer() # Use the imported module's class or function
|
| 69 |
-
|
| 70 |
-
# Define the Gradio function to analyze the video
|
| 71 |
-
import json # Ensure imports include json for dictionary-to-JSON string handling
|
| 72 |
|
| 73 |
def analyze_video(video_file):
|
| 74 |
try:
|
| 75 |
-
# Truncate the video to 15 seconds
|
| 76 |
truncated_video = truncate_video(video_file)
|
| 77 |
-
|
| 78 |
-
# Pass the truncated video to the analyzer
|
| 79 |
results = analyzer.analyze_media(truncated_video)
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
|
|
|
|
|
|
|
|
|
| 83 |
analysis_result = "genuine/original" if combined_probability < 50 else "a deepfake"
|
| 84 |
-
|
| 85 |
-
# Prepare JSON output
|
| 86 |
output = {
|
| 87 |
"message": f"According to our analysis, the video you uploaded appears to be {analysis_result} "
|
| 88 |
f"with a {combined_probability:.2f}% probability. "
|
|
@@ -92,12 +80,8 @@ def analyze_video(video_file):
|
|
| 92 |
return output
|
| 93 |
|
| 94 |
except Exception as e:
|
| 95 |
-
# Log the error and return a JSON-compatible error message
|
| 96 |
logging.error(f"Error during analysis: {e}")
|
| 97 |
return {"error": "An error occurred during video analysis. Please check your input and try again."}
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
# Define the Gradio interface with json output to handle dictionaries
|
| 101 |
interface = gr.Interface(
|
| 102 |
fn=analyze_video,
|
| 103 |
inputs=gr.Video(label="Upload Video"),
|
|
@@ -106,6 +90,5 @@ interface = gr.Interface(
|
|
| 106 |
description="Upload a video to analyze for deepfake content. N.B. - Only mp4 files supported for now. Analysis usually takes between 1 to 5 minutes."
|
| 107 |
)
|
| 108 |
|
| 109 |
-
# Launch Gradio app
|
| 110 |
if __name__ == "__main__":
|
| 111 |
interface.launch()
|
|
|
|
| 4 |
import gradio as gr
|
| 5 |
import logging
|
| 6 |
from moviepy.editor import VideoFileClip
|
| 7 |
+
import json
|
| 8 |
+
|
| 9 |
|
|
|
|
| 10 |
def truncate_video(video_file):
|
| 11 |
"""Truncates video to 15 seconds and saves it as a temporary file."""
|
| 12 |
clip = VideoFileClip(video_file)
|
|
|
|
| 15 |
truncated_clip.write_videofile(truncated_video_file, codec="libx264", audio_codec="aac")
|
| 16 |
return truncated_video_file
|
| 17 |
|
|
|
|
| 18 |
def clone_repo():
|
| 19 |
"""Clone the GitHub repository containing the backend."""
|
| 20 |
+
repo_url = "https://github.com/NeeravSood/AllMark-MVP.git" # Update when changing
|
| 21 |
repo_path = "./repository"
|
| 22 |
|
|
|
|
| 23 |
github_pat = os.getenv("GITHUB_PAT")
|
| 24 |
if not github_pat:
|
| 25 |
raise RuntimeError("GitHub Personal Access Token (GITHUB_PAT) not found in environment variables.")
|
|
|
|
|
|
|
| 26 |
authenticated_repo_url = f"https://{github_pat}@github.com/NeeravSood/AllMark-MVP.git"
|
| 27 |
|
| 28 |
if os.path.exists(repo_path):
|
| 29 |
print("Repository already cloned.")
|
| 30 |
else:
|
| 31 |
try:
|
|
|
|
| 32 |
subprocess.run(
|
| 33 |
["git", "clone", authenticated_repo_url, repo_path],
|
| 34 |
check=True,
|
|
|
|
| 56 |
logging.error(f"Error importing backend script: {str(e)}")
|
| 57 |
raise RuntimeError(f"Failed to import backend script: {str(e)}")
|
| 58 |
|
|
|
|
| 59 |
clone_repo()
|
| 60 |
+
backend = import_backend_script("app.py")
|
| 61 |
+
analyzer = backend.DeepfakeAnalyzer()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 62 |
|
| 63 |
def analyze_video(video_file):
|
| 64 |
try:
|
|
|
|
| 65 |
truncated_video = truncate_video(video_file)
|
|
|
|
|
|
|
| 66 |
results = analyzer.analyze_media(truncated_video)
|
| 67 |
+
combined_assessment = results.get('combined_assessment', 0)
|
| 68 |
+
if isinstance(combined_assessment, (int, float)):
|
| 69 |
+
combined_probability = combined_assessment
|
| 70 |
+
else:
|
| 71 |
+
combined_probability = 100 if combined_assessment == "Deepfake" else 0
|
| 72 |
+
|
| 73 |
analysis_result = "genuine/original" if combined_probability < 50 else "a deepfake"
|
|
|
|
|
|
|
| 74 |
output = {
|
| 75 |
"message": f"According to our analysis, the video you uploaded appears to be {analysis_result} "
|
| 76 |
f"with a {combined_probability:.2f}% probability. "
|
|
|
|
| 80 |
return output
|
| 81 |
|
| 82 |
except Exception as e:
|
|
|
|
| 83 |
logging.error(f"Error during analysis: {e}")
|
| 84 |
return {"error": "An error occurred during video analysis. Please check your input and try again."}
|
|
|
|
|
|
|
|
|
|
| 85 |
interface = gr.Interface(
|
| 86 |
fn=analyze_video,
|
| 87 |
inputs=gr.Video(label="Upload Video"),
|
|
|
|
| 90 |
description="Upload a video to analyze for deepfake content. N.B. - Only mp4 files supported for now. Analysis usually takes between 1 to 5 minutes."
|
| 91 |
)
|
| 92 |
|
|
|
|
| 93 |
if __name__ == "__main__":
|
| 94 |
interface.launch()
|