mgokg commited on
Commit
eb2635d
·
verified ·
1 Parent(s): 6e1b4fe

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +66 -13
app.py CHANGED
@@ -1,14 +1,50 @@
1
  import streamlit as st
2
  import os
3
  import google.generativeai as genai
 
 
 
4
 
5
  # Streamlit Seite konfigurieren
6
  st.set_page_config(
7
- page_title="Gemini-2 Chatbot",
8
  page_icon="🤖"
9
  )
10
 
 
 
 
 
 
11
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
12
  genai.configure(api_key=os.environ["geminiapi"])
13
 
14
  # Modell-Konfiguration
@@ -31,12 +67,36 @@ if "chat_session" not in st.session_state:
31
  st.session_state.chat_session = model.start_chat(history=[])
32
 
33
  # UI Komponenten
34
- st.title("Gemini-2 Chatbot")
35
- user_input = st.text_input("Stelle deine Frage:", key="user_input")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
36
 
37
  if user_input:
38
  messages = st.container()
39
- #messages.chat_message("user").write(f"{user_input}")
40
  # Prompt mit Sprachaufforderung kombinieren
41
  full_prompt = f"{user_input}\nAntworte immer auf Deutsch"
42
 
@@ -50,12 +110,5 @@ if user_input:
50
  response_text = "Keine Antwort erhalten"
51
 
52
  # Antwort anzeigen
53
- #st.subheader("Antwort:")
54
- #st.chat_input(response_text)
55
- messages.chat_message("assistant").write(f"{response_text}")
56
- # Quellen anzeigen falls vorhanden
57
- #if response.grounding_metadata:
58
- #st.subheader("Quellen:")
59
- #for source in response.grounding_metadata.sources:
60
- #st.markdown(f"- [{source.url}]({source.url})")
61
-
 
1
  import streamlit as st
2
  import os
3
  import google.generativeai as genai
4
+ import io
5
+ import soundfile as sf
6
+ from groq import Groq
7
 
8
  # Streamlit Seite konfigurieren
9
  st.set_page_config(
10
+ page_title="Gemini-2 Chatbot mit Spracheingabe",
11
  page_icon="🤖"
12
  )
13
 
14
+ # Load environment variables for Groq
15
+ api_key = os.getenv('groqwhisper')
16
+ if api_key is None:
17
+ st.error("The 'groq_whisper' environment variable is not set. Please set it and restart the app.")
18
+ st.stop()
19
 
20
+ # Initialize Groq client
21
+ client = Groq(api_key=api_key)
22
+
23
+ # Funktion zur Verarbeitung von Audio
24
+ def process_audio(audio_data):
25
+ """Process audio data and return transcription."""
26
+ try:
27
+ sample_rate, samples = audio_data
28
+
29
+ # Create in-memory WAV file
30
+ with io.BytesIO() as wav_buffer:
31
+ sf.write(wav_buffer, samples, sample_rate, format='WAV')
32
+ wav_buffer.seek(0)
33
+
34
+ # Send to Groq for transcription
35
+ transcription = client.audio.transcriptions.create(
36
+ file=("recording.wav", wav_buffer.read(), "audio/wav"),
37
+ model="whisper-large-v3-turbo",
38
+ prompt="transcribe",
39
+ language="de",
40
+ response_format="json",
41
+ temperature=0.0
42
+ )
43
+ return transcription.text
44
+ except Exception as e:
45
+ return f"An error occurred: {str(e)}"
46
+
47
+ # Gemini API Konfiguration
48
  genai.configure(api_key=os.environ["geminiapi"])
49
 
50
  # Modell-Konfiguration
 
67
  st.session_state.chat_session = model.start_chat(history=[])
68
 
69
  # UI Komponenten
70
+ st.title("Gemini-2 Chatbot mit Spracheingabe")
71
+
72
+ # Audio recorder component
73
+ audio_bytes = st.audio_input("Klicke zum Aufnehmen", key="audio_input")
74
+
75
+ # Chat Input
76
+ user_input = st.text_input("Oder schreibe deine Frage:", key="text_input")
77
+
78
+ if audio_bytes:
79
+ # Extrahiere die Bytes aus dem UploadedFile-Objekt
80
+ audio_bytes_content = audio_bytes.getvalue()
81
+
82
+ # Konvertiere die Bytes in ein numpy-Array mit soundfile
83
+ with io.BytesIO(audio_bytes_content) as wav_io:
84
+ samples, sample_rate = sf.read(wav_io)
85
+
86
+ # Konvertiere Stereo in Mono, falls erforderlich
87
+ if len(samples.shape) > 1 and samples.shape[1] == 2:
88
+ samples = samples.mean(axis=1)
89
+
90
+ # Verarbeite das Audio
91
+ with st.spinner("Transcribing..."):
92
+ transcription = process_audio((sample_rate, samples))
93
+
94
+ # Verarbeitet die Transkription als Eingabe für den Chatbot
95
+ if transcription:
96
+ user_input = transcription
97
 
98
  if user_input:
99
  messages = st.container()
 
100
  # Prompt mit Sprachaufforderung kombinieren
101
  full_prompt = f"{user_input}\nAntworte immer auf Deutsch"
102
 
 
110
  response_text = "Keine Antwort erhalten"
111
 
112
  # Antwort anzeigen
113
+ messages.chat_message("user").write(f"{user_input}")
114
+ messages.chat_message("assistant").write(f"{response_text}")