meraj12 commited on
Commit
566375b
·
verified ·
1 Parent(s): 0212563

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +36 -36
app.py CHANGED
@@ -1,27 +1,28 @@
 
1
  import streamlit as st
2
- from utils import save_uploaded_audio
3
- from voice_cloner import clone_and_generate_text
4
  import os
5
  import whisper
6
- import torchaudio
 
 
7
  from groq import Groq
8
- from utils import save_chat_history, load_chat_history, list_chat_histories
9
-
10
 
11
-
12
- # Load Whisper model for transcription
13
  whisper_model = whisper.load_model("tiny")
14
 
15
- # Initialize Groq LLM client
16
  groq_client = Groq(api_key=os.environ.get("GROQ_API_KEY"))
17
 
18
  st.set_page_config(page_title="Voice Chat", layout="centered")
19
  st.title("🎤💬 Voice & Text Chat using Your Cloned Voice")
20
 
21
- # Store cloned voice path
22
  if "clone_path" not in st.session_state:
23
  st.session_state.clone_path = None
24
 
 
 
 
 
25
  st.sidebar.header("🧬 Setup Your Clone Voice")
26
  voice_option = st.sidebar.radio("Choose how to provide clone voice", ["Upload Voice"])
27
 
@@ -32,34 +33,27 @@ if voice_option == "Upload Voice":
32
  st.session_state.clone_path = path
33
  st.success("✅ Voice uploaded and saved as your clone voice.")
34
 
35
- # --- Conversation section ---
36
  st.subheader("🗣️ Ask with voice or type text below")
37
-
38
  tab1, tab2 = st.tabs(["🎤 Voice Input", "💬 Text Input"])
39
 
40
- # --- VOICE INPUT TAB ---
41
  with tab1:
42
  user_voice = st.file_uploader("Upload your voice question", type=["wav", "mp3", "m4a", "flac", "ogg"])
43
  if user_voice:
44
  user_voice_path = save_uploaded_audio(user_voice, "user_question.wav")
45
  st.audio(user_voice_path)
46
 
47
- # Step 1: Transcribe voice
48
  st.info("Transcribing...")
49
- result = whisper_model.transcribe(user_voice_path)
50
- user_text = result["text"]
51
  st.success(f"📝 You said: {user_text}")
52
 
53
- # Step 2: Get LLM response
54
  st.info("Thinking...")
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.success(f"🤖 AI says: {reply}")
61
 
62
- # Step 3: Voice reply
 
63
  if st.session_state.clone_path:
64
  st.info("Cloning voice reply...")
65
  voice_output_path = clone_and_generate_text(reply, st.session_state.clone_path)
@@ -67,32 +61,38 @@ with tab1:
67
  else:
68
  st.warning("Upload your voice clone first in the sidebar.")
69
 
70
- # --- TEXT INPUT TAB ---
71
  with tab2:
72
  user_input = st.text_input("Type your question here:")
73
  if st.button("Send Text"):
74
  if user_input.strip() == "":
75
  st.warning("Please enter a message.")
76
  else:
77
- # Step 1: Get LLM response
78
  st.info("Thinking...")
79
- response = groq_client.chat.completions.create(
80
- model="llama3-8b-8192",
81
- messages=[{"role": "user", "content": user_input}]
82
- )
83
- reply = response.choices[0].message.content
84
  st.success(f"🤖 AI says: {reply}")
85
 
86
- # Step 2: Voice reply
 
87
  if st.session_state.clone_path:
88
  st.info("Cloning voice reply...")
89
  voice_output_path = clone_and_generate_text(reply, st.session_state.clone_path)
90
  st.audio(voice_output_path)
91
  else:
92
  st.warning("Upload your voice clone first in the sidebar.")
93
- # Footer
94
- st.markdown("""
95
- <div class='footer'>
96
- DESIGN BY MERAJ GRAPHICS
97
- </div>
98
- """, unsafe_allow_html=True)
 
 
 
 
 
 
 
 
 
 
 
1
+ # app.py
2
  import streamlit as st
 
 
3
  import os
4
  import whisper
5
+ from groq_llm import get_groq_response
6
+ from utils import save_uploaded_audio, transcribe_audio, save_chat_history, list_chat_histories, load_chat_history
7
+ from voice_cloner import clone_and_generate_text
8
  from groq import Groq
 
 
9
 
10
+ # Load Whisper model
 
11
  whisper_model = whisper.load_model("tiny")
12
 
13
+ # Initialize Groq client
14
  groq_client = Groq(api_key=os.environ.get("GROQ_API_KEY"))
15
 
16
  st.set_page_config(page_title="Voice Chat", layout="centered")
17
  st.title("🎤💬 Voice & Text Chat using Your Cloned Voice")
18
 
 
19
  if "clone_path" not in st.session_state:
20
  st.session_state.clone_path = None
21
 
22
+ if "chat_history" not in st.session_state:
23
+ st.session_state.chat_history = []
24
+
25
+ # Sidebar: voice setup
26
  st.sidebar.header("🧬 Setup Your Clone Voice")
27
  voice_option = st.sidebar.radio("Choose how to provide clone voice", ["Upload Voice"])
28
 
 
33
  st.session_state.clone_path = path
34
  st.success("✅ Voice uploaded and saved as your clone voice.")
35
 
36
+ # Tabs for input
37
  st.subheader("🗣️ Ask with voice or type text below")
 
38
  tab1, tab2 = st.tabs(["🎤 Voice Input", "💬 Text Input"])
39
 
40
+ # Voice input tab
41
  with tab1:
42
  user_voice = st.file_uploader("Upload your voice question", type=["wav", "mp3", "m4a", "flac", "ogg"])
43
  if user_voice:
44
  user_voice_path = save_uploaded_audio(user_voice, "user_question.wav")
45
  st.audio(user_voice_path)
46
 
 
47
  st.info("Transcribing...")
48
+ user_text = transcribe_audio(user_voice_path, language="Urdu")
 
49
  st.success(f"📝 You said: {user_text}")
50
 
 
51
  st.info("Thinking...")
52
+ reply = get_groq_response(user_text, system_prompt="آپ ایک مددگار AI ہیں، جو اردو میں جواب دیتی ہے", language="Urdu")
 
 
 
 
53
  st.success(f"🤖 AI says: {reply}")
54
 
55
+ st.session_state.chat_history.append({"user": user_text, "ai": reply})
56
+
57
  if st.session_state.clone_path:
58
  st.info("Cloning voice reply...")
59
  voice_output_path = clone_and_generate_text(reply, st.session_state.clone_path)
 
61
  else:
62
  st.warning("Upload your voice clone first in the sidebar.")
63
 
64
+ # Text input tab
65
  with tab2:
66
  user_input = st.text_input("Type your question here:")
67
  if st.button("Send Text"):
68
  if user_input.strip() == "":
69
  st.warning("Please enter a message.")
70
  else:
 
71
  st.info("Thinking...")
72
+ reply = get_groq_response(user_input, system_prompt="You are a helpful AI.", language="Urdu")
 
 
 
 
73
  st.success(f"🤖 AI says: {reply}")
74
 
75
+ st.session_state.chat_history.append({"user": user_input, "ai": reply})
76
+
77
  if st.session_state.clone_path:
78
  st.info("Cloning voice reply...")
79
  voice_output_path = clone_and_generate_text(reply, st.session_state.clone_path)
80
  st.audio(voice_output_path)
81
  else:
82
  st.warning("Upload your voice clone first in the sidebar.")
83
+
84
+ # Save history
85
+ if st.button("Save Chat History"):
86
+ filename = save_chat_history(st.session_state.chat_history)
87
+ st.success(f"History saved as {filename}")
88
+
89
+ # View past chat histories
90
+ st.sidebar.markdown("---")
91
+ st.sidebar.subheader("📃 Load Chat History")
92
+ chat_files = list_chat_histories()
93
+ selected_file = st.sidebar.selectbox("Select a file to load", chat_files)
94
+ if st.sidebar.button("Load History"):
95
+ history = load_chat_history(selected_file)
96
+ for entry in history:
97
+ st.write(f"**You:** {entry['user']}")
98
+ st.write(f"**AI:** {entry['ai']}")