Updated ai_audio_tour_agent
This commit is contained in:
parent
471ab9150d
commit
8e038c486b
4 changed files with 223 additions and 135 deletions
|
|
@ -1,4 +1,4 @@
|
|||
# 🗺️ Self-Guided Audio Tour Agent
|
||||
# 🎧 Self-Guided AI Audio Tour Agent
|
||||
|
||||
A conversational voice agent system that generates immersive, self-guided audio tours based on the user’s **location**, **areas of interest**, and **tour duration**. Built on a multi-agent architecture using OpenAI Agents SDK, real-time information retrieval, and expressive TTS for natural speech output.
|
||||
|
||||
|
|
@ -43,11 +43,7 @@ A conversational voice agent system that generates immersive, self-guided audio
|
|||
|
||||
### 🔊 Expressive Speech Output
|
||||
|
||||
- High-quality audio generated using **Text-to-Speech (TTS)**
|
||||
|
||||
|
||||
|
||||
|
||||
- High-quality audio generated using **Gpt-4o Mini Audio**
|
||||
|
||||
### How to get Started?
|
||||
|
||||
|
|
|
|||
|
|
@ -25,7 +25,7 @@ class Architecture(BaseModel):
|
|||
architecture_agent = Agent(
|
||||
name="ArchitectureAgent",
|
||||
instructions=ARCHITECTURE_AGENT_INSTRUCTIONS,
|
||||
model="gpt-4o",
|
||||
model="gpt-4o-mini",
|
||||
tools=[WebSearchTool()],
|
||||
model_settings=ModelSettings(tool_choice="required"),
|
||||
output_type=Architecture
|
||||
|
|
@ -55,7 +55,7 @@ class Culinary(BaseModel):
|
|||
culinary_agent = Agent(
|
||||
name="CulinaryAgent",
|
||||
instructions=CULINARY_AGENT_INSTRUCTIONS,
|
||||
model="gpt-4o",
|
||||
model="gpt-4o-mini",
|
||||
tools=[WebSearchTool()],
|
||||
model_settings=ModelSettings(tool_choice="required"),
|
||||
output_type=Culinary
|
||||
|
|
@ -84,7 +84,7 @@ class Culture(BaseModel):
|
|||
culture_agent = Agent(
|
||||
name="CulturalAgent",
|
||||
instructions=CULTURE_AGENT_INSTRUCTIONS,
|
||||
model="gpt-4o",
|
||||
model="gpt-4o-mini",
|
||||
tools=[WebSearchTool()],
|
||||
model_settings=ModelSettings(tool_choice="required"),
|
||||
output_type=Culture
|
||||
|
|
@ -113,7 +113,7 @@ class History(BaseModel):
|
|||
historical_agent = Agent(
|
||||
name="HistoricalAgent",
|
||||
instructions=HISTORY_AGENT_INSTRUCTIONS,
|
||||
model="gpt-4o",
|
||||
model="gpt-4o-mini",
|
||||
output_type=History,
|
||||
tools=[WebSearchTool()],
|
||||
model_settings=ModelSettings(tool_choice="required"),
|
||||
|
|
@ -197,7 +197,7 @@ class FinalTour(BaseModel):
|
|||
orchestrator_agent = Agent(
|
||||
name="OrchestratorAgent",
|
||||
instructions=ORCHESTRATOR_INSTRUCTIONS,
|
||||
model="gpt-4o",
|
||||
model="gpt-4o-mini",
|
||||
output_type=FinalTour,
|
||||
)
|
||||
|
||||
|
|
|
|||
|
|
@ -2,6 +2,7 @@ import streamlit as st
|
|||
import asyncio
|
||||
from manager import TourManager
|
||||
from agents import set_default_openai_key
|
||||
import json
|
||||
|
||||
def tts(text):
|
||||
from pathlib import Path
|
||||
|
|
@ -12,15 +13,11 @@ def tts(text):
|
|||
|
||||
response = client.audio.speech.create(
|
||||
model="gpt-4o-mini-tts",
|
||||
voice="coral",
|
||||
voice="nova",
|
||||
input=text,
|
||||
instructions="""Speak with different tones based on the content sections:
|
||||
- Use an inviting and warm tone for the Introduction.
|
||||
- Speak in an authoritative tone for History sections.
|
||||
- Use a positive and curious tone when discussing Architecture.
|
||||
- Use an enthusiastic and lively tone when covering Culture.
|
||||
- Use a joyful and passionate tone when talking about Culinary topics.
|
||||
Keep the transitions smooth and natural between sections."""
|
||||
instructions="""You are a friendly and engaging tour guide. Speak naturally and conversationally, as if you're walking alongside the visitor.
|
||||
Use a warm, inviting tone throughout. Avoid robotic or formal language. Make the tour feel like a casual conversation with a knowledgeable friend.
|
||||
Use natural transitions between topics and maintain an enthusiastic but relaxed pace."""
|
||||
)
|
||||
response.stream_to_file(speech_file_path)
|
||||
return speech_file_path
|
||||
|
|
@ -31,35 +28,69 @@ def run_async(func, *args, **kwargs):
|
|||
except RuntimeError:
|
||||
loop = asyncio.get_event_loop()
|
||||
return loop.run_until_complete(func(*args, **kwargs))
|
||||
|
||||
st.sidebar.title("🔑 API Settings")
|
||||
api_key = st.sidebar.text_input("Enter your OpenAI API key:", type="password")
|
||||
|
||||
if api_key:
|
||||
st.session_state["OPENAI_API_KEY"] = api_key
|
||||
st.sidebar.success("API key saved!")
|
||||
# Set page config for a better UI
|
||||
st.set_page_config(
|
||||
page_title="AI Audio Tour Agent",
|
||||
page_icon="🎧",
|
||||
layout="wide",
|
||||
initial_sidebar_state="collapsed"
|
||||
)
|
||||
|
||||
# Sidebar for API key
|
||||
with st.sidebar:
|
||||
st.title("🔑 Settings")
|
||||
api_key = st.text_input("OpenAI API Key:", type="password")
|
||||
if api_key:
|
||||
st.session_state["OPENAI_API_KEY"] = api_key
|
||||
st.success("API key saved!")
|
||||
|
||||
set_default_openai_key(api_key)
|
||||
|
||||
st.title("🏛️ Self-Guided Audio Tour Generator")
|
||||
# Main content
|
||||
st.title("🎧 AI Audio Tour Agent")
|
||||
st.markdown("""
|
||||
<div class='welcome-card'>
|
||||
<h3>Welcome to your personalized audio tour guide!</h3>
|
||||
<p>I'll help you explore any location with an engaging, natural-sounding tour tailored to your interests.</p>
|
||||
</div>
|
||||
""", unsafe_allow_html=True)
|
||||
|
||||
# Create a clean layout with cards
|
||||
col1, col2 = st.columns([2, 1])
|
||||
|
||||
location = st.text_input("📍 Enter the location for your tour:")
|
||||
with col1:
|
||||
st.markdown("### 📍 Where would you like to explore?")
|
||||
location = st.text_input("", placeholder="Enter a city, landmark, or location...")
|
||||
|
||||
st.markdown("### 🎯 What interests you?")
|
||||
interests = st.multiselect(
|
||||
"",
|
||||
options=["History", "Architecture", "Culinary", "Culture"],
|
||||
default=["History", "Architecture"],
|
||||
help="Select the topics you'd like to learn about"
|
||||
)
|
||||
|
||||
interests = st.multiselect(
|
||||
"🎯 Select your interests:",
|
||||
options=["History", "Architecture", "Culinary", "Culture"]
|
||||
)
|
||||
with col2:
|
||||
st.markdown("### ⏱️ Tour Settings")
|
||||
duration = st.slider(
|
||||
"Tour Duration (minutes)",
|
||||
min_value=5,
|
||||
max_value=60,
|
||||
value=10,
|
||||
step=5,
|
||||
help="Choose how long you'd like your tour to be"
|
||||
)
|
||||
|
||||
st.markdown("### 🎙️ Voice Settings")
|
||||
voice_style = st.selectbox(
|
||||
"Guide's Voice Style",
|
||||
options=["Friendly & Casual", "Professional & Detailed", "Enthusiastic & Energetic"],
|
||||
help="Select the personality of your tour guide"
|
||||
)
|
||||
|
||||
duration = st.slider(
|
||||
"⏱️ Select the duration of the tour (in minutes):",
|
||||
min_value=5,
|
||||
max_value=60,
|
||||
value=30,
|
||||
step=5
|
||||
)
|
||||
|
||||
if st.button("🎧 Generate Tour"):
|
||||
# Generate Tour Button
|
||||
if st.button("🎧 Generate Tour", type="primary"):
|
||||
if "OPENAI_API_KEY" not in st.session_state:
|
||||
st.error("Please enter your OpenAI API key in the sidebar.")
|
||||
elif not location:
|
||||
|
|
@ -67,17 +98,31 @@ if st.button("🎧 Generate Tour"):
|
|||
elif not interests:
|
||||
st.error("Please select at least one interest.")
|
||||
else:
|
||||
with st.spinner(f"Generating a {duration}-minute tour in {location} focused on {', '.join(interests)}."):
|
||||
with st.spinner(f"Creating your personalized tour of {location}..."):
|
||||
mgr = TourManager()
|
||||
final_tour = run_async(
|
||||
mgr.run, location, interests, duration
|
||||
)
|
||||
|
||||
with st.expander("🎬 Tour"):
|
||||
# Display the tour content in an expandable section
|
||||
with st.expander("📝 Tour Content", expanded=True):
|
||||
st.markdown(final_tour)
|
||||
|
||||
with st.spinner("Generating Audio"):
|
||||
tour_audio = tts(final_tour)
|
||||
|
||||
# Add a progress bar for audio generation
|
||||
with st.spinner("🎙️ Generating audio tour..."):
|
||||
progress_bar = st.progress(0)
|
||||
tour_audio = tts(final_tour)
|
||||
progress_bar.progress(100)
|
||||
|
||||
# Display audio player with custom styling
|
||||
st.markdown("### 🎧 Listen to Your Tour")
|
||||
st.audio(tour_audio, format="audio/mp3")
|
||||
|
||||
|
||||
# Add download button for the audio
|
||||
with open(tour_audio, "rb") as file:
|
||||
st.download_button(
|
||||
label="📥 Download Audio Tour",
|
||||
data=file,
|
||||
file_name=f"{location.lower().replace(' ', '_')}_tour.mp3",
|
||||
mime="audio/mp3"
|
||||
)
|
||||
|
|
@ -27,142 +27,189 @@ class TourManager:
|
|||
self.console = Console()
|
||||
self.printer = Printer(self.console)
|
||||
|
||||
async def run(self, query: str, interest: str, duration: str) -> None:
|
||||
async def run(self, query: str, interests: list, duration: str) -> None:
|
||||
trace_id = gen_trace_id()
|
||||
with trace("Tour Research trace", trace_id=trace_id):
|
||||
self.printer.update_item(
|
||||
"trace_id",
|
||||
f"View trace: https://platform.openai.com/traces/{trace_id}",
|
||||
"View trace: https://platform.openai.com/traces/{}".format(trace_id),
|
||||
is_done=True,
|
||||
hide_checkmark=True,
|
||||
)
|
||||
self.printer.update_item("start", "Starting tour research...", is_done=True)
|
||||
planner = await self._get_plan(query, interest, duration)
|
||||
print(planner)
|
||||
architecture_research = await self._get_architecture(query, interest, planner.architecture)
|
||||
historical_research = await self._get_history(query, interest, planner.history)
|
||||
culinary_research = await self._get_culinary(query, interest, planner.culinary)
|
||||
culture_research = await self._get_culture(query, interest, planner.culture)
|
||||
final_tour = await self._get_final_tour(query, interest, duration, architecture_research.output, planner.architecture, historical_research.output, planner.history, culinary_research.output, planner.culinary, culture_research.output, planner.culture)
|
||||
|
||||
# Get plan based on selected interests
|
||||
planner = await self._get_plan(query, interests, duration)
|
||||
|
||||
# Initialize research results
|
||||
research_results = {}
|
||||
|
||||
# Calculate word limits based on duration
|
||||
# Assuming average speaking rate of 150 words per minute
|
||||
words_per_minute = 150
|
||||
total_words = int(duration) * words_per_minute
|
||||
words_per_section = total_words // len(interests)
|
||||
|
||||
# Only research selected interests
|
||||
if "Architecture" in interests:
|
||||
research_results["architecture"] = await self._get_architecture(query, interests, words_per_section)
|
||||
|
||||
if "History" in interests:
|
||||
research_results["history"] = await self._get_history(query, interests, words_per_section)
|
||||
|
||||
if "Culinary" in interests:
|
||||
research_results["culinary"] = await self._get_culinary(query, interests, words_per_section)
|
||||
|
||||
if "Culture" in interests:
|
||||
research_results["culture"] = await self._get_culture(query, interests, words_per_section)
|
||||
|
||||
# Get final tour with only selected interests
|
||||
final_tour = await self._get_final_tour(
|
||||
query,
|
||||
interests,
|
||||
duration,
|
||||
research_results
|
||||
)
|
||||
|
||||
self.printer.update_item("final_report", "", is_done=True)
|
||||
|
||||
self.printer.end()
|
||||
|
||||
tour_intro = final_tour.introduction
|
||||
architecture = final_tour.architecture
|
||||
history = final_tour.history
|
||||
culinary = final_tour.culinary
|
||||
culture = final_tour.culture
|
||||
conclusion = final_tour.conclusion
|
||||
|
||||
final = (
|
||||
"INTRODUCTION\n\n"
|
||||
+ tour_intro + "\n\n"
|
||||
|
||||
+ "ARCHITECTURE\n\n"
|
||||
+ architecture + "\n\n"
|
||||
|
||||
+ "HISTORY\n\n"
|
||||
+ history + "\n\n"
|
||||
|
||||
+ "CULTURE\n\n"
|
||||
+ culture + "\n\n"
|
||||
|
||||
+ "CULINARY\n\n"
|
||||
+ culinary + "\n\n"
|
||||
|
||||
+ "CONCLUSION\n\n"
|
||||
+ conclusion
|
||||
)
|
||||
# Build final tour content based on selected interests
|
||||
sections = []
|
||||
|
||||
# Add selected interest sections without headers
|
||||
if "Architecture" in interests:
|
||||
sections.append(final_tour.architecture)
|
||||
if "History" in interests:
|
||||
sections.append(final_tour.history)
|
||||
if "Culture" in interests:
|
||||
sections.append(final_tour.culture)
|
||||
if "Culinary" in interests:
|
||||
sections.append(final_tour.culinary)
|
||||
|
||||
# Format final tour with natural transitions
|
||||
final = ""
|
||||
for i, content in enumerate(sections):
|
||||
if i > 0:
|
||||
final += "\n\n" # Add spacing between sections
|
||||
final += content
|
||||
|
||||
return final
|
||||
|
||||
async def _get_plan(self, query: str, interest: str, duration: str) -> Planner:
|
||||
self.printer.update_item("Planner", "Getting Time allocation for each section")
|
||||
result = await Runner.run(planner_agent, f"Query: {query} Interest: {interest} Duration: {duration}")
|
||||
async def _get_plan(self, query: str, interests: list, duration: str) -> Planner:
|
||||
self.printer.update_item("Planner", "Planning your personalized tour...")
|
||||
result = await Runner.run(
|
||||
planner_agent,
|
||||
"Query: {} Interests: {} Duration: {}".format(query, ', '.join(interests), duration)
|
||||
)
|
||||
self.printer.update_item(
|
||||
"Planner",
|
||||
f"Completed planning",
|
||||
"Completed planning",
|
||||
is_done=True,
|
||||
)
|
||||
return result.final_output_as(Planner)
|
||||
|
||||
async def _get_history(self, query: str, interest: str, duration: float) -> History:
|
||||
self.printer.update_item("History", "Getting Historical Data for Location")
|
||||
word_limit = int(duration) * 120
|
||||
result = await Runner.run(historical_agent, f"Query: {query} Interest: {interest} Word Limit: {word_limit} - {word_limit + 20}")
|
||||
async def _get_history(self, query: str, interests: list, word_limit: int) -> History:
|
||||
self.printer.update_item("History", "Researching historical highlights...")
|
||||
result = await Runner.run(
|
||||
historical_agent,
|
||||
"Query: {} Interests: {} Word Limit: {} - {}\n\nInstructions: Create engaging historical content for an audio tour. Focus on interesting stories and personal connections. Make it conversational and include specific details that would be interesting to hear while walking. Include specific locations and landmarks where possible. The content should be approximately {} words when spoken at a natural pace.".format(query, ', '.join(interests), word_limit, word_limit + 20, word_limit)
|
||||
)
|
||||
self.printer.update_item(
|
||||
"History",
|
||||
f"Completed history research",
|
||||
"Completed history research",
|
||||
is_done=True,
|
||||
)
|
||||
return result.final_output_as(History)
|
||||
# return result.final_output_as(FinancialSearchPlan)
|
||||
|
||||
async def _get_architecture(self, query: str, interest: str, duration: float):
|
||||
self.printer.update_item("Architecture", "Getting Architectural Data for Location")
|
||||
word_limit = int(duration) * 120
|
||||
result = await Runner.run(architecture_agent, f"Query: {query} Interest: {interest} Word Limit: {word_limit} - {word_limit + 20}")
|
||||
async def _get_architecture(self, query: str, interests: list, word_limit: int):
|
||||
self.printer.update_item("Architecture", "Exploring architectural wonders...")
|
||||
result = await Runner.run(
|
||||
architecture_agent,
|
||||
"Query: {} Interests: {} Word Limit: {} - {}\n\nInstructions: Create engaging architectural content for an audio tour. Focus on visual descriptions and interesting design details. Make it conversational and include specific buildings and their unique features. Describe what visitors should look for and why it matters. The content should be approximately {} words when spoken at a natural pace.".format(query, ', '.join(interests), word_limit, word_limit + 20, word_limit)
|
||||
)
|
||||
self.printer.update_item(
|
||||
"Architecture",
|
||||
f"Completed architecture research",
|
||||
"Completed architecture research",
|
||||
is_done=True,
|
||||
)
|
||||
return result.final_output_as(Architecture)
|
||||
|
||||
async def _get_culinary(self, query: str, interest: str, duration: float):
|
||||
self.printer.update_item("Culinary", "Getting Culinary Data for Location")
|
||||
word_limit = int(duration) * 120
|
||||
result = await Runner.run(culinary_agent, f"Query: {query} Interest: {interest} Word Limit: {word_limit} - {word_limit + 20}")
|
||||
async def _get_culinary(self, query: str, interests: list, word_limit: int):
|
||||
self.printer.update_item("Culinary", "Discovering local flavors...")
|
||||
result = await Runner.run(
|
||||
culinary_agent,
|
||||
"Query: {} Interests: {} Word Limit: {} - {}\n\nInstructions: Create engaging culinary content for an audio tour. Focus on local specialties, food history, and interesting stories about restaurants and dishes. Make it conversational and include specific recommendations. Describe the flavors and cultural significance of the food. The content should be approximately {} words when spoken at a natural pace.".format(query, ', '.join(interests), word_limit, word_limit + 20, word_limit)
|
||||
)
|
||||
self.printer.update_item(
|
||||
"Culinary",
|
||||
f"Completed culinary research",
|
||||
"Completed culinary research",
|
||||
is_done=True,
|
||||
)
|
||||
return result.final_output_as(Culinary)
|
||||
|
||||
async def _get_culture(self, query: str, interest: str, duration: float):
|
||||
self.printer.update_item("Culture", "Getting Cultural Data for Location")
|
||||
word_limit = int(duration) * 120
|
||||
result = await Runner.run(culture_agent, f"Query: {query} Interest: {interest} Word Limit: {word_limit} - {word_limit + 20}")
|
||||
async def _get_culture(self, query: str, interests: list, word_limit: int):
|
||||
self.printer.update_item("Culture", "Exploring cultural highlights...")
|
||||
result = await Runner.run(
|
||||
culture_agent,
|
||||
"Query: {} Interests: {} Word Limit: {} - {}\n\nInstructions: Create engaging cultural content for an audio tour. Focus on local traditions, arts, and community life. Make it conversational and include specific cultural venues and events. Describe the atmosphere and significance of cultural landmarks. The content should be approximately {} words when spoken at a natural pace.".format(query, ', '.join(interests), word_limit, word_limit + 20, word_limit)
|
||||
)
|
||||
self.printer.update_item(
|
||||
"Culture",
|
||||
f"Completed culture research",
|
||||
"Completed culture research",
|
||||
is_done=True,
|
||||
)
|
||||
return result.final_output_as(Culture)
|
||||
|
||||
async def _get_final_tour(self, query: str, interest: str, duration: float, architecture: str, architecture_dur: float, history: str, history_dur: float, culinary: str, culinary_dur: float, culture: str, culture_dur:float):
|
||||
self.printer.update_item("Final Tour", "Getting Final Tour")
|
||||
async def _get_final_tour(self, query: str, interests: list, duration: float, research_results: dict):
|
||||
self.printer.update_item("Final Tour", "Creating your personalized tour...")
|
||||
|
||||
# Build content sections based on selected interests
|
||||
content_sections = []
|
||||
for interest in interests:
|
||||
if interest.lower() in research_results:
|
||||
content_sections.append(research_results[interest.lower()].output)
|
||||
|
||||
# Calculate total words based on duration
|
||||
# Assuming average speaking rate of 150 words per minute
|
||||
words_per_minute = 150
|
||||
total_words = int(duration) * words_per_minute
|
||||
|
||||
# Create the prompt with proper string formatting
|
||||
prompt = (
|
||||
"Query: {}\n"
|
||||
"Selected Interests: {}\n"
|
||||
"Total Tour Duration (in minutes): {}\n"
|
||||
"Target Word Count: {}\n\n"
|
||||
"Content Sections:\n{}\n\n"
|
||||
"Instructions: Create a natural, conversational audio tour that focuses only on the selected interests. "
|
||||
"Make it feel like a friendly guide walking alongside the visitor, sharing interesting stories and insights. "
|
||||
"Use natural transitions between topics and maintain an engaging but relaxed pace. "
|
||||
"Include specific locations and landmarks where possible. "
|
||||
"Add natural pauses and transitions as if walking between locations. "
|
||||
"Use phrases like 'as we walk', 'look to your left', 'notice how', etc. "
|
||||
"Make it interactive and engaging, as if the guide is actually there with the visitor. "
|
||||
"Start with a warm welcome and end with a natural closing thought. "
|
||||
"The total content should be approximately {} words when spoken at a natural pace of 150 words per minute. "
|
||||
"This will ensure the tour lasts approximately {} minutes."
|
||||
).format(
|
||||
query,
|
||||
', '.join(interests),
|
||||
duration,
|
||||
total_words,
|
||||
'\n\n'.join(content_sections),
|
||||
total_words,
|
||||
duration
|
||||
)
|
||||
|
||||
result = await Runner.run(
|
||||
orchestrator_agent,
|
||||
f"""Query: {query}
|
||||
Interest: {interest}
|
||||
Total Tour Duration (in minutes): {duration}
|
||||
|
||||
Word Limit Allocation:
|
||||
- Architecture: {architecture_dur*100}
|
||||
- History: {history_dur*100}
|
||||
- Culture: {culture_dur*100}
|
||||
- Culinary: {culinary_dur*100}
|
||||
|
||||
Content Sections:
|
||||
Architecture:
|
||||
{architecture}
|
||||
|
||||
History:
|
||||
{history}
|
||||
|
||||
Culture:
|
||||
{culture}
|
||||
|
||||
Culinary:
|
||||
{culinary}
|
||||
"""
|
||||
)
|
||||
orchestrator_agent,
|
||||
prompt
|
||||
)
|
||||
|
||||
self.printer.update_item(
|
||||
"Final Tour",
|
||||
f"Completed Final Tour Guide Creation",
|
||||
"Completed Final Tour Guide Creation",
|
||||
is_done=True,
|
||||
)
|
||||
return result.final_output_as(FinalTour)
|
||||
Loading…
Reference in a new issue