File size: 16,727 Bytes
ca7a425 | 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 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 | import gradio as gr
import openai
from openai import OpenAI
import os
import requests
from dotenv import load_dotenv
import asyncio
from openai import AsyncOpenAI
import tempfile
# Load environment variables from .env file
load_dotenv()
# Access the API key
openai_api_key = os.getenv("OPENAI_API_KEY")
YOUTUBE_API_KEY = os.getenv("YOUTUBE_API_KEY")
client = openai.OpenAI(api_key=openai_api_key)
async_client = AsyncOpenAI(api_key=openai_api_key)
async def tts_itinerary(text, instructions, voice="nova"):
from aiohttp import ClientSession
# Use Gradio-safe temp file
with tempfile.NamedTemporaryFile(suffix=".wav", delete=False) as tmpfile:
output_path = tmpfile.name
url = "https://api.openai.com/v1/audio/speech"
headers = {
"Authorization": f"Bearer {openai_api_key}",
"Content-Type": "application/json"
}
payload = {
"model": "gpt-4o-mini-tts",
"input": text,
"voice": voice,
"instructions": instructions,
"response_format": "wav"
}
async with ClientSession() as session:
async with session.post(url, headers=headers, json=payload) as resp:
if resp.status != 200:
error_text = await resp.text()
raise Exception(f"TTS failed: {resp.status} - {error_text}")
with open(output_path, "wb") as f:
while True:
chunk = await resp.content.read(1024)
if not chunk:
break
f.write(chunk)
return output_path
def sync_tts_wrapper(text):
instructions = (
"Voice: Friendly and enthusiastic, like a museum guide talking to kids. "
"Tone: Excited, informative, curious. Delivery: Clear and fun."
)
return asyncio.run(tts_itinerary(text, instructions))
# β¨ Itinerary Generator Logic
def generate_itinerary(age, interests, language, expectations,
learning_goals, eta, estimated_staying_time):
print(f"language is {language}")
system_prompt = (
"You are an advanced AI working for MuseoGo, a platform dedicated to enhancing museum visits using AI. "
"You will speak in the language that the current user provides to you."
"Your main task is to create user-friendly itineraries specifically for The Franklin Institute, "
f"Ensure that the expected number of hours for the exhibits sums to {estimated_staying_time} and does not go over that. "
"factoring in user profiles (age, interests, learning goals, ETA, and estimated staying time). "
"Ensure your responses are accurate, thorough, and easy to understand."
"Be explicit about how your recommendation matches my child's interests, age, and learning goals."
"Provide complete, detailed recommendations for exhibits and scheduling, integrating daily show times."
"You are displaying this directly to the user, don't exclaim in response to the prompt."
"Don't use the word I and instead use second person. Directly address your users. Don't respond to me with \"Certainly\"."
)
user_prompt = (
f"I am planning a trip to The Franklin Institute with my child.\n\n"
f"Child's Age: {age}\n"
f"Interests: {interests}\n"
f"Learning Goals: {learning_goals}\n"
f"Arrival Time (ETA): {eta}\n"
f"Estimated Staying Time: {estimated_staying_time}\n\n"
f"Keep in mind my child's conditions: {expectations}\n\n"
"Generate a personalized museum itinerary tailored to this information. "
"Include:\n"
"- Recommended exhibits in order, including why you chose each exhibit.\n"
"- Time estimates for each exhibit.\n"
"- Integration of daily show schedules.\n"
"- Any additional tips to maximize their visit.\n\n"
f"Please respond to me in {language} for the rest of the conversation.\n"
"Always format your answer as:\n"
"\"Number. Title - explanation\"\n"
)
response = client.chat.completions.create(
model="gpt-3.5-turbo",
messages=[
{"role": "system", "content": system_prompt},
{"role": "user", "content": user_prompt}
]
)
itinerary = response.choices[0].message.content
audio_path = sync_tts_wrapper(itinerary)
return itinerary, gr.update(value=audio_path, visible=True)
# β¨ Knowledge Companion Logic
def answer_question(age, question):
system_prompt = (
"You are an educational AI chatbot for MuseoGo, designed to answer questions from children visiting "
"The Franklin Institute. Your answers must be:\n"
"- Age-appropriate (based on the child's age)\n"
"- Friendly and engaging π\n"
"- Factually correct and easy to understand\n"
"- Focused on science, exhibits, or museum topics\n"
"- Designed to make learning fun and spark curiosity π§ β¨\n\n"
"SAFETY GUIDELINES:\n"
"- If the input includes hate speech, harmful content, or anything inappropriate, DO NOT answer the question.\n"
"- Instead, respond with a kind and firm message like: 'Let's keep things friendly and fun! π I'm here to help with science questions and cool stuff about the museum. If you're curious about something science-y, I'm all ears!'\n"
"- Never repeat or acknowledge harmful content directly.\n\n"
"Use emojis where helpful to make your responses more fun and relatable.\n"
"Always end your answer with a simple multiple-choice question to check understanding."
)
user_prompt = (
f"My child, age {age}, is visiting The Franklin Institute and asks: '{question}'\n\n"
"Please give a clear, fun explanation that matches their age level, and connect it to any related exhibit or science concept in the museum when possible."
)
response = client.chat.completions.create(
model="gpt-3.5-turbo",
messages=[
{"role": "system", "content": system_prompt},
{"role": "user", "content": user_prompt}
]
)
return response.choices[0].message.content
# β¨ Exit Ticket Generator Logic
def generate_exit_ticket(age, exhibits, favorite_part):
system_prompt = (
"You are an AI assistant creating personalized exit tickets for young visitors "
"at The Franklin Institute. Your goal is to reinforce learning by summarizing key takeaways "
"and providing engaging prompts for reflection. Keep the tone friendly, simple, and age-appropriate. "
"Use fun language to celebrate learning and encourage curiosity."
)
user_prompt = (
f"A {age}-year-old child visited The Franklin Institute and explored the following topics:\n"
f"{exhibits}\n\n"
f"Their favorite part of the visit was: {favorite_part}\n\n"
"Create a fun, engaging exit ticket that includes:\n"
"- A summary of what they learned (in simple language for their age).\n"
"- A playful reflection question.\n"
"- A suggested hands-on or creative activity they can try at home to keep learning."
)
response = client.chat.completions.create(
model="gpt-3.5-turbo",
messages=[
{"role": "system", "content": system_prompt},
{"role": "user", "content": user_prompt}
]
)
return response.choices[0].message.content
# def generate_exit_ticket(age, exhibits, favorite_part):
# # Handle both string and list inputs for exhibits
# if isinstance(exhibits, str):
# exhibits_list = [ex.strip() for ex in exhibits.split(",")]
# exhibits_str = exhibits
# elif isinstance(exhibits, list):
# exhibits_list = [ex.strip() for ex in exhibits]
# exhibits_str = ", ".join(exhibits)
# else:
# exhibits_list = []
# exhibits_str = str(exhibits)
# # Generate the exit ticket content
# exit_ticket_content = f"""## What You Learned Today!
# Hey there, future scientist! Today at The Franklin Institute, you explored {exhibits_str}.
# Here's what you might have discovered:
# - {exhibits_list[0] if len(exhibits_list) > 0 else "Science"} is all about...
# {'- ' + exhibits_list[1] + ' which shows...' if len(exhibits_list) > 1 else ''}
# ## Reflection Question
# What was the most surprising thing you learned about {favorite_part}?
# ## Keep Exploring at Home
# Try this fun activity: [simple activity related to {exhibits_list[0] if exhibits_list else 'science'}]
# """
# return exit_ticket_content
def search_youtube_videos(query, CHANNEL_ID="UCpAQimPOzeu_VRWRs_S4cPw"):
"""
Searches YouTube videos from The Franklin Institute channel based on a query
and returns relevant videos with their URLs.
"""
base_url = "https://www.googleapis.com/youtube/v3/search"
params = {
"key": YOUTUBE_API_KEY,
"channelId": CHANNEL_ID,
"part": "snippet",
"q": query,
"maxResults": 3,
"order": "relevance",
"type": "video"
}
try:
response = requests.get(base_url, params=params)
response.raise_for_status()
data = response.json()
results = []
for item in data.get("items", []):
video_id = item["id"]["videoId"]
title = item["snippet"]["title"]
# url = f"https://www.youtube.com/watch?v={video_id}"
results.append((title, video_id))
return results
except Exception as e:
print(f"Error searching YouTube videos: {e}")
return []
def format_embedded_videos(video_data):
"""
Accepts a list of (title, video_id) and returns HTML with videos
in a responsive grid layout that adapts to screen size.
"""
if not video_data:
return ""
html = '''
<style>
.video-grid {
display: flex;
flex-wrap: wrap;
gap: 20px;
justify-content: space-between;
}
.video-item {
flex: 1 1 45%;
min-width: 300px;
}
.video-container {
position: relative;
padding-bottom: 56.25%; /* 16:9 ratio */
height: 0;
overflow: hidden;
}
.video-container iframe {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
border: 0;
}
@media (max-width: 600px) {
.video-item {
flex: 1 1 100%;
}
}
</style>
<div class="video-grid">
'''
for title, video_id in video_data:
html += f"""
<div class="video-item">
<h4>π₯ {title}</h4>
<div class="video-container">
<iframe
src="https://www.youtube.com/embed/{video_id}"
title="{title}"
allow="accelerometer; autoplay; clipboard-write;
encrypted-media; gyroscope; picture-in-picture"
allowfullscreen>
</iframe>
</div>
</div>
"""
html += '</div>'
return html
def create_exit_ticket(age, exhibits, favorite_part):
# Generate the exit ticket content - plain text
exit_ticket_text = generate_exit_ticket(age, exhibits, favorite_part)
# Format the text into HTML-friendly format (replace newlines with <br>)
formatted_text = exit_ticket_text.replace('\n', '<br>')
# Wrap the content in a styled HTML container
exit_ticket_html = f"""
<div style="font-family: Arial, sans-serif; line-height: 1.6;">
<h2>π§Ύ Your Personalized Exit Ticket</h2>
<p>{formatted_text}</p>
</div>
"""
# Get exhibits as a list
exhibits_list = [
ex.strip() for ex in
(exhibits.split(",") if isinstance(exhibits, str) else exhibits)
]
# Prepare embedded video section
video_section = """
<div style="margin-top: 40px;">
<h2>π¬ Recommended Videos from The Franklin Institute</h2>
"""
video_urls = []
for i, exhibit in enumerate(exhibits_list[:3]): # Limit to 3 exhibits
videos = search_youtube_videos(exhibit)
if videos:
if i < 2: # Only embed for first 2 exhibits
video_section += f"<h3>About {exhibit}:</h3>"
video_section += format_embedded_videos(videos[:2])
video_urls.append(videos[0][1]) # Store first video ID
# Pad with None if fewer than 3
while len(video_urls) < 3:
video_urls.append(None)
video_section += "</div>"
# Combine all content
full_exit_ticket = exit_ticket_html + video_section
return full_exit_ticket, video_urls[0], video_urls[1], video_urls[2]
## Gradio Interface
css = """
@import url('https://fonts.googleapis.com/css2?family=Helvetica+Neue:wght@400;700&display=swap');
body, .gradio-container {
font-family: 'Helvetica Neue', Arial, sans-serif !important;
}
}
"""
with gr.Blocks(title="Museum Experience", css=css) as demo:
gr.Markdown("# ποΈ Museum Experience")
with gr.Tabs() as tabs:
with gr.Tab("Itinerary Generator", id=0):
# Kid Information Section
with gr.Group():
gr.Markdown("## πΆ Kid Information")
age = gr.Number(label="Age", value=10, precision=0)
interests = gr.CheckboxGroup(
label="What are the interested topics of your kids? (Select at least one)",
choices=["Space exploration", "Human biology", "Technology and innovation",
"Physics and mechanics", "Environmental science", "History of science"]
)
learning_goals = gr.Textbox(
label="Learning Goals",
placeholder="What do you hope your child will learn from this visit?"
)
# Visit Information Section
with gr.Group():
gr.Markdown("## π Visit Information")
eta = gr.Textbox(
label="Estimated arrival time",
placeholder="Enter time between 9:00 and 17:00",
value="12:00",
)
estimated_staying_time = gr.Radio(
label="Duration",
choices=["1 hour", "2 hours", "3 hours (recommended)", "4 hours", "> 4 hours"],
value="3 hours (recommended)"
)
language = gr.Radio(
label="Language preference",
choices=["English", "Spanish", "French", "Chinese", "Arabic"],
value="English"
)
expectations = gr.Textbox(
label="Other expectations (Optional)",
placeholder="E.g., wheelchair accessibility, quiet areas...",
lines=3
)
itinerary_output = gr.Markdown()
tts_audio_output = gr.Audio(label="π§ Listen to Itinerary", visible=False)
generate_btn = gr.Button("Generate Itinerary", variant="primary")
generate_btn.click(
fn=generate_itinerary,
inputs=[age, interests, language, expectations, learning_goals, eta, estimated_staying_time],
outputs=[itinerary_output, tts_audio_output]
)
with gr.Tab("Learning Companion", id=1):
chatbot = gr.Chatbot(
avatar_images=(None, "museo.png"),
show_copy_button=True,
height="75vh",
show_label=True,
container=True
)
# Create the chat interface with supported parameters
chat_interface = gr.ChatInterface(
fn=answer_question,
chatbot=chatbot,
examples=[
"Why is the sky blue?",
"What is a black hole?",
"What makes a rainbow?"
],
additional_inputs=None,
submit_btn=True,
fill_height=True
)
with gr.Tab("Exit Ticket Generator", id=2):
gr.Markdown("# ποΈ Franklin Institute Exit Ticket Generator")
gr.Markdown("Create personalized exit tickets for young visitors with learning summaries and video recommendations!")
exhibits = gr.CheckboxGroup(
label="What exhibitions did you visit today? (Select at least one)",
choices=["The Giant Heart" , "Your Brain" , "Changing Earth" , "Space Command" , "The Train Factory" , "Sir Isaac's Loft" , "SportsZone"],
interactive=True
)
favorite_part = gr.Textbox(
label="What is your favorite exhibition?",
placeholder="E.g., Space Exploration",
lines=3
)
generate_btn = gr.Button("Generate Exit Ticket", variant="primary")
with gr.Row():
exit_ticket_output = gr.HTML(label="Your Personalized Exit Ticket")
video_outputs = []
for i in range(3):
video_outputs.append(gr.Video(label=f"Recommended Video {i+1}", visible=False))
generate_btn.click(
fn=create_exit_ticket,
inputs=[age, exhibits, favorite_part],
outputs=[exit_ticket_output, *video_outputs]
)
demo.launch() |