Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -26,39 +26,45 @@ def process_audio(audio: tuple, state: AppState):
|
|
| 26 |
state.pause_detected = pause_detected
|
| 27 |
|
| 28 |
if state.pause_detected:
|
| 29 |
-
return gr.Audio(recording=False), state
|
| 30 |
return None, state
|
| 31 |
|
| 32 |
# Generate response based on input type (text or audio)
|
| 33 |
def response(input_data, state: AppState, input_type: str):
|
| 34 |
if input_type == "text":
|
| 35 |
-
|
| 36 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
state.conversation.append({"role": "assistant", "content": bot_response})
|
| 38 |
return bot_response, state
|
| 39 |
|
| 40 |
-
if
|
| 41 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
state.
|
| 46 |
-
frame_rate=state.sampling_rate,
|
| 47 |
-
sample_width=state.stream.dtype.itemsize,
|
| 48 |
-
channels=1 if len(state.stream.shape) == 1 else state.stream.shape[1]
|
| 49 |
-
)
|
| 50 |
-
segment.export(audio_buffer, format="wav")
|
| 51 |
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
|
|
|
| 55 |
|
| 56 |
-
|
| 57 |
-
with tempfile.NamedTemporaryFile(suffix=".mp3", delete=False) as f:
|
| 58 |
-
f.write(chatbot_response)
|
| 59 |
-
state.conversation.append({"role": "assistant", "content": {"path": f.name, "mime_type": "audio/mp3"}})
|
| 60 |
|
| 61 |
-
|
| 62 |
|
| 63 |
# Start recording audio input
|
| 64 |
def start_recording_user(state: AppState):
|
|
@@ -69,7 +75,7 @@ def start_recording_user(state: AppState):
|
|
| 69 |
with gr.Blocks() as demo:
|
| 70 |
with gr.Row():
|
| 71 |
with gr.Column():
|
| 72 |
-
input_audio = gr.Audio(label="Input Audio", type="numpy")
|
| 73 |
text_input = gr.Textbox(label="Text Input", placeholder="Type your message here...")
|
| 74 |
with gr.Column():
|
| 75 |
chatbot = gr.Chatbot(label="Conversation", type="messages")
|
|
|
|
| 26 |
state.pause_detected = pause_detected
|
| 27 |
|
| 28 |
if state.pause_detected:
|
| 29 |
+
return gr.Audio(recording=False), state # Stop recording
|
| 30 |
return None, state
|
| 31 |
|
| 32 |
# Generate response based on input type (text or audio)
|
| 33 |
def response(input_data, state: AppState, input_type: str):
|
| 34 |
if input_type == "text":
|
| 35 |
+
# Ensure text input is handled correctly
|
| 36 |
+
user_message = input_data.strip() # Prevent errors from empty inputs
|
| 37 |
+
if not user_message:
|
| 38 |
+
return "Please enter a valid message.", state
|
| 39 |
+
|
| 40 |
+
state.conversation.append({"role": "user", "content": user_message})
|
| 41 |
+
bot_response = f"Echo: {user_message}" # Simulated bot response
|
| 42 |
state.conversation.append({"role": "assistant", "content": bot_response})
|
| 43 |
return bot_response, state
|
| 44 |
|
| 45 |
+
if input_type == "audio" and state.pause_detected:
|
| 46 |
+
# Convert audio to WAV and store in conversation history
|
| 47 |
+
audio_buffer = io.BytesIO()
|
| 48 |
+
segment = AudioSegment(
|
| 49 |
+
state.stream.tobytes(),
|
| 50 |
+
frame_rate=state.sampling_rate,
|
| 51 |
+
sample_width=state.stream.dtype.itemsize,
|
| 52 |
+
channels=1 if len(state.stream.shape) == 1 else state.stream.shape[1]
|
| 53 |
+
)
|
| 54 |
+
segment.export(audio_buffer, format="wav")
|
| 55 |
|
| 56 |
+
with tempfile.NamedTemporaryFile(suffix=".wav", delete=False) as f:
|
| 57 |
+
f.write(audio_buffer.getvalue())
|
| 58 |
+
state.conversation.append({"role": "user", "content": {"path": f.name, "mime_type": "audio/wav"}})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 59 |
|
| 60 |
+
chatbot_response = b"Simulated response audio content"
|
| 61 |
+
with tempfile.NamedTemporaryFile(suffix=".mp3", delete=False) as f:
|
| 62 |
+
f.write(chatbot_response)
|
| 63 |
+
state.conversation.append({"role": "assistant", "content": {"path": f.name, "mime_type": "audio/mp3"}})
|
| 64 |
|
| 65 |
+
yield None, state
|
|
|
|
|
|
|
|
|
|
| 66 |
|
| 67 |
+
return None, state # Handle unexpected input cases gracefully
|
| 68 |
|
| 69 |
# Start recording audio input
|
| 70 |
def start_recording_user(state: AppState):
|
|
|
|
| 75 |
with gr.Blocks() as demo:
|
| 76 |
with gr.Row():
|
| 77 |
with gr.Column():
|
| 78 |
+
input_audio = gr.Audio(label="Input Audio", type="numpy")
|
| 79 |
text_input = gr.Textbox(label="Text Input", placeholder="Type your message here...")
|
| 80 |
with gr.Column():
|
| 81 |
chatbot = gr.Chatbot(label="Conversation", type="messages")
|