meraj12 commited on
Commit
ff8e2d1
Β·
verified Β·
1 Parent(s): 4ef7273

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +27 -33
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 API client
14
  groq_client = Groq(api_key=os.environ.get("GROQ_API_KEY"))
15
 
16
- st.set_page_config(page_title="Voice Cloner Chat", layout="centered")
17
- st.title("πŸŽ™οΈ Voice Cloner Chat App")
18
 
19
- # Session State
20
  if "clone_path" not in st.session_state:
21
  st.session_state.clone_path = None
22
 
23
- st.sidebar.header("Voice Input")
24
- option = st.sidebar.radio("Choose input method", ["Upload Voice", "Record Voice"])
25
 
26
- if option == "Upload Voice":
27
- uploaded = st.sidebar.file_uploader("Upload voice sample", type=["wav", "mp3"])
28
  if uploaded:
29
- voice_path = save_uploaded_audio(uploaded, "reference_voice.wav")
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 recorded and saved as clone voice.")
40
 
41
- st.divider()
42
- st.subheader("πŸ’¬ Talk with AI using cloned voice")
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 question
 
50
  result = whisper_model.transcribe(user_voice_path)
51
  user_text = result["text"]
52
- st.write(f"πŸ“ Transcribed Text: {user_text}")
53
 
54
- # Generate response using Groq
 
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.write(f"πŸ€– AI Response: {reply}")
61
-
62
- if st.checkbox("Use cloned voice to reply"):
63
- if st.session_state.clone_path:
64
- clone_audio = clone_and_generate_text(reply, st.session_state.clone_path)
65
- st.audio(clone_audio)
66
- else:
67
- st.warning("No clone voice found. Please upload or record one.")
 
 
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.")