Spaces:
Running
Running
import streamlit as st | |
import os | |
import io | |
from groq import Groq | |
import soundfile as sf | |
from tempfile import NamedTemporaryFile | |
# API-Key aus Umgebungsvariable laden | |
api_key = os.getenv('groqwhisper') | |
if not api_key: | |
st.error("Bitte setzen Sie die Umgebungsvariable 'groqwhisper'") | |
st.stop() | |
# Groq-Client initialisieren | |
client = Groq(api_key=api_key) | |
def process_audio(audio_bytes): | |
"""Verarbeitet Audio-Bytes und gibt Transkript zurück""" | |
try: | |
# Konvertiere Bytes in Audio-Daten | |
audio_io = io.BytesIO(audio_bytes) | |
samples, sample_rate = sf.read(audio_io) | |
with NamedTemporaryFile(suffix=".wav", delete=False) as tmpfile: | |
sf.write(tmpfile.name, samples, sample_rate) | |
with open(tmpfile.name, "rb") as audio_file: | |
transcription = client.audio.transcriptions.create( | |
file=(tmpfile.name, audio_file, "audio/wav"), | |
model="whisper-large-v3-turbo", | |
language="de", | |
response_format="text" | |
) | |
return transcription | |
except Exception as e: | |
return f"Fehler: {str(e)}" | |
finally: | |
if 'tmpfile' in locals(): | |
os.unlink(tmpfile.name) | |
# Streamlit UI | |
st.title("🎤 Audio Transkription") | |
st.info("Funktioniert auf Hugging Face Spaces!") | |
audio_bytes = st.audio_input( | |
"Oder sprechen Sie jetzt:", | |
type="wav" | |
) | |
if audio_bytes: | |
# Verarbeitung Mikrofonaufnahme | |
try: | |
# Konvertiere Bytes in Audio-Daten | |
audio_io = io.BytesIO(audio_bytes) | |
audio_data = sf.read(audio_io) | |
transcription = processaudio(audio_data) | |
sr_outputs.text(transcription) | |
except Exception as e: | |
sr_outputs.text(f"Fehler bei der Aufnahmeverarbeitung: {str(e)}") |