Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -6,62 +6,56 @@ import whisper
|
|
6 |
import torchaudio
|
7 |
from groq import Groq
|
8 |
|
9 |
-
|
10 |
-
# Load Whisper model
|
11 |
whisper_model = whisper.load_model("tiny")
|
12 |
|
13 |
-
# Groq
|
14 |
groq_client = Groq(api_key=os.environ.get("GROQ_API_KEY"))
|
15 |
|
16 |
-
st.set_page_config(page_title="Voice
|
17 |
-
st.title("
|
18 |
|
19 |
-
#
|
20 |
if "clone_path" not in st.session_state:
|
21 |
st.session_state.clone_path = None
|
22 |
|
23 |
-
st.sidebar.header("Voice
|
24 |
-
|
25 |
|
26 |
-
if
|
27 |
-
uploaded = st.sidebar.file_uploader("Upload voice sample", type=["wav", "mp3"])
|
28 |
if uploaded:
|
29 |
-
|
30 |
-
st.session_state.clone_path = voice_path
|
31 |
-
st.success("Voice uploaded and saved as clone voice.")
|
32 |
-
|
33 |
-
if option == "Record Voice":
|
34 |
-
duration = st.sidebar.slider("Duration (seconds)", 3, 10, 5)
|
35 |
-
if st.sidebar.button("Record"):
|
36 |
-
from utils import record_audio
|
37 |
-
path = record_audio(duration=duration)
|
38 |
st.session_state.clone_path = path
|
39 |
-
st.success("Voice
|
40 |
|
41 |
-
|
42 |
-
st.subheader("
|
43 |
|
44 |
user_voice = st.file_uploader("Upload your voice question", type=["wav", "mp3"])
|
45 |
if user_voice:
|
46 |
user_voice_path = save_uploaded_audio(user_voice, "user_question.wav")
|
47 |
st.audio(user_voice_path)
|
48 |
|
49 |
-
# Transcribe
|
|
|
50 |
result = whisper_model.transcribe(user_voice_path)
|
51 |
user_text = result["text"]
|
52 |
-
st.
|
53 |
|
54 |
-
# Generate response
|
|
|
55 |
response = groq_client.chat.completions.create(
|
56 |
model="llama3-8b-8192",
|
57 |
messages=[{"role": "user", "content": user_text}]
|
58 |
)
|
59 |
reply = response.choices[0].message.content
|
60 |
-
st.
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
|
|
|
6 |
import torchaudio
|
7 |
from groq import Groq
|
8 |
|
9 |
+
# Load Whisper model for transcription
|
|
|
10 |
whisper_model = whisper.load_model("tiny")
|
11 |
|
12 |
+
# Initialize Groq LLM client
|
13 |
groq_client = Groq(api_key=os.environ.get("GROQ_API_KEY"))
|
14 |
|
15 |
+
st.set_page_config(page_title="Voice Chat", layout="centered")
|
16 |
+
st.title("π€ Voice Chat using Your Cloned Voice")
|
17 |
|
18 |
+
# Store cloned voice path
|
19 |
if "clone_path" not in st.session_state:
|
20 |
st.session_state.clone_path = None
|
21 |
|
22 |
+
st.sidebar.header("Setup Your Clone Voice")
|
23 |
+
voice_option = st.sidebar.radio("Choose how to provide clone voice", ["Upload Voice", "Record Voice"])
|
24 |
|
25 |
+
if voice_option == "Upload Voice":
|
26 |
+
uploaded = st.sidebar.file_uploader("Upload a voice sample", type=["wav", "mp3"])
|
27 |
if uploaded:
|
28 |
+
path = save_uploaded_audio(uploaded, "reference_voice.wav")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
29 |
st.session_state.clone_path = path
|
30 |
+
st.success("Voice uploaded and saved as your clone voice.")
|
31 |
|
32 |
+
# --- Main conversation section ---
|
33 |
+
st.subheader("π£οΈ Ask something using your voice")
|
34 |
|
35 |
user_voice = st.file_uploader("Upload your voice question", type=["wav", "mp3"])
|
36 |
if user_voice:
|
37 |
user_voice_path = save_uploaded_audio(user_voice, "user_question.wav")
|
38 |
st.audio(user_voice_path)
|
39 |
|
40 |
+
# Step 1: Transcribe
|
41 |
+
st.info("Transcribing...")
|
42 |
result = whisper_model.transcribe(user_voice_path)
|
43 |
user_text = result["text"]
|
44 |
+
st.success(f"π You said: {user_text}")
|
45 |
|
46 |
+
# Step 2: Generate LLM response
|
47 |
+
st.info("Thinking...")
|
48 |
response = groq_client.chat.completions.create(
|
49 |
model="llama3-8b-8192",
|
50 |
messages=[{"role": "user", "content": user_text}]
|
51 |
)
|
52 |
reply = response.choices[0].message.content
|
53 |
+
st.success(f"π€ AI says: {reply}")
|
54 |
+
|
55 |
+
# Step 3: Speak back in your clone voice
|
56 |
+
if st.session_state.clone_path:
|
57 |
+
st.info("Generating voice reply using your cloned voice...")
|
58 |
+
voice_output_path = clone_and_generate_text(reply, st.session_state.clone_path)
|
59 |
+
st.audio(voice_output_path)
|
60 |
+
else:
|
61 |
+
st.warning("Please upload your clone voice first in the sidebar.")
|