Update app.py
Browse files
app.py
CHANGED
@@ -1,5 +1,5 @@
|
|
1 |
import streamlit as st
|
2 |
-
from streamlit_webrtc import webrtc_streamer, WebRtcMode
|
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 |
-
|
20 |
-
st.info("π€ Please record your question below:")
|
21 |
-
|
22 |
-
audio_placeholder = st.empty()
|
23 |
|
24 |
webrtc_ctx = webrtc_streamer(
|
25 |
-
key="
|
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 "
|
35 |
-
st.session_state.
|
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.
|
41 |
|
42 |
if st.button("π Process Voice"):
|
43 |
with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as f:
|
44 |
-
f.write(st.session_state.
|
45 |
audio_path = f.name
|
46 |
|
47 |
st.audio(audio_path)
|
48 |
|
49 |
-
st.info("Transcribing
|
50 |
result = model.transcribe(audio_path)
|
51 |
-
|
52 |
-
st.success(f"You said: {
|
53 |
|
54 |
-
st.info("
|
55 |
response = openai.ChatCompletion.create(
|
56 |
model="mixtral-8x7b-32768",
|
57 |
-
messages=[{"role": "user", "content":
|
58 |
)
|
59 |
-
|
60 |
-
st.success(f"AI says: {
|
61 |
|
62 |
-
# Convert to
|
63 |
-
tts = gTTS(
|
64 |
-
tts_path = "
|
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")
|