Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,66 +1,86 @@
|
|
1 |
import streamlit as st
|
2 |
import os
|
|
|
3 |
from groq import Groq
|
4 |
import soundfile as sf
|
5 |
from tempfile import NamedTemporaryFile
|
6 |
-
import io # Für Bytes-IO hinzugefügt
|
7 |
|
8 |
-
#
|
9 |
api_key = os.getenv('groqwhisper')
|
10 |
|
11 |
-
if api_key
|
12 |
-
|
|
|
13 |
|
14 |
-
#
|
15 |
client = Groq(api_key=api_key)
|
16 |
|
17 |
-
def
|
|
|
18 |
try:
|
19 |
-
|
20 |
-
sample_rate, samples = audio_data
|
21 |
|
22 |
-
|
23 |
-
with NamedTemporaryFile(suffix=".wav", delete=True) as tmpfile:
|
24 |
-
# Audio als WAV-Datei speichern
|
25 |
sf.write(tmpfile.name, samples, sample_rate)
|
26 |
|
27 |
-
|
28 |
-
with open(tmpfile.name, "rb") as file:
|
29 |
transcription = client.audio.transcriptions.create(
|
30 |
-
file=(
|
31 |
model="whisper-large-v3-turbo",
|
32 |
-
prompt="transcribe",
|
33 |
language="de",
|
34 |
-
response_format="
|
35 |
-
temperature=0.0
|
36 |
)
|
37 |
-
|
38 |
-
|
39 |
except Exception as e:
|
40 |
-
return f"
|
|
|
|
|
|
|
41 |
|
42 |
-
# Streamlit
|
43 |
-
st.title("Audio Transkription")
|
44 |
-
|
45 |
|
46 |
-
|
47 |
-
uploaded_file = st.file_uploader("Laden Sie eine Audiodatei hoch", type=["wav", "mp3"])
|
48 |
-
audio_bytes = st.audio_input("Oder sprechen Sie jetzt:", type="wav")
|
49 |
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
|
|
|
|
55 |
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
sr_outputs.text(transcription)
|
64 |
-
except Exception as e:
|
65 |
-
sr_outputs.text(f"Fehler bei der Aufnahmeverarbeitung: {str(e)}")
|
66 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import streamlit as st
|
2 |
import os
|
3 |
+
import io
|
4 |
from groq import Groq
|
5 |
import soundfile as sf
|
6 |
from tempfile import NamedTemporaryFile
|
|
|
7 |
|
8 |
+
# API-Key aus Umgebungsvariable laden
|
9 |
api_key = os.getenv('groqwhisper')
|
10 |
|
11 |
+
if not api_key:
|
12 |
+
st.error("Bitte setzen Sie die Umgebungsvariable 'groqwhisper'")
|
13 |
+
st.stop()
|
14 |
|
15 |
+
# Groq-Client initialisieren
|
16 |
client = Groq(api_key=api_key)
|
17 |
|
18 |
+
def process_audio(audio_data):
|
19 |
+
"""Verarbeitet Audiodaten und gibt Transkript zurück"""
|
20 |
try:
|
21 |
+
samples, sample_rate = audio_data # Korrigierte Reihenfolge
|
|
|
22 |
|
23 |
+
with NamedTemporaryFile(suffix=".wav", delete=False) as tmpfile:
|
|
|
|
|
24 |
sf.write(tmpfile.name, samples, sample_rate)
|
25 |
|
26 |
+
with open(tmpfile.name, "rb") as audio_file:
|
|
|
27 |
transcription = client.audio.transcriptions.create(
|
28 |
+
file=(tmpfile.name, audio_file, "audio/wav"),
|
29 |
model="whisper-large-v3-turbo",
|
|
|
30 |
language="de",
|
31 |
+
response_format="text"
|
|
|
32 |
)
|
33 |
+
return transcription
|
34 |
+
|
35 |
except Exception as e:
|
36 |
+
return f"Fehler: {str(e)}"
|
37 |
+
finally:
|
38 |
+
if tmpfile:
|
39 |
+
os.unlink(tmpfile.name)
|
40 |
|
41 |
+
# Streamlit UI
|
42 |
+
st.title("🎤 Live Audio Transkription")
|
43 |
+
st.info("Erlaube Mikrofonzugriff im Browser wenn gefragt!")
|
44 |
|
45 |
+
col1, col2 = st.columns(2)
|
|
|
|
|
46 |
|
47 |
+
with col1:
|
48 |
+
st.subheader("Option 1: Mikrofonaufnahme")
|
49 |
+
audio_bytes = st.audio_input(
|
50 |
+
"Sprich jetzt:",
|
51 |
+
type="wav",
|
52 |
+
key="mic_input"
|
53 |
+
)
|
54 |
|
55 |
+
with col2:
|
56 |
+
st.subheader("Option 2: Datei-Upload")
|
57 |
+
uploaded_file = st.file_uploader(
|
58 |
+
"Oder Datei hochladen:",
|
59 |
+
type=["wav", "mp3"],
|
60 |
+
key="file_upload"
|
61 |
+
)
|
|
|
|
|
|
|
62 |
|
63 |
+
# Verarbeitung
|
64 |
+
if audio_bytes or uploaded_file:
|
65 |
+
with st.spinner("Verarbeite Audio..."):
|
66 |
+
try:
|
67 |
+
if audio_bytes:
|
68 |
+
# Mikrofonaufnahme verarbeiten
|
69 |
+
audio_io = io.BytesIO(audio_bytes)
|
70 |
+
audio_data = sf.read(audio_io)
|
71 |
+
else:
|
72 |
+
# Hochgeladene Datei verarbeiten
|
73 |
+
audio_data = sf.read(uploaded_file)
|
74 |
+
|
75 |
+
# Transkription durchführen
|
76 |
+
result = process_audio(audio_data)
|
77 |
+
|
78 |
+
# Ergebnis anzeigen
|
79 |
+
st.subheader("Transkription:")
|
80 |
+
st.success(result)
|
81 |
+
|
82 |
+
# Audio-Player anzeigen
|
83 |
+
st.audio(audio_bytes or uploaded_file)
|
84 |
+
|
85 |
+
except Exception as e:
|
86 |
+
st.error(f"Verarbeitungsfehler: {str(e)}")
|