Laksh-itha-08 commited on
Commit
2f23519
·
verified ·
1 Parent(s): 5b63fb0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +55 -23
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
- # Load pretrained model for voice classification (AI vs Human)
7
- model = pipeline("audio-classification", model="microsoft/wavlm-base-sv") # multilingual friendly
 
 
8
 
9
- def classify_audio(base64_audio):
 
 
 
10
  try:
11
- # Convert Base64 to bytes
12
- audio_bytes = base64.b64decode(base64_audio)
13
- audio_file = io.BytesIO(audio_bytes)
14
-
15
- # Run model
16
- result = model(audio_file)[0]
17
-
18
- # Map labels to AI / HUMAN (example)
19
- label_map = {
20
- "AI_GENERATED": "AI_GENERATED",
21
- "human": "HUMAN"
22
- }
23
- classification = label_map.get(result['label'], "HUMAN")
24
- confidence = float(result['score'])
25
-
26
- # Return JSON
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
27
  return {
28
  "classification": classification,
29
- "confidence": round(confidence, 2)
 
30
  }
31
  except Exception as e:
32
  return {"error": str(e)}
33
 
34
- # Gradio UI
 
 
35
  with gr.Blocks() as demo:
36
- gr.Markdown("### AI vs Human Voice Detector")
37
  with gr.Row():
38
- audio_input = gr.Textbox(label="Paste Base64 Audio Here")
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
+