| import streamlit as st |
| import requests |
| import sounddevice as sd |
| import numpy as np |
| import tempfile |
| import wave |
| import os |
|
|
| |
| HF_ENDPOINT = "https://lngcv623mxnv20nt.eu-west-1.aws.endpoints.huggingface.cloud" |
| HF_TOKEN = os.getenv("HF_TOKEN") |
|
|
| |
| 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 |
|
|
| |
| 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) |
| wf.setframerate(samplerate) |
| wf.writeframes(audio.tobytes()) |
| return temp_file.name |
|
|
| |
| 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 |
|
|
| |
| 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)) |