NeeravS commited on
Commit
9b30919
·
verified ·
1 Parent(s): 660a68d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +17 -10
app.py CHANGED
@@ -4,10 +4,12 @@ import importlib.util
4
  import gradio as gr
5
  import logging
6
  from moviepy.editor import VideoFileClip
7
- import json
8
- import spaces
9
  import torch
10
 
 
 
 
 
11
  torch.set_num_threads(1)
12
  torch.set_num_interop_threads(1)
13
  torch.use_deterministic_algorithms(True)
@@ -16,8 +18,6 @@ torch.backends.cudnn.benchmark = False
16
  torch.backends.cuda.matmul.allow_tf32 = False
17
  torch.backends.cudnn.allow_tf32 = False
18
 
19
- ACCESS_KEY = os.getenv("ACCESS_KEY")
20
-
21
  def truncate_video(video_file):
22
  """Truncates video to 15 seconds and saves it as a temporary file."""
23
  clip = VideoFileClip(video_file)
@@ -28,7 +28,7 @@ def truncate_video(video_file):
28
 
29
  def clone_repo():
30
  """Clone the GitHub repository containing the backend."""
31
- repo_url = "https://github.com/NeeravSood/AllMark-MVP.git" # Update when changing
32
  repo_path = "./repository"
33
 
34
  github_pat = os.getenv("GITHUB_PAT")
@@ -67,15 +67,22 @@ def import_backend_script(script_name):
67
  logging.error(f"Error importing backend script: {str(e)}")
68
  raise RuntimeError(f"Failed to import backend script: {str(e)}")
69
 
 
70
  clone_repo()
71
  backend = import_backend_script("app.py")
72
  analyzer = backend.DeepfakeAnalyzer()
73
 
74
- @spaces.GPU(duration=1000)
75
  def analyze_video(video_file):
76
- if access_key != ACCESS_KEY:
77
- logging.error("Invalid access key provided.")
78
- return {"error": "Unauthorized access. Invalid access key."}
 
 
 
 
 
 
 
79
 
80
  try:
81
  truncated_video = truncate_video(video_file)
@@ -102,7 +109,7 @@ def analyze_video(video_file):
102
 
103
  interface = gr.Interface(
104
  fn=analyze_video,
105
- inputs=[gr.Video(label="Upload Video"), gr.Textbox(label="Access Key", type="password")],
106
  outputs="json",
107
  title="AllMark - Deepfake Analyzer",
108
  description="Upload a video to analyze. N.B. - Only mp4 files. Processing time 1-10 minutes. For any false negatives, please contact the publisher for verification. Incognito Mode Recommended"
 
4
  import gradio as gr
5
  import logging
6
  from moviepy.editor import VideoFileClip
 
 
7
  import torch
8
 
9
+ # Retrieve the access key from environment variables (Hugging Face secret)
10
+ ACCESS_KEY = os.getenv("ACCESS_KEY")
11
+
12
+ # Configure PyTorch for deterministic behavior
13
  torch.set_num_threads(1)
14
  torch.set_num_interop_threads(1)
15
  torch.use_deterministic_algorithms(True)
 
18
  torch.backends.cuda.matmul.allow_tf32 = False
19
  torch.backends.cudnn.allow_tf32 = False
20
 
 
 
21
  def truncate_video(video_file):
22
  """Truncates video to 15 seconds and saves it as a temporary file."""
23
  clip = VideoFileClip(video_file)
 
28
 
29
  def clone_repo():
30
  """Clone the GitHub repository containing the backend."""
31
+ repo_url = "https://github.com/NeeravSood/AllMark-MVP.git"
32
  repo_path = "./repository"
33
 
34
  github_pat = os.getenv("GITHUB_PAT")
 
67
  logging.error(f"Error importing backend script: {str(e)}")
68
  raise RuntimeError(f"Failed to import backend script: {str(e)}")
69
 
70
+ # Run repository setup and model import
71
  clone_repo()
72
  backend = import_backend_script("app.py")
73
  analyzer = backend.DeepfakeAnalyzer()
74
 
 
75
  def analyze_video(video_file):
76
+ # Validate the access key at runtime
77
+ if ACCESS_KEY is None:
78
+ logging.error("Access key not set in environment variables.")
79
+ return {"error": "Server misconfiguration. Access key not set."}
80
+
81
+ # Assuming you want to enforce a predefined, hardcoded key internally
82
+ expected_key = "expected_internal_key_here"
83
+ if ACCESS_KEY != expected_key:
84
+ logging.error("Unauthorized access attempt.")
85
+ return {"error": "Unauthorized access. Invalid key provided."}
86
 
87
  try:
88
  truncated_video = truncate_video(video_file)
 
109
 
110
  interface = gr.Interface(
111
  fn=analyze_video,
112
+ inputs=gr.Video(label="Upload Video"),
113
  outputs="json",
114
  title="AllMark - Deepfake Analyzer",
115
  description="Upload a video to analyze. N.B. - Only mp4 files. Processing time 1-10 minutes. For any false negatives, please contact the publisher for verification. Incognito Mode Recommended"