RGriya commited on
Commit
7a3f58c
Β·
verified Β·
1 Parent(s): 45a6ab4

Delete app.py

Browse files
Files changed (1) hide show
  1. app.py +0 -105
app.py DELETED
@@ -1,105 +0,0 @@
1
- import gradio as gr
2
- from transformers import pipeline
3
- import torch
4
- import numpy as np
5
- import soundfile as sf
6
- import io
7
- import os
8
-
9
- MODEL = "mo-thecreator/Deepfake-audio-detection"
10
-
11
- print(f"Loading model: {MODEL}")
12
- classifier = pipeline(
13
- "audio-classification",
14
- model=MODEL,
15
- device=0 if torch.cuda.is_available() else -1
16
- )
17
- print("Model loaded SUCCESS")
18
-
19
-
20
- def detect(audio):
21
- """
22
- Called by Gradio with audio input.
23
- When called via API with a file upload, `audio` can be:
24
- - A string path: "/tmp/gradio/xxx/audio.wav"
25
- - A dict: {"path": "/tmp/gradio/xxx/audio.wav", "orig_name": "audio.wav", ...}
26
- - A tuple: (sample_rate, numpy_array) ← from gr.Audio(type="numpy")
27
- Returns: {"fake": 0.97, "real": 0.03}
28
- """
29
- print(f"detect() called: type={type(audio).__name__} value={repr(audio)[:200]}")
30
-
31
- if audio is None:
32
- return {"error": 1.0}
33
-
34
- # ── Resolve the file path ──────────────────────────────────────────────
35
- file_path = None
36
- np_data = None
37
- sr = 16000
38
-
39
- if isinstance(audio, dict):
40
- # Gradio 5 passes file objects as dicts
41
- file_path = audio.get("path") or audio.get("name") or audio.get("url")
42
- elif isinstance(audio, str):
43
- file_path = audio
44
- elif isinstance(audio, tuple) and len(audio) == 2:
45
- # gr.Audio(type="numpy") β†’ (sample_rate, ndarray)
46
- sr, np_data = audio
47
- if isinstance(np_data, np.ndarray):
48
- np_data = np_data.astype(np.float32)
49
- if np_data.max() > 1.0:
50
- np_data /= 32768.0
51
- else:
52
- print(f"Unknown audio type: {type(audio)}")
53
- return {"error": 1.0}
54
-
55
- # ── Load from file path if we have one ─────────────────────────────────
56
- if file_path and os.path.exists(file_path):
57
- print(f"Loading file: {file_path}")
58
- np_data, sr = sf.read(file_path, dtype="float32", always_2d=False)
59
- elif file_path:
60
- print(f"File not found: {file_path}")
61
- return {"error": 1.0}
62
-
63
- if np_data is None:
64
- print("No audio data resolved")
65
- return {"error": 1.0}
66
-
67
- # ── Pre-process ────────────────────────────────────────────────────────
68
- if np_data.ndim > 1:
69
- np_data = np_data.mean(axis=1)
70
-
71
- if np.abs(np_data).max() > 1.0:
72
- np_data /= 32768.0
73
-
74
- if sr != 16000:
75
- try:
76
- import librosa
77
- np_data = librosa.resample(np_data, orig_sr=sr, target_sr=16000)
78
- except Exception as e:
79
- print(f"Resample warning: {e}")
80
-
81
- print(f"Audio: {len(np_data)} samples @ 16kHz, range=[{np_data.min():.3f},{np_data.max():.3f}]")
82
-
83
- # ── Classify ───────────────────────────────────────────────────────────
84
- buf = io.BytesIO()
85
- sf.write(buf, np_data, 16000, format="WAV", subtype="PCM_16")
86
- buf.seek(0)
87
- raw_wav = buf.read()
88
-
89
- print(f"Classifying {len(raw_wav)} bytes WAV")
90
- results = classifier(raw_wav)
91
- print(f"Results: {results}")
92
-
93
- return {r["label"]: float(r["score"]) for r in results}
94
-
95
-
96
- demo = gr.Interface(
97
- fn=detect,
98
- inputs=gr.Audio(type="filepath", label="Audio Input"),
99
- outputs=gr.Label(num_top_classes=2, label="Result"),
100
- title="Deepfake Audio Detection API",
101
- allow_flagging="never",
102
- api_name="predict",
103
- )
104
-
105
- demo.launch()