Spaces:
Configuration error
Configuration error
Streaming agent
#2
by SpicyqSama007 - opened
1 unmodified line23456744 unmodified lines52535455565714 unmodified lines727374757677787980818223 unmodified lines10610710810911011111211311411511611711811912012112212312412512612751 unmodified lines1791801811821831841851 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(51 unmodified linestext_input = gr.Textbox(label="Or type your message", type="text",value="Can you give a brief introduction of yourself?")output_audio = gr.Audio(label="Assistant's Voice", type="numpy")send_button = gr.Button("Send", variant="primary")clear_button = gr.Button("Clear")1 unmodified line2345678944 unmodified lines545556575859606162636465666768697014 unmodified lines85868788899091929323 unmodified lines11711811912012112212312412512612712812913013113251 unmodified lines1841851861871881891901911921931941951 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(51 unmodified linestext_input = gr.Textbox(label="Or type your message", type="text",value="Can you give a brief introduction of yourself?")output_audio = gr.Audio(label="Assistant's Voice",streaming=True,autoplay=True,interactive=False,)send_button = gr.Button("Send", variant="primary")clear_button = gr.Button("Clear")
app.py+25-15