File size: 3,564 Bytes
2570129
 
 
6a9fed6
 
2570129
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6a9fed6
2570129
6a9fed6
 
 
2570129
 
 
 
 
 
 
 
 
 
6a9fed6
2570129
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
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)