meraj12 commited on
Commit
0ec1227
·
verified ·
1 Parent(s): a5cfbe2

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +66 -0
app.py ADDED
@@ -0,0 +1,66 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ from utils import save_uploaded_audio
3
+ from voice_cloner.py import clone_and_generate_text
4
+ import os
5
+ import whisper
6
+ import torchaudio
7
+ from groq import Groq
8
+
9
+ # Load Whisper model
10
+ whisper_model = whisper.load_model("tiny")
11
+
12
+ # Groq API client
13
+ groq_client = Groq(api_key=os.environ.get("GROQ_API_KEY"))
14
+
15
+ st.set_page_config(page_title="Voice Cloner Chat", layout="centered")
16
+ st.title("🎙️ Voice Cloner Chat App")
17
+
18
+ # Session State
19
+ if "clone_path" not in st.session_state:
20
+ st.session_state.clone_path = None
21
+
22
+ st.sidebar.header("Voice Input")
23
+ option = st.sidebar.radio("Choose input method", ["Upload Voice", "Record Voice"])
24
+
25
+ if option == "Upload Voice":
26
+ uploaded = st.sidebar.file_uploader("Upload voice sample", type=["wav", "mp3"])
27
+ if uploaded:
28
+ voice_path = save_uploaded_audio(uploaded, "reference_voice.wav")
29
+ st.session_state.clone_path = voice_path
30
+ st.success("Voice uploaded and saved as clone voice.")
31
+
32
+ if option == "Record Voice":
33
+ duration = st.sidebar.slider("Duration (seconds)", 3, 10, 5)
34
+ if st.sidebar.button("Record"):
35
+ from utils import record_audio
36
+ path = record_audio(duration=duration)
37
+ st.session_state.clone_path = path
38
+ st.success("Voice recorded and saved as clone voice.")
39
+
40
+ st.divider()
41
+ st.subheader("💬 Talk with AI using cloned voice")
42
+
43
+ user_voice = st.file_uploader("Upload your voice question", type=["wav", "mp3"])
44
+ if user_voice:
45
+ user_voice_path = save_uploaded_audio(user_voice, "user_question.wav")
46
+ st.audio(user_voice_path)
47
+
48
+ # Transcribe question
49
+ result = whisper_model.transcribe(user_voice_path)
50
+ user_text = result["text"]
51
+ st.write(f"📝 Transcribed Text: {user_text}")
52
+
53
+ # Generate response using Groq
54
+ response = groq_client.chat.completions.create(
55
+ model="llama3-8b-8192",
56
+ messages=[{"role": "user", "content": user_text}]
57
+ )
58
+ reply = response.choices[0].message.content
59
+ st.write(f"🤖 AI Response: {reply}")
60
+
61
+ if st.checkbox("Use cloned voice to reply"):
62
+ if st.session_state.clone_path:
63
+ clone_audio = clone_and_generate_text(reply, st.session_state.clone_path)
64
+ st.audio(clone_audio)
65
+ else:
66
+ st.warning("No clone voice found. Please upload or record one.")