Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,43 +1,75 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from transformers import pipeline
|
|
|
|
| 3 |
import base64
|
| 4 |
import io
|
|
|
|
| 5 |
|
| 6 |
-
#
|
| 7 |
-
|
|
|
|
|
|
|
| 8 |
|
| 9 |
-
|
|
|
|
|
|
|
|
|
|
| 10 |
try:
|
| 11 |
-
# Convert
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
return {
|
| 28 |
"classification": classification,
|
| 29 |
-
"
|
|
|
|
| 30 |
}
|
| 31 |
except Exception as e:
|
| 32 |
return {"error": str(e)}
|
| 33 |
|
| 34 |
-
#
|
|
|
|
|
|
|
| 35 |
with gr.Blocks() as demo:
|
| 36 |
-
gr.Markdown("##
|
| 37 |
with gr.Row():
|
| 38 |
-
audio_input = gr.
|
| 39 |
output = gr.JSON(label="Prediction")
|
| 40 |
classify_btn = gr.Button("Classify")
|
| 41 |
classify_btn.click(classify_audio, inputs=audio_input, outputs=output)
|
| 42 |
|
| 43 |
demo.launch()
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from transformers import pipeline
|
| 3 |
+
from langdetect import detect
|
| 4 |
import base64
|
| 5 |
import io
|
| 6 |
+
from pydub import AudioSegment
|
| 7 |
|
| 8 |
+
# ----------------------------
|
| 9 |
+
# Load pretrained model globally (fixes runtime error)
|
| 10 |
+
# ----------------------------
|
| 11 |
+
model = pipeline("audio-classification", model="microsoft/wavlm-base-sv") # AI vs Human
|
| 12 |
|
| 13 |
+
# ----------------------------
|
| 14 |
+
# Function to detect language
|
| 15 |
+
# ----------------------------
|
| 16 |
+
def detect_language(audio_file):
|
| 17 |
try:
|
| 18 |
+
# Convert audio to text using Whisper (optional)
|
| 19 |
+
from transformers import pipeline as hf_pipeline
|
| 20 |
+
asr = hf_pipeline("automatic-speech-recognition", model="openai/whisper-small")
|
| 21 |
+
transcription = asr(audio_file)["text"]
|
| 22 |
+
lang = detect(transcription)
|
| 23 |
+
return lang
|
| 24 |
+
except Exception:
|
| 25 |
+
return "unknown"
|
| 26 |
+
|
| 27 |
+
# ----------------------------
|
| 28 |
+
# Main classification function
|
| 29 |
+
# ----------------------------
|
| 30 |
+
def classify_audio(input_audio):
|
| 31 |
+
try:
|
| 32 |
+
# If input is Base64 string, decode it
|
| 33 |
+
if isinstance(input_audio, str):
|
| 34 |
+
audio_bytes = base64.b64decode(input_audio)
|
| 35 |
+
audio_file = io.BytesIO(audio_bytes)
|
| 36 |
+
else:
|
| 37 |
+
audio_file = input_audio # If it's a file from Gradio audio recorder
|
| 38 |
+
|
| 39 |
+
# Convert to WAV for model
|
| 40 |
+
audio_segment = AudioSegment.from_file(audio_file)
|
| 41 |
+
wav_io = io.BytesIO()
|
| 42 |
+
audio_segment.export(wav_io, format="wav")
|
| 43 |
+
wav_io.seek(0)
|
| 44 |
+
|
| 45 |
+
# Run AI vs Human model
|
| 46 |
+
result = model(wav_io)[0]
|
| 47 |
+
classification = "AI_GENERATED" if "AI" in result['label'].upper() else "HUMAN"
|
| 48 |
+
confidence = round(float(result['score']), 2)
|
| 49 |
+
|
| 50 |
+
# Detect language
|
| 51 |
+
wav_io.seek(0)
|
| 52 |
+
language = detect_language(wav_io)
|
| 53 |
+
|
| 54 |
+
# Return JSON for buildathon
|
| 55 |
return {
|
| 56 |
"classification": classification,
|
| 57 |
+
"language": language,
|
| 58 |
+
"confidence": confidence
|
| 59 |
}
|
| 60 |
except Exception as e:
|
| 61 |
return {"error": str(e)}
|
| 62 |
|
| 63 |
+
# ----------------------------
|
| 64 |
+
# Gradio Interface
|
| 65 |
+
# ----------------------------
|
| 66 |
with gr.Blocks() as demo:
|
| 67 |
+
gr.Markdown("## AI vs Human Voice Detector + Language Detection")
|
| 68 |
with gr.Row():
|
| 69 |
+
audio_input = gr.Audio(source="microphone", type="filepath", label="Speak or upload audio")
|
| 70 |
output = gr.JSON(label="Prediction")
|
| 71 |
classify_btn = gr.Button("Classify")
|
| 72 |
classify_btn.click(classify_audio, inputs=audio_input, outputs=output)
|
| 73 |
|
| 74 |
demo.launch()
|
| 75 |
+
|