File size: 2,038 Bytes
545ec70
 
336a7dd
 
 
 
 
 
545ec70
 
 
 
336a7dd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
545ec70
 
336a7dd
545ec70
 
 
 
 
336a7dd
 
545ec70
 
 
 
 
 
336a7dd
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
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.")