meraj12 commited on
Commit
eab7da3
Β·
verified Β·
1 Parent(s): 123c1f2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +21 -27
app.py CHANGED
@@ -1,5 +1,5 @@
1
  import streamlit as st
2
- from streamlit_webrtc import webrtc_streamer, WebRtcMode, ClientSettings
3
  import av
4
  import whisper
5
  import openai
@@ -8,59 +8,53 @@ import os
8
  from gtts import gTTS
9
  from pydub import AudioSegment
10
 
 
 
 
11
  # Load Whisper model
12
  model = whisper.load_model("base")
13
 
14
- # Set OpenAI (Groq-compatible) API Key
15
- openai.api_key = os.getenv("GROQ_API_KEY", "your-groq-api-key")
16
-
17
  st.title("πŸŽ™οΈ Voice-to-Voice Conversational App")
18
 
19
- # Record audio using streamlit-webrtc
20
- st.info("🎀 Please record your question below:")
21
-
22
- audio_placeholder = st.empty()
23
 
24
  webrtc_ctx = webrtc_streamer(
25
- key="speech",
26
  mode=WebRtcMode.SENDRECV,
27
- client_settings=ClientSettings(
28
- media_stream_constraints={"audio": True, "video": False},
29
- rtc_configuration={"iceServers": [{"urls": ["stun:stun.l.google.com:19302"]}]}
30
- ),
31
  audio_receiver_size=1024,
 
32
  )
33
 
34
- if "audio_buffer" not in st.session_state:
35
- st.session_state.audio_buffer = b""
36
 
37
  if webrtc_ctx.audio_receiver:
38
  audio_frames = webrtc_ctx.audio_receiver.get_frames(timeout=1)
39
  for frame in audio_frames:
40
- st.session_state.audio_buffer += frame.to_ndarray().tobytes()
41
 
42
  if st.button("πŸ›‘ Process Voice"):
43
  with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as f:
44
- f.write(st.session_state.audio_buffer)
45
  audio_path = f.name
46
 
47
  st.audio(audio_path)
48
 
49
- st.info("Transcribing with Whisper...")
50
  result = model.transcribe(audio_path)
51
- user_text = result["text"]
52
- st.success(f"You said: {user_text}")
53
 
54
- st.info("Generating AI response...")
55
  response = openai.ChatCompletion.create(
56
  model="mixtral-8x7b-32768",
57
- messages=[{"role": "user", "content": user_text}]
58
  )
59
- reply = response['choices'][0]['message']['content']
60
- st.success(f"AI says: {reply}")
61
 
62
- # Convert to speech
63
- tts = gTTS(reply)
64
- tts_path = "reply.mp3"
65
  tts.save(tts_path)
66
  st.audio(tts_path, format="audio/mp3")
 
1
  import streamlit as st
2
+ from streamlit_webrtc import webrtc_streamer, WebRtcMode
3
  import av
4
  import whisper
5
  import openai
 
8
  from gtts import gTTS
9
  from pydub import AudioSegment
10
 
11
+ # Set your API Key (Groq-compatible)
12
+ openai.api_key = os.getenv("GROQ_API_KEY", "your-groq-api-key")
13
+
14
  # Load Whisper model
15
  model = whisper.load_model("base")
16
 
 
 
 
17
  st.title("πŸŽ™οΈ Voice-to-Voice Conversational App")
18
 
19
+ st.info("🎀 Record your voice and click 'Stop' to process:")
 
 
 
20
 
21
  webrtc_ctx = webrtc_streamer(
22
+ key="example",
23
  mode=WebRtcMode.SENDRECV,
 
 
 
 
24
  audio_receiver_size=1024,
25
+ media_stream_constraints={"audio": True, "video": False},
26
  )
27
 
28
+ if "audio_bytes" not in st.session_state:
29
+ st.session_state.audio_bytes = b""
30
 
31
  if webrtc_ctx.audio_receiver:
32
  audio_frames = webrtc_ctx.audio_receiver.get_frames(timeout=1)
33
  for frame in audio_frames:
34
+ st.session_state.audio_bytes += frame.to_ndarray().tobytes()
35
 
36
  if st.button("πŸ›‘ Process Voice"):
37
  with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as f:
38
+ f.write(st.session_state.audio_bytes)
39
  audio_path = f.name
40
 
41
  st.audio(audio_path)
42
 
43
+ st.info("πŸ”  Transcribing...")
44
  result = model.transcribe(audio_path)
45
+ user_input = result["text"]
46
+ st.success(f"You said: {user_input}")
47
 
48
+ st.info("🧠 Thinking...")
49
  response = openai.ChatCompletion.create(
50
  model="mixtral-8x7b-32768",
51
+ messages=[{"role": "user", "content": user_input}]
52
  )
53
+ answer = response["choices"][0]["message"]["content"]
54
+ st.success(f"AI says: {answer}")
55
 
56
+ # Convert to voice
57
+ tts = gTTS(answer)
58
+ tts_path = "output.mp3"
59
  tts.save(tts_path)
60
  st.audio(tts_path, format="audio/mp3")