AI-TALKS-BACK / app.py
Bonosa2's picture
Upload 3 files
2570129 verified
raw
history blame
3.56 kB
import os
import uuid
import logging
import streamlit as st
import requests
from dotenv import load_dotenv
from utils import voice_map, get_voice_prompt_style, AUDIO_DIR
from generate_audio import generate_audio
# Load secrets
load_dotenv()
OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
ELEVENLABS_API_KEY = os.getenv("ELEVENLABS_API_KEY")
# Setup
os.makedirs(AUDIO_DIR, exist_ok=True)
logging.basicConfig(filename="frontend.log", level=logging.INFO)
# Page config
st.set_page_config(page_title="Voice Agent Pro", page_icon="🎀")
st.title("πŸŽ™οΈ Voice Agent Pro")
st.markdown("Summarized answers with expressive AI voices.")
# Sidebar voice selector
st.sidebar.header("🎚️ Voice Settings")
voice_label = st.sidebar.selectbox("Choose a voice:", list(voice_map.keys()))
voice_id = voice_map[voice_label]
tone_prompt = get_voice_prompt_style(voice_label)
# App state
if "answer" not in st.session_state: st.session_state.answer = ""
if "audio_key" not in st.session_state: st.session_state.audio_key = None
if "file_text" not in st.session_state: st.session_state.file_text = ""
if "key_points" not in st.session_state: st.session_state.key_points = []
# Query box
query = st.text_area(
"πŸ—¨οΈ Ask or refine something based on the bullets:",
value="",
placeholder="e.g., What makes you so cool, Grandma?",
key="query"
)
url = st.text_input("🌐 Optional URL to summarize:")
uploaded_file = st.file_uploader("πŸ“Ž Or upload a file (PDF, TXT, DOCX)", type=["pdf", "txt", "docx"])
# Clear all
if st.button("🧹 Clear All"):
st.session_state.query = ""
st.session_state.file_text = ""
st.session_state.answer = ""
st.session_state.audio_key = None
st.session_state.key_points = []
# Summarize
if st.button("πŸ” Summarize"):
if not query and not url and not uploaded_file:
st.warning("Please enter a question, a URL, or upload a file.")
else:
with st.spinner("Talking to GPT..."):
try:
if uploaded_file:
st.session_state.file_text = uploaded_file.read().decode("utf-8")
# Compose prompt
context = ""
if st.session_state.file_text:
context += st.session_state.file_text + "\n\n"
if url:
context += f"Summarize this page: {url}\n\n"
context += f"{tone_prompt}\n\nNow answer: {query}"
# GPT call
headers = {"Authorization": f"Bearer {OPENAI_API_KEY}"}
response = requests.post(
"https://api.openai.com/v1/chat/completions",
headers=headers,
json={
"model": "gpt-4o",
"messages": [{"role": "user", "content": context}],
"temperature": 0.7
}
)
answer = response.json()["choices"][0]["message"]["content"]
st.session_state.answer = answer
# Generate audio
audio_key = str(uuid.uuid4())
generate_audio(answer, voice_id, audio_key)
st.session_state.audio_key = audio_key
except Exception as e:
st.error(f"πŸ”₯ Error: {e}")
# Display answer
if st.session_state.answer:
st.subheader("πŸ“œ Answer")
st.success(st.session_state.answer)
if st.session_state.audio_key:
audio_path = os.path.join(AUDIO_DIR, f"{st.session_state.audio_key}.mp3")
st.audio(audio_path)