AdityaManojShinde commited on
Commit
8b9bf31
·
verified ·
1 Parent(s): 9672372

Fix: version issue

Browse files
Files changed (1) hide show
  1. app.py +35 -1
app.py CHANGED
@@ -1,9 +1,26 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
 
2
  import torch
3
  import gradio as gr
4
  from torchvision import transforms
5
  from model import HybridDeepfakeDetector
6
 
 
 
 
7
  model = HybridDeepfakeDetector()
8
  model.load_state_dict(
9
  torch.load("deepfake_detector_phase2.pth",
@@ -21,15 +38,26 @@ transform = transforms.Compose([
21
  )
22
  ])
23
 
 
 
 
24
  def predict(image):
 
 
 
 
25
  tensor = transform(image).unsqueeze(0)
26
  with torch.no_grad():
27
  prob = model(tensor).item()
 
28
  print(f"Raw prob: {prob:.4f}")
29
  label = "REAL" if prob > 0.5 else "FAKE"
30
  confidence = prob if label == "REAL" else 1 - prob
31
  return f"{label} ({confidence*100:.1f}% confident)"
32
 
 
 
 
33
  demo = gr.Interface(
34
  fn=predict,
35
  inputs=gr.Image(type="pil"),
@@ -38,4 +66,10 @@ demo = gr.Interface(
38
  description="Upload a face image to detect if it is real or AI-generated."
39
  )
40
 
41
- demo.launch()
 
 
 
 
 
 
 
1
+ import os
2
+ import warnings
3
+
4
+ # ==========================================
5
+ # 1. ENVIRONMENT CONFIGURATION (Must be at the top)
6
+ # ==========================================
7
+ # Bypass internal container proxies that block health checks
8
+ os.environ["NO_PROXY"] = "localhost,127.0.0.1,::1"
9
+ # Force Gradio to bind to all IPs on the correct port
10
+ os.environ["GRADIO_SERVER_NAME"] = "0.0.0.0"
11
+ os.environ["GRADIO_SERVER_PORT"] = "7860"
12
+
13
+ # Suppress the non-fatal timm legacy warnings to keep logs clean
14
+ warnings.filterwarnings("ignore", category=UserWarning, module="timm.models._factory")
15
 
16
  import torch
17
  import gradio as gr
18
  from torchvision import transforms
19
  from model import HybridDeepfakeDetector
20
 
21
+ # ==========================================
22
+ # 2. MODEL INITIALIZATION
23
+ # ==========================================
24
  model = HybridDeepfakeDetector()
25
  model.load_state_dict(
26
  torch.load("deepfake_detector_phase2.pth",
 
38
  )
39
  ])
40
 
41
+ # ==========================================
42
+ # 3. PREDICTION LOGIC
43
+ # ==========================================
44
  def predict(image):
45
+ # Safety check: Prevent crash if user clicks submit before image uploads
46
+ if image is None:
47
+ return "Please upload an image."
48
+
49
  tensor = transform(image).unsqueeze(0)
50
  with torch.no_grad():
51
  prob = model(tensor).item()
52
+
53
  print(f"Raw prob: {prob:.4f}")
54
  label = "REAL" if prob > 0.5 else "FAKE"
55
  confidence = prob if label == "REAL" else 1 - prob
56
  return f"{label} ({confidence*100:.1f}% confident)"
57
 
58
+ # ==========================================
59
+ # 4. UI & DEPLOYMENT
60
+ # ==========================================
61
  demo = gr.Interface(
62
  fn=predict,
63
  inputs=gr.Image(type="pil"),
 
66
  description="Upload a face image to detect if it is real or AI-generated."
67
  )
68
 
69
+ if __name__ == "__main__":
70
+ # Optimal launch parameters for Hugging Face Spaces
71
+ demo.launch(
72
+ share=False, # Let HF Spaces handle the public URL tunneling
73
+ ssr_mode=False, # Disable Server-Side Rendering to prevent localhost loop crashes
74
+ show_error=True # Surface actual code errors to the UI if they happen
75
+ )