art / app.py
meraj12's picture
Update app.py
e18b024 verified
raw
history blame
1.54 kB
import streamlit as st
import whisper # This will now refer to the correct openai-whisper
import openai
import tempfile
import os
import requests
from gtts import gTTS
from pydub import AudioSegment
from pydub.playback import play
# Set your Groq-compatible OpenAI API key
openai.api_key = os.getenv("GROQ_API_KEY", "your-groq-api-key")
# Load Whisper model
model = whisper.load_model("base")
# Title
st.title("πŸŽ™οΈ Voice-to-Voice Conversational App")
# Upload or record voice
uploaded_file = st.file_uploader("Upload your voice message (MP3/WAV)", type=["mp3", "wav"])
if uploaded_file:
# Save audio to a temp file
with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as tmp:
tmp.write(uploaded_file.read())
tmp_path = tmp.name
# Transcribe with Whisper
st.info("Transcribing...")
result = model.transcribe(tmp_path)
user_text = result["text"]
st.success(f"You said: {user_text}")
# Ask Groq/OpenAI
st.info("Thinking...")
response = openai.ChatCompletion.create(
model="mixtral-8x7b-32768", # Groq supports this
messages=[{"role": "user", "content": user_text}]
)
reply_text = response["choices"][0]["message"]["content"]
st.success(f"AI says: {reply_text}")
# Convert to voice (TTS)
tts = gTTS(reply_text)
tts_path = "response.mp3"
tts.save(tts_path)
# Play the voice
audio = AudioSegment.from_file(tts_path)
st.audio(tts_path, format="audio/mp3")
# Clean up temp file
os.remove(tmp_path)