Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,23 +1,65 @@
|
|
1 |
import streamlit as st
|
2 |
from streamlit_webrtc import webrtc_streamer, WebRtcMode, ClientSettings
|
|
|
|
|
|
|
|
|
|
|
|
|
3 |
|
4 |
# Initialize Streamlit app layout
|
5 |
st.title("Microphone Input in Streamlit")
|
6 |
|
7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
8 |
webrtc_ctx = webrtc_streamer(
|
9 |
key="audio-only",
|
10 |
-
mode=WebRtcMode.
|
11 |
client_settings=ClientSettings(
|
12 |
media_stream_constraints={
|
13 |
"audio": True,
|
14 |
"video": False
|
15 |
}
|
16 |
-
)
|
|
|
17 |
)
|
18 |
|
19 |
# Placeholder for capturing audio
|
20 |
if webrtc_ctx.state.playing:
|
21 |
st.write("Microphone is active. Speak into the microphone...")
|
22 |
else:
|
23 |
-
st.write("Click to start microphone input.")
|
|
|
|
1 |
import streamlit as st
|
2 |
from streamlit_webrtc import webrtc_streamer, WebRtcMode, ClientSettings
|
3 |
+
import numpy as np
|
4 |
+
import io
|
5 |
+
import wave
|
6 |
+
import requests
|
7 |
+
from audio_processor import process_audio
|
8 |
+
|
9 |
|
10 |
# Initialize Streamlit app layout
|
11 |
st.title("Microphone Input in Streamlit")
|
12 |
|
13 |
+
def audio_callback(frame):
|
14 |
+
# Get raw audio data from the frame
|
15 |
+
audio_data = frame.to_ndarray().astype(np.int16)
|
16 |
+
|
17 |
+
# Save audio data to a WAV file
|
18 |
+
with io.BytesIO() as wav_buffer:
|
19 |
+
with wave.open(wav_buffer, 'wb') as wav_file:
|
20 |
+
wav_file.setnchannels(1) # Mono
|
21 |
+
wav_file.setsampwidth(2) # 16-bit
|
22 |
+
wav_file.setframerate(16000) # Sample rate
|
23 |
+
wav_file.writeframes(audio_data.tobytes())
|
24 |
+
wav_buffer.seek(0)
|
25 |
+
audio_bytes = wav_buffer.read()
|
26 |
+
|
27 |
+
# Convert audio to text
|
28 |
+
transcription = process_audio(audio_bytes)
|
29 |
+
|
30 |
+
# Display the transcription
|
31 |
+
st.write("Transcription:", transcription)
|
32 |
+
|
33 |
+
API_URL = "https://eaa0-34-74-179-199.ngrok-free.app/generate"
|
34 |
+
# Optionally, send the transcription to an API
|
35 |
+
headers = {
|
36 |
+
"Content-Type": "application/json"
|
37 |
+
}
|
38 |
+
payload = {
|
39 |
+
"prompt": transcription
|
40 |
+
}
|
41 |
+
response = requests.post(API_URL, json=payload, headers=headers)
|
42 |
+
if response.status_code == 200:
|
43 |
+
st.write("Assistant:", response.json())
|
44 |
+
else:
|
45 |
+
st.write("Error:", response.status_code, response.text)
|
46 |
+
|
47 |
+
|
48 |
webrtc_ctx = webrtc_streamer(
|
49 |
key="audio-only",
|
50 |
+
mode=WebRtcMode.SENDRECV,
|
51 |
client_settings=ClientSettings(
|
52 |
media_stream_constraints={
|
53 |
"audio": True,
|
54 |
"video": False
|
55 |
}
|
56 |
+
),
|
57 |
+
audio_frame_callback=audio_callback
|
58 |
)
|
59 |
|
60 |
# Placeholder for capturing audio
|
61 |
if webrtc_ctx.state.playing:
|
62 |
st.write("Microphone is active. Speak into the microphone...")
|
63 |
else:
|
64 |
+
st.write("Click to start microphone input.")
|
65 |
+
|