mgokg commited on
Commit
7d31ac3
·
verified ·
1 Parent(s): bca18f5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +27 -15
app.py CHANGED
@@ -17,6 +17,7 @@ client = Groq(api_key=api_key)
17
 
18
  def process_audio(audio_bytes):
19
  """Verarbeitet Audio-Bytes und gibt Transkript zurück"""
 
20
  try:
21
  # Konvertiere Bytes in Audio-Daten
22
  audio_io = io.BytesIO(audio_bytes)
@@ -27,7 +28,7 @@ def process_audio(audio_bytes):
27
 
28
  with open(tmpfile.name, "rb") as audio_file:
29
  transcription = client.audio.transcriptions.create(
30
- file=(tmpfile.name, audio_file, "audio/wav"),
31
  model="whisper-large-v3-turbo",
32
  language="de",
33
  response_format="text"
@@ -37,25 +38,36 @@ def process_audio(audio_bytes):
37
  except Exception as e:
38
  return f"Fehler: {str(e)}"
39
  finally:
40
- if 'tmpfile' in locals():
41
  os.unlink(tmpfile.name)
42
 
43
  # Streamlit UI
44
  st.title("🎤 Audio Transkription")
45
  st.info("Funktioniert auf Hugging Face Spaces!")
46
- #sr_outputs = st.text
47
 
48
- audio_bytes = st.audio_input(
49
- "Oder sprechen Sie jetzt:",
 
 
 
 
50
  )
51
 
52
- if audio_bytes:
53
- # Verarbeitung Mikrofonaufnahme
54
- try:
55
- # Konvertiere Bytes in Audio-Daten
56
- audio_io = io.BytesIO(audio_bytes)
57
- audio_data = sf.read(audio_io)
58
- transcription = processaudio(audio_data)
59
- st.text(transcription)
60
- except Exception as e:
61
- st.text(f"Fehler bei der Aufnahmeverarbeitung: {str(e)}")
 
 
 
 
 
 
 
 
 
17
 
18
  def process_audio(audio_bytes):
19
  """Verarbeitet Audio-Bytes und gibt Transkript zurück"""
20
+ tmpfile = None # Initialisierung für finally-Block
21
  try:
22
  # Konvertiere Bytes in Audio-Daten
23
  audio_io = io.BytesIO(audio_bytes)
 
28
 
29
  with open(tmpfile.name, "rb") as audio_file:
30
  transcription = client.audio.transcriptions.create(
31
+ file=(os.path.basename(tmpfile.name), audio_file), # Korrigierte Parameter
32
  model="whisper-large-v3-turbo",
33
  language="de",
34
  response_format="text"
 
38
  except Exception as e:
39
  return f"Fehler: {str(e)}"
40
  finally:
41
+ if tmpfile and os.path.exists(tmpfile.name):
42
  os.unlink(tmpfile.name)
43
 
44
  # Streamlit UI
45
  st.title("🎤 Audio Transkription")
46
  st.info("Funktioniert auf Hugging Face Spaces!")
 
47
 
48
+ # Kombinierter Uploader für Datei und Mikrofon
49
+ audio_file = st.file_uploader(
50
+ "Aufnahme starten oder Datei hochladen",
51
+ type=["wav"],
52
+ accept_multiple_files=False,
53
+ help="Klicken Sie auf 'Browse Files' und wählen Sie im Browser 'Take a recording'"
54
  )
55
 
56
+ if audio_file:
57
+ with st.spinner("Verarbeite Audio..."):
58
+ try:
59
+ # Datei in Bytes lesen
60
+ audio_bytes = audio_file.read()
61
+
62
+ # Transkription durchführen
63
+ result = process_audio(audio_bytes)
64
+
65
+ # Ergebnis anzeigen
66
+ st.subheader("Transkription:")
67
+ st.success(result)
68
+
69
+ # Audio-Player anzeigen
70
+ st.audio(audio_bytes, format="audio/wav")
71
+
72
+ except Exception as e:
73
+ st.error(f"Fehler: {str(e)}")