mgokg commited on
Commit
79c7151
·
verified ·
1 Parent(s): 7e33190

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +61 -41
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
- # Load the API key from the environment variable
9
  api_key = os.getenv('groqwhisper')
10
 
11
- if api_key is None:
12
- raise ValueError("groq_whisper environment variable is not set")
 
13
 
14
- # Initialize the Groq client
15
  client = Groq(api_key=api_key)
16
 
17
- def processaudio(audio_data):
 
18
  try:
19
- # Entpacken der Audiodaten (Sample-Rate und Numpy-Array)
20
- sample_rate, samples = audio_data
21
 
22
- # Temporäre Audiodatei erstellen
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
- # Datei erneut öffnen und an Groq senden
28
- with open(tmpfile.name, "rb") as file:
29
  transcription = client.audio.transcriptions.create(
30
- file=(os.path.basename(tmpfile.name), file.read()),
31
  model="whisper-large-v3-turbo",
32
- prompt="transcribe",
33
  language="de",
34
- response_format="json",
35
- temperature=0.0
36
  )
37
- return transcription.text
38
-
39
  except Exception as e:
40
- return f"Ein Fehler ist aufgetreten: {str(e)}"
 
 
 
41
 
42
- # Streamlit Interface
43
- st.title("Audio Transkription")
44
- sr_outputs = st.empty()
45
 
46
- # Dateiupload und Mikrofonaufnahme als getrennte Eingaben
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
- if uploaded_file:
51
- # Verarbeitung hochgeladener Datei
52
- audio_data = sf.read(uploaded_file)
53
- transcription = processaudio(audio_data)
54
- sr_outputs.text(transcription)
 
 
55
 
56
- elif audio_bytes is not None:
57
- # Verarbeitung Mikrofonaufnahme
58
- try:
59
- # Konvertiere Bytes in Audio-Daten
60
- audio_io = io.BytesIO(audio_bytes)
61
- audio_data = sf.read(audio_io)
62
- transcription = processaudio(audio_data)
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)}")