Ericsson_day_demo_model / tests /test_whisperer.py
Ali
remove files
8d62394
import streamlit as st
import requests
import sounddevice as sd
import numpy as np
import tempfile
import wave
import os
# Hugging Face endpoint
HF_ENDPOINT = "https://lngcv623mxnv20nt.eu-west-1.aws.endpoints.huggingface.cloud"
HF_TOKEN = os.getenv("HF_TOKEN") # Or replace with your token string
# Function to record audio
def record_audio(duration=5, samplerate=16000):
st.info("Recording... Speak now 🎙️")
audio = sd.rec(int(duration * samplerate), samplerate=samplerate, channels=1, dtype='int16')
sd.wait()
return audio, samplerate
# Save audio to temp WAV file
def save_wav(audio, samplerate):
temp_file = tempfile.NamedTemporaryFile(delete=False, suffix=".wav")
with wave.open(temp_file.name, 'wb') as wf:
wf.setnchannels(1)
wf.setsampwidth(2) # 16-bit
wf.setframerate(samplerate)
wf.writeframes(audio.tobytes())
return temp_file.name
# Send audio to Hugging Face endpoint
def transcribe(audio_path):
with open(audio_path, "rb") as f:
response = requests.post(
HF_ENDPOINT,
headers={
"Authorization": f"Bearer {HF_TOKEN}",
"Content-Type": "audio/wav"
},
data=f
)
if response.status_code == 200:
return response.json()
else:
st.error(f"Error: {response.status_code} - {response.text}")
return None
# Streamlit UI
st.title("🎤 Whisper Tiny Transcriber")
duration = st.slider("Recording duration (seconds):", 3, 15, 5)
if st.button("Press to Record"):
audio, sr = record_audio(duration)
wav_path = save_wav(audio, sr)
st.audio(wav_path, format="audio/wav")
result = transcribe(wav_path)
if result:
st.subheader("Transcription:")
st.write(result.get("text", result))