Skip to content

Commit

Permalink
Merge pull request #317 from NexaAI/perry-debug
Browse files Browse the repository at this point in the history
Perry debug
  • Loading branch information
zhiyuan8 authored Dec 13, 2024
2 parents ab58a2d + 180d712 commit bdee294
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 26 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,13 @@ Welcome to submit your requests through [issues](https://github.com/NexaAI/nexa-
## Install Option 1: Executable Installer

<p>
<a href="https://public-storage.nexa4ai.com/nexa-sdk-executable-installer/nexa-sdk-0.0.9.6-macos-installer.pkg">
<a href="https://public-storage.nexa4ai.com/nexa-sdk-executable-installer/nexa-sdk-0.0.9.7-macos-installer.pkg">
<img src="./assets/mac.png" style="height: 1em; width: auto" /> <strong> macOS Installer </strong>
</a>
</p>

<p>
<a href="https://public-storage.nexa4ai.com/nexa-sdk-executable-installer/nexa-sdk-0.0.9.6-windows-setup.exe">
<a href="https://public-storage.nexa4ai.com/nexa-sdk-executable-installer/nexa-sdk-0.0.9.7-windows-setup.exe">
<img src="./assets/windows.png" style="height: 1em; width: auto" /> <strong>Windows Installer</strong>
</a>
</p>
Expand Down
50 changes: 26 additions & 24 deletions nexa/gguf/nexa_inference_audio_lm.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
import logging
import os
import sys
import time
import librosa
import tempfile
import soundfile as sf
from pathlib import Path
from streamlit.web import cli as stcli
Expand Down Expand Up @@ -215,15 +215,6 @@ def inference(self, audio_path: str, prompt: str = "") -> str:
return response.decode("utf-8") if isinstance(response, bytes) else response
except Exception as e:
raise RuntimeError(f"Error during inference: {str(e)}")
finally:
if self.temp_file:
try:
self.temp_file.close()
if os.path.exists(self.temp_file.name):
os.unlink(self.temp_file.name)
except:
pass
self.temp_file = None

def cleanup(self):
"""
Expand All @@ -233,14 +224,12 @@ def cleanup(self):
audio_lm_cpp.free(self.context, is_qwen=self.is_qwen)
self.context = None

if self.temp_file:
if self.temp_file and os.path.exists(self.temp_file):
try:
self.temp_file.close()
if os.path.exists(self.temp_file.name):
os.unlink(self.temp_file.name)
except:
pass
self.temp_file = None
os.remove(self.temp_file)
self.temp_file = None
except Exception as e:
logging.warning(f"Failed to remove temporary file {self.temp_file}: {e}")

# def __del__(self):
# """
Expand All @@ -255,25 +244,38 @@ def _ensure_16khz(self, audio_path: str) -> str:
Supports various audio formats (mp3, wav, m4a, etc.)
"""
try:
base_dir = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))

# Create tmp directory if it doesn't exist
tmp_dir = os.path.join(base_dir, 'tmp')
os.makedirs(tmp_dir, exist_ok=True)

# Load audio file
y, sr = librosa.load(audio_path, sr=None)

if sr == 16000:
return audio_path

# Resample to 16kHz
print(f"Resampling audio from {sr} to 16000")
y_resampled = librosa.resample(y=y, orig_sr=sr, target_sr=16000)
self.temp_file = tempfile.NamedTemporaryFile(
suffix='.wav',
delete=False
)

# Create a unique filename in the tmp directory
original_name = os.path.splitext(os.path.basename(audio_path))[0]
tmp_filename = f"resampled_{original_name}_16khz_{int(time.time())}.wav"
tmp_path = os.path.join(tmp_dir, tmp_filename)

# Save the resampled audio
sf.write(
self.temp_file.name,
tmp_path,
y_resampled,
16000,
subtype='PCM_16'
)
return self.temp_file.name

# Store the path for cleanup
self.temp_file = tmp_path
return tmp_path

except Exception as e:
raise RuntimeError(f"Error processing audio file: {str(e)}")
Expand Down

0 comments on commit bdee294

Please sign in to comment.