Spaces:
Configuration error
Configuration error
Streaming agent
Browse files1 unmodified line23456744 unmodified lines52535455565714 unmodified lines727374757677787980818223 unmodified lines1061071081091101111121131141151161171181191201211221231241251261271 unmodified lineimport gradio as grimport numpy as npimport osimport threadingimport subprocessimport sys44 unmodified linesdef clear_fn():return [], ChatState(), None, None, Noneasync def process_audio_input(sys_audio_input, sys_text_input, audio_input, state: ChatState, text_input: str14 unmodified linesif isinstance(sys_audio_input, tuple):sr, sys_audio_data = sys_audio_inputelif text_input:sr = 44100sys_audio_data = Noneelse:raise gr.Error("Invalid audio format")def append_to_chat_ctx(part: ServeTextPart | ServeVQPart, role: str = "assistant"23 unmodified lines):if event.type == FishE2EEventType.USER_CODES:append_to_chat_ctx(ServeVQPart(codes=event.vq_codes), role="user")elif event.type == FishE2EEventType.SPEECH_SEGMENT:result_audio += event.frame.datanp_audio = np.frombuffer(result_audio, dtype=np.int16)append_to_chat_ctx(ServeVQPart(codes=event.vq_codes))yield state.get_history(), (44100, np_audio), None, Noneelif event.type == FishE2EEventType.TEXT_SEGMENT:append_to_chat_ctx(ServeTextPart(text=event.text))if result_audio:np_audio = np.frombuffer(result_audio, dtype=np.int16)yield state.get_history(), (44100, np_audio), None, Noneelse:yield state.get_history(), None, None, Nonenp_audio = np.frombuffer(result_audio, dtype=np.int16)yield state.get_history(), (44100, np_audio), None, Noneasync def process_text_input(1 unmodified line2345678944 unmodified lines545556575859606162636465666768697014 unmodified lines85868788899091929323 unmodified lines1171181191201211221231241251261271281291301311321 unmodified lineimport gradio as grimport numpy as npimport osimport ioimport waveimport threadingimport subprocessimport sys44 unmodified linesdef clear_fn():return [], ChatState(), None, None, Nonedef wav_chunk_header(sample_rate=44100, bit_depth=16, channels=1):buffer = io.BytesIO()with wave.open(buffer, "wb") as wav_file:wav_file.setnchannels(channels)wav_file.setsampwidth(bit_depth // 8)wav_file.setframerate(sample_rate)wav_header_bytes = buffer.getvalue()buffer.close()return wav_header_bytesasync def process_audio_input(sys_audio_input, sys_text_input, audio_input, state: ChatState, text_input: str14 unmodified linesif isinstance(sys_audio_input, tuple):sr, sys_audio_data = sys_audio_inputelse:sr = 44100sys_audio_data = Nonedef append_to_chat_ctx(part: ServeTextPart | ServeVQPart, role: str = "assistant"23 unmodified lines):if event.type == FishE2EEventType.USER_CODES:append_to_chat_ctx(ServeVQPart(codes=event.vq_codes), role="user")elif event.type == FishE2EEventType.SPEECH_SEGMENT:append_to_chat_ctx(ServeVQPart(codes=event.vq_codes))yield state.get_history(), wav_chunk_header() + event.frame.data, None, Noneelif event.type == FishE2EEventType.TEXT_SEGMENT:append_to_chat_ctx(ServeTextPart(text=event.text))yield state.get_history(), None, None, Noneyield state.get_history(), None, None, Noneasync def process_text_input(
app.py+20-15