import os import gradio as gr from contextloader import ContextLoader from models.gpt4o…
- User
import os import gradio as gr from context_loader import ContextLoader from models.gpt4o import ModelClient from models.speech.local_whisper import SpeechRecognizer
class NeuroRavenApp: def init(self): self.system_prompt = None self.current_theme = None self.client = ModelClient() self.context_loader = ContextLoader() self.speech_recognizer = SpeechRecognizer() self.conversation_themes = self.get_conversation_themes() if "Без контекста" not in self.conversation_themes: self.conversation_themes.append("Без контекста")
@staticmethod def get_conversation_themes(docs_path='docs'): # Получаем список всех директорий в указанном пути return [name for name in os.listdir(docs_path) if os.path.isdir(os.path.join(docs_path, name))] def handle_query(self, message, history, audio, context_theme): # Use SpeechRecognizer to transcribe audio transcribed_text = self.speech_recognizer.transcribe_audio(audio) message += "\n" + transcribed_text if transcribed_text else message if context_theme != self.current_theme: context = self.context_loader.load_txt_files(context_theme) if context: self.system_prompt = f"Отвечай на вопросы беря информацию из контекста: \n{context}" else: self.system_prompt = "You are a helpful assistant." self.current_theme = context_theme response = self.client.get_response( message=message, history=history, sys_prompt=self.system_prompt ) return response def launch_app(self): gr.ChatInterface( self.handle_query, chatbot=gr.Chatbot(height=500), retry_btn=None, clear_btn="Clear", theme="soft", title="NeuroRaven", css="footer{display:none !important}", additional_inputs=[ gr.Microphone(type="filepath"), gr.Dropdown(self.conversation_themes, label="Выбери тему контекста", value="Без контекста"), ] ).launch()if name == "main": app = NeuroRavenApp() app.launch_app()
Как это предупреждение убрать? C:\Developing\Python\NeuroRaven.venv\lib\site-packages\gradio\processing_utils.py:188: UserWarning: Trying to convert audio automatically from int32 to 16-bit int format. warnings.warn(warning.format(data.dtype))
- Assistant
The warning message you are seeing is due to the Gradio library automatically converting your audio files from a 32-bit integer format to a 16-bit integer format. This is a common process for handling audio data, as most audio processing libraries and formats work with 16-bit integers.
To specifically address and suppress this warning, you have a few options:
1. Convert Audio Data Manually
One way to handle this is to manually convert the audio files to the desired 16-bit format before passing them to Gradio or your processing function. This can be done using libraries like
numpyorscipy.Here's an example on how to do that using
numpy:import numpy as np import soundfile as sf def convert_audio_to_16bit(audio_path): data, samplerate = sf.read(audio_path) if data.dtype == np.int32: data = (data / np.iinfo(np.int32).max * np.iinfo(np.int16).max).astype(np.int16) output_path = "converted_" + os.path.basename(audio_path) sf.write(output_path, data, samplerate) return output_pathYou would then call this function to convert your audio before processing:
def handle_query(self, message, history, audio, context_theme): # Convert audio to 16-bit format audio = convert_audio_to_16bit(audio) # Use SpeechRecognizer to transcribe audio transcribed_text = self.speech_recognizer.transcribe_audio(audio) ...2. Ignore Warnings (Not Recommended)
If you want to simply suppress the warning without addressing the root cause, you can use Python's
warningsmodule:import warnings # Suppress specific warning warnings.filterwarnings("ignore", message="Trying to convert audio automatically from int32 to 16-bit int format.")Place the suppression line at the start of your script. However, it's generally better to modify the data types or processing steps correctly rather than ignoring warnings.
3. Update Gradio or Dependencies
Ensure all your libraries are up-to-date as sometimes warnings are removed in newer versions. You can update Gradio via pip:
pip install --upgrade gradioThese options should help you manage or eliminate the warning about audio conversion effectively.