litagin commited on
Commit
7a0a380
โ€ข
1 Parent(s): 3e40110
Files changed (1) hide show
  1. app.py +19 -6
app.py CHANGED
@@ -1,4 +1,5 @@
1
  import pprint
 
2
 
3
  import gradio as gr
4
  import librosa
@@ -34,20 +35,32 @@ label_map = {
34
 
35
  @spaces.GPU
36
  def pipe(filename: str) -> tuple[dict[str, float], go.Figure]:
37
- audio, sr = librosa.load(filename, sr=16000)
38
- duration = librosa.get_duration(y=audio, sr=sr)
39
- logger.info(f"filename: {filename}, duration: {duration}")
 
 
 
 
 
 
 
 
 
 
 
 
 
40
  if duration > 30.0:
41
  return (
42
- {f"Error: ้Ÿณๅฃฐใƒ•ใ‚กใ‚คใƒซใฎ้•ทใ•ใŒ้•ทใ™ใŽใพใ™: {duration}็ง’": 0.0},
43
  go.Figure(),
44
  )
45
- inputs = processor(audio, sampling_rate=sr, return_tensors="pt")
46
  inputs = {k: v.to(device) for k, v in inputs.items()}
47
  with torch.no_grad():
48
  outputs: SequenceClassifierOutput = model(**inputs)
49
  logits = outputs.logits # shape: (batch_size, num_labels)
50
- # ใƒญใ‚ธใƒƒใƒˆใฎๅ–ๅพ—
51
  logits = logits[0].cpu().numpy()
52
  labels = [label_map[label] for id, label in model.config.id2label.items()]
53
  sorted_pairs = sorted(zip(logits, labels), key=lambda x: x[0])
 
1
  import pprint
2
+ from pathlib import Path
3
 
4
  import gradio as gr
5
  import librosa
 
35
 
36
  @spaces.GPU
37
  def pipe(filename: str) -> tuple[dict[str, float], go.Figure]:
38
+ if not filename:
39
+ return {"Error: ใƒ•ใ‚กใ‚คใƒซใŒๆŒ‡ๅฎšใ•ใ‚Œใฆใ„ใพใ›ใ‚“": 0.0}, go.Figure()
40
+ logger.info(f"filename: {Path(filename).name}")
41
+ try:
42
+ y, sr = librosa.load(filename, mono=True, sr=16000)
43
+ except Exception as e:
44
+ # First convert to wav if librosa cannot read the file
45
+ logger.error(f"Error reading file: {e}")
46
+ from pydub import AudioSegment
47
+
48
+ segment = AudioSegment.from_file(filename)
49
+ segment.export("temp.wav", format="wav")
50
+ y, sr = librosa.load("temp.wav", mono=True, sr=16000)
51
+ Path("temp.wav").unlink()
52
+ duration = librosa.get_duration(y=y, sr=sr)
53
+ logger.info(f"Duration: {duration:.2f}s")
54
  if duration > 30.0:
55
  return (
56
+ {f"Error: ้Ÿณๅฃฐใƒ•ใ‚กใ‚คใƒซใฎ้•ทใ•ใŒ้•ทใ™ใŽใพใ™: {duration:.2f}s": 0.0},
57
  go.Figure(),
58
  )
59
+ inputs = processor(y, sampling_rate=sr, return_tensors="pt")
60
  inputs = {k: v.to(device) for k, v in inputs.items()}
61
  with torch.no_grad():
62
  outputs: SequenceClassifierOutput = model(**inputs)
63
  logits = outputs.logits # shape: (batch_size, num_labels)
 
64
  logits = logits[0].cpu().numpy()
65
  labels = [label_map[label] for id, label in model.config.id2label.items()]
66
  sorted_pairs = sorted(zip(logits, labels), key=lambda x: x[0])