Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -13,50 +13,77 @@ whisper_model = whisper.load_model("tiny")
|
|
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("
|
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"
|
24 |
|
25 |
if voice_option == "Upload Voice":
|
26 |
uploaded = st.sidebar.file_uploader("Upload a voice sample", type=["wav", "mp3", "m4a", "flac", "ogg"])
|
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 |
-
# ---
|
33 |
-
st.subheader("π£οΈ Ask
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
st.
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
st.
|
61 |
-
|
62 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 & Text 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"])
|
24 |
|
25 |
if voice_option == "Upload Voice":
|
26 |
uploaded = st.sidebar.file_uploader("Upload a voice sample", type=["wav", "mp3", "m4a", "flac", "ogg"])
|
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 |
+
# --- Conversation section ---
|
33 |
+
st.subheader("π£οΈ Ask with voice or type text below")
|
34 |
+
|
35 |
+
tab1, tab2 = st.tabs(["π€ Voice Input", "π¬ Text Input"])
|
36 |
+
|
37 |
+
# --- VOICE INPUT TAB ---
|
38 |
+
with tab1:
|
39 |
+
user_voice = st.file_uploader("Upload your voice question", type=["wav", "mp3", "m4a", "flac", "ogg"])
|
40 |
+
if user_voice:
|
41 |
+
user_voice_path = save_uploaded_audio(user_voice, "user_question.wav")
|
42 |
+
st.audio(user_voice_path)
|
43 |
+
|
44 |
+
# Step 1: Transcribe voice
|
45 |
+
st.info("Transcribing...")
|
46 |
+
result = whisper_model.transcribe(user_voice_path)
|
47 |
+
user_text = result["text"]
|
48 |
+
st.success(f"π You said: {user_text}")
|
49 |
+
|
50 |
+
# Step 2: Get LLM response
|
51 |
+
st.info("Thinking...")
|
52 |
+
response = groq_client.chat.completions.create(
|
53 |
+
model="llama3-8b-8192",
|
54 |
+
messages=[{"role": "user", "content": user_text}]
|
55 |
+
)
|
56 |
+
reply = response.choices[0].message.content
|
57 |
+
st.success(f"π€ AI says: {reply}")
|
58 |
+
|
59 |
+
# Step 3: Voice reply
|
60 |
+
if st.session_state.clone_path:
|
61 |
+
st.info("Cloning voice reply...")
|
62 |
+
voice_output_path = clone_and_generate_text(reply, st.session_state.clone_path)
|
63 |
+
st.audio(voice_output_path)
|
64 |
+
else:
|
65 |
+
st.warning("Upload your voice clone first in the sidebar.")
|
66 |
+
|
67 |
+
# --- TEXT INPUT TAB ---
|
68 |
+
with tab2:
|
69 |
+
user_input = st.text_input("Type your question here:")
|
70 |
+
if st.button("Send Text"):
|
71 |
+
if user_input.strip() == "":
|
72 |
+
st.warning("Please enter a message.")
|
73 |
+
else:
|
74 |
+
# Step 1: Get LLM response
|
75 |
+
st.info("Thinking...")
|
76 |
+
response = groq_client.chat.completions.create(
|
77 |
+
model="llama3-8b-8192",
|
78 |
+
messages=[{"role": "user", "content": user_input}]
|
79 |
+
)
|
80 |
+
reply = response.choices[0].message.content
|
81 |
+
st.success(f"π€ AI says: {reply}")
|
82 |
+
|
83 |
+
# Step 2: Voice reply
|
84 |
+
if st.session_state.clone_path:
|
85 |
+
st.info("Cloning voice reply...")
|
86 |
+
voice_output_path = clone_and_generate_text(reply, st.session_state.clone_path)
|
87 |
+
st.audio(voice_output_path)
|
88 |
+
else:
|
89 |
+
st.warning("Upload your voice clone first in the sidebar.")
|