meraj12 commited on
Commit
b2cade9
Β·
verified Β·
1 Parent(s): c22cdba

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +63 -36
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("🎀 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", "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
- # --- 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", "m4a", "flac", "ogg"])
36
-
37
- if user_voice:
38
- user_voice_path = save_uploaded_audio(user_voice, "user_question.wav")
39
- st.audio(user_voice_path)
40
-
41
- # Step 1: Transcribe
42
- st.info("Transcribing...")
43
- result = whisper_model.transcribe(user_voice_path)
44
- user_text = result["text"]
45
- st.success(f"πŸ“ You said: {user_text}")
46
-
47
- # Step 2: Generate LLM response
48
- st.info("Thinking...")
49
- response = groq_client.chat.completions.create(
50
- model="llama3-8b-8192",
51
- messages=[{"role": "user", "content": user_text}]
52
- )
53
- reply = response.choices[0].message.content
54
- st.success(f"πŸ€– AI says: {reply}")
55
-
56
- # Step 3: Speak back in your clone voice
57
- if st.session_state.clone_path:
58
- st.info("Generating voice reply using your cloned voice...")
59
- voice_output_path = clone_and_generate_text(reply, st.session_state.clone_path)
60
- st.audio(voice_output_path)
61
- else:
62
- st.warning("Please upload your clone voice first in the sidebar.")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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.")