meraj12 commited on
Commit
0a3696b
Β·
verified Β·
1 Parent(s): e18b024

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +22 -17
app.py CHANGED
@@ -1,12 +1,10 @@
1
  import streamlit as st
2
- import whisper # This will now refer to the correct openai-whisper
3
  import openai
4
  import tempfile
5
  import os
6
- import requests
7
  from gtts import gTTS
8
  from pydub import AudioSegment
9
- from pydub.playback import play
10
 
11
  # Set your Groq-compatible OpenAI API key
12
  openai.api_key = os.getenv("GROQ_API_KEY", "your-groq-api-key")
@@ -14,41 +12,48 @@ openai.api_key = os.getenv("GROQ_API_KEY", "your-groq-api-key")
14
  # Load Whisper model
15
  model = whisper.load_model("base")
16
 
17
- # Title
18
- st.title("πŸŽ™οΈ Voice-to-Voice Conversational App")
19
 
20
- # Upload or record voice
21
- uploaded_file = st.file_uploader("Upload your voice message (MP3/WAV)", type=["mp3", "wav"])
22
 
23
- if uploaded_file:
24
- # Save audio to a temp file
 
 
 
 
 
 
 
 
 
25
  with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as tmp:
26
- tmp.write(uploaded_file.read())
27
  tmp_path = tmp.name
28
 
29
- # Transcribe with Whisper
30
  st.info("Transcribing...")
31
  result = model.transcribe(tmp_path)
32
  user_text = result["text"]
33
  st.success(f"You said: {user_text}")
34
 
35
- # Ask Groq/OpenAI
36
  st.info("Thinking...")
37
  response = openai.ChatCompletion.create(
38
- model="mixtral-8x7b-32768", # Groq supports this
39
  messages=[{"role": "user", "content": user_text}]
40
  )
41
  reply_text = response["choices"][0]["message"]["content"]
42
  st.success(f"AI says: {reply_text}")
43
 
44
- # Convert to voice (TTS)
45
  tts = gTTS(reply_text)
46
  tts_path = "response.mp3"
47
  tts.save(tts_path)
48
 
49
- # Play the voice
50
- audio = AudioSegment.from_file(tts_path)
51
  st.audio(tts_path, format="audio/mp3")
52
 
53
- # Clean up temp file
54
  os.remove(tmp_path)
 
1
  import streamlit as st
2
+ import whisper
3
  import openai
4
  import tempfile
5
  import os
 
6
  from gtts import gTTS
7
  from pydub import AudioSegment
 
8
 
9
  # Set your Groq-compatible OpenAI API key
10
  openai.api_key = os.getenv("GROQ_API_KEY", "your-groq-api-key")
 
12
  # Load Whisper model
13
  model = whisper.load_model("base")
14
 
15
+ st.title("🎀 Voice-to-Voice Chat App")
 
16
 
17
+ # Option to record or upload
18
+ mode = st.radio("Choose input method:", ["πŸŽ™οΈ Record Voice", "πŸ“ Upload Voice"])
19
 
20
+ audio_data = None
21
+
22
+ if mode == "πŸŽ™οΈ Record Voice":
23
+ audio_data = st.audio_recorder("Record your voice", format="audio/wav")
24
+ elif mode == "πŸ“ Upload Voice":
25
+ uploaded_file = st.file_uploader("Upload your voice message (MP3/WAV)", type=["mp3", "wav"])
26
+ if uploaded_file:
27
+ audio_data = uploaded_file.read()
28
+
29
+ # If there's audio data
30
+ if audio_data:
31
  with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as tmp:
32
+ tmp.write(audio_data)
33
  tmp_path = tmp.name
34
 
35
+ # Transcribe using Whisper
36
  st.info("Transcribing...")
37
  result = model.transcribe(tmp_path)
38
  user_text = result["text"]
39
  st.success(f"You said: {user_text}")
40
 
41
+ # Use Groq API (OpenAI-compatible)
42
  st.info("Thinking...")
43
  response = openai.ChatCompletion.create(
44
+ model="mixtral-8x7b-32768",
45
  messages=[{"role": "user", "content": user_text}]
46
  )
47
  reply_text = response["choices"][0]["message"]["content"]
48
  st.success(f"AI says: {reply_text}")
49
 
50
+ # Convert to speech
51
  tts = gTTS(reply_text)
52
  tts_path = "response.mp3"
53
  tts.save(tts_path)
54
 
55
+ # Play the response
 
56
  st.audio(tts_path, format="audio/mp3")
57
 
58
+ # Clean up
59
  os.remove(tmp_path)