Spaces:
Running
Running
import streamlit as st | |
from streamlit_webrtc import webrtc_streamer, WebRtcMode, ClientSettings | |
import numpy as np | |
import io | |
import wave | |
import requests | |
from audio_processor import process_audio | |
# Initialize Streamlit app layout | |
st.title("Microphone Input in Streamlit") | |
def audio_callback(frame): | |
# Get raw audio data from the frame | |
audio_data = frame.to_ndarray().astype(np.int16) | |
# Save audio data to a WAV file | |
with io.BytesIO() as wav_buffer: | |
with wave.open(wav_buffer, 'wb') as wav_file: | |
wav_file.setnchannels(1) # Mono | |
wav_file.setsampwidth(2) # 16-bit | |
wav_file.setframerate(16000) # Sample rate | |
wav_file.writeframes(audio_data.tobytes()) | |
wav_buffer.seek(0) | |
audio_bytes = wav_buffer.read() | |
# Convert audio to text | |
transcription = process_audio(audio_bytes) | |
# Display the transcription | |
st.write("Transcription:", transcription) | |
API_URL = "https://eaa0-34-74-179-199.ngrok-free.app/generate" | |
# Optionally, send the transcription to an API | |
headers = { | |
"Content-Type": "application/json" | |
} | |
payload = { | |
"prompt": transcription | |
} | |
response = requests.post(API_URL, json=payload, headers=headers) | |
if response.status_code == 200: | |
st.write("Assistant:", response.json()) | |
else: | |
st.write("Error:", response.status_code, response.text) | |
webrtc_ctx = webrtc_streamer( | |
key="audio-only", | |
mode=WebRtcMode.SENDRECV, | |
client_settings=ClientSettings( | |
media_stream_constraints={ | |
"audio": True, | |
"video": False | |
} | |
), | |
audio_frame_callback=audio_callback | |
) | |
# Placeholder for capturing audio | |
if webrtc_ctx.state.playing: | |
st.write("Microphone is active. Speak into the microphone...") | |
else: | |
st.write("Click to start microphone input.") | |