Nitzantry1 commited on
Commit
d9e71ec
ยท
verified ยท
1 Parent(s): c82bffb

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +29 -13
app.py CHANGED
@@ -1,16 +1,17 @@
1
- from pyannote.audio import Pipeline
2
- import gradio as gr
3
  import os
 
 
 
4
 
5
- # ื˜ืขืŸ ืืช ื”-Token
6
  hf_token = os.getenv("HF_TOKEN")
7
  if not hf_token:
8
- raise ValueError("HF_TOKEN is missing. Set it in the Secrets section.")
9
 
10
- # ื˜ืขืŸ ืืช ื”ืžื•ื“ืœ
11
  try:
12
  pipeline = Pipeline.from_pretrained(
13
- "pyannote/speaker-diarization",
14
  use_auth_token=hf_token
15
  )
16
  except Exception as e:
@@ -19,21 +20,36 @@ except Exception as e:
19
  # ืคื•ื ืงืฆื™ื” ืœื–ื™ื”ื•ื™ ื“ื•ื‘ืจื™ื
20
  def diarize(audio):
21
  try:
22
- diarization = pipeline(audio)
 
 
 
 
 
 
 
 
23
  result = []
24
  for turn, _, speaker in diarization.itertracks(yield_label=True):
25
  result.append(f"{speaker}: {turn.start:.1f}s - {turn.end:.1f}s")
 
 
 
 
26
  return "\n".join(result)
 
27
  except Exception as e:
28
  return f"Error during diarization: {e}"
29
 
30
- # ืžืžืฉืง ืžืฉืชืžืฉ
31
  interface = gr.Interface(
32
- fn=diarize,
33
- inputs="audio",
34
- outputs="text",
35
  title="Speaker Diarization",
36
- description="Upload an audio file to detect speakers and their timestamps."
37
  )
38
 
39
- interface.launch()
 
 
 
 
 
1
  import os
2
+ import tempfile
3
+ import gradio as gr
4
+ from pyannote.audio import Pipeline
5
 
6
+ # ืฉืœื™ืคืช Hugging Face Token ืžื”-Secret
7
  hf_token = os.getenv("HF_TOKEN")
8
  if not hf_token:
9
+ raise ValueError("HF_TOKEN is missing. Please set it in the Secrets section.")
10
 
11
+ # ื˜ืขื™ื ืช ืžื•ื“ืœ pyannote ืœื–ื™ื”ื•ื™ ื“ื•ื‘ืจื™ื
12
  try:
13
  pipeline = Pipeline.from_pretrained(
14
+ "pyannote/speaker-diarization",
15
  use_auth_token=hf_token
16
  )
17
  except Exception as e:
 
20
  # ืคื•ื ืงืฆื™ื” ืœื–ื™ื”ื•ื™ ื“ื•ื‘ืจื™ื
21
  def diarize(audio):
22
  try:
23
+ # ืฉืžื™ืจืช ื”ืื•ื“ื™ื• ืœืงื•ื‘ืฅ ื–ืžื ื™
24
+ with tempfile.NamedTemporaryFile(suffix=".wav", delete=False) as temp_audio:
25
+ temp_audio.write(audio.read())
26
+ temp_audio_path = temp_audio.name
27
+
28
+ # ืขื™ื‘ื•ื“ ื”ืื•ื“ื™ื• ืขื pyannote
29
+ diarization = pipeline(temp_audio_path)
30
+
31
+ # ืขื™ื‘ื•ื“ ื”ืชื•ืฆืื” ืœื–ื™ื”ื•ื™ ื“ื•ื‘ืจื™ื
32
  result = []
33
  for turn, _, speaker in diarization.itertracks(yield_label=True):
34
  result.append(f"{speaker}: {turn.start:.1f}s - {turn.end:.1f}s")
35
+
36
+ # ืžื—ื™ืงืช ืงื•ื‘ืฅ ื–ืžื ื™ ืœืื—ืจ ืฉื™ืžื•ืฉ
37
+ os.remove(temp_audio_path)
38
+
39
  return "\n".join(result)
40
+
41
  except Exception as e:
42
  return f"Error during diarization: {e}"
43
 
44
+ # ื™ืฆื™ืจืช ืžืžืฉืง Gradio
45
  interface = gr.Interface(
46
+ fn=diarize,
47
+ inputs="audio",
48
+ outputs="text",
49
  title="Speaker Diarization",
50
+ description="Upload an audio file (WAV, MP3, etc.) to detect speakers and their timestamps."
51
  )
52
 
53
+ # ื”ืคืขืœืช ื”ืžืžืฉืง
54
+ if __name__ == "__main__":
55
+ interface.launch()