Download app.py from dxdoan/WC: direct link, hf CLI and curl.
- Browser
- Download file 12.6 kB
-
https://huggingface.co/spaces/dxdoan/WC/resolve/main/app.py
- Command line
-
hf download hf://spaces/dxdoan/WC/app.py
-
curl -L -o app.py https://huggingface.co/spaces/dxdoan/WC/resolve/main/app.py
12.6 kB
| import streamlit as st | |
| import requests | |
| import os | |
| from dotenv import load_dotenv | |
| from media_config import AUDIO_URLS | |
| from environments import get_current_config, get_environment, get_environment_display_info | |
| load_dotenv() | |
| # Get current environment configuration | |
| ENVIRONMENT = get_environment() | |
| config = get_current_config() | |
| env_info = get_environment_display_info() | |
| # Use configuration for URLs | |
| AGENT_URL = config["agent_url"] | |
| MCP_SERVER_URL = config["mcp_server_url"] | |
| # API Key for agent authentication (set in HF Space secrets for production) | |
| # AGENT_API_KEY is optional for local development | |
| st.set_page_config( | |
| page_title="World Cup AI Assistant (Beta)", | |
| page_icon="π", | |
| layout="wide", # Use full width | |
| initial_sidebar_state="collapsed" # Hide sidebar to save space | |
| ) | |
| # Initialize session state | |
| if "chat_history" not in st.session_state: | |
| st.session_state.chat_history = [] | |
| # Enhanced CSS with background styling | |
| st.markdown(""" | |
| <style> | |
| /* Force white background for HF Spaces compatibility */ | |
| .stApp, .main, [data-testid="stAppViewContainer"], [data-testid="stHeader"] { | |
| background: #FFFFFF !important; | |
| } | |
| /* Main background with World Cup theme - more specific selectors */ | |
| .main, [data-testid="stAppViewContainer"] > .main { | |
| background: #FFFFFF !important; | |
| min-height: 100vh; | |
| } | |
| /* Overlay for better text readability */ | |
| .main::before { | |
| content: ''; | |
| position: fixed; | |
| top: 0; | |
| left: 0; | |
| width: 100%; | |
| height: 100%; | |
| background: rgba(0, 0, 0, 0.4); | |
| z-index: -1; | |
| } | |
| /* Enhanced prompt cards */ | |
| .prompt-card { | |
| background: rgba(255, 255, 255, 0.95); | |
| border: 2px solid #ffd700; | |
| border-radius: 12px; | |
| padding: 16px; | |
| margin: 8px 0; | |
| transition: all 0.3s ease; | |
| box-shadow: 0 4px 15px rgba(0, 0, 0, 0.2); | |
| } | |
| .prompt-card:hover { | |
| background: rgba(255, 255, 255, 1); | |
| border-color: #ffed4e; | |
| transform: translateY(-2px); | |
| box-shadow: 0 8px 25px rgba(0, 0, 0, 0.3); | |
| } | |
| .prompt-title { | |
| font-weight: bold; | |
| color: #1e3c72; | |
| margin-bottom: 6px; | |
| font-size: 1.1em; | |
| } | |
| .prompt-description { | |
| color: #495057; | |
| font-size: 0.9em; | |
| margin-bottom: 8px; | |
| line-height: 1.3; | |
| } | |
| .prompt-template { | |
| color: #007bff; | |
| font-size: 0.85em; | |
| background: rgba(0, 123, 255, 0.1); | |
| padding: 6px 10px; | |
| border-radius: 6px; | |
| margin: 4px 0; | |
| border-left: 4px solid #007bff; | |
| line-height: 1.4; | |
| } | |
| .prompt-template-placeholder { | |
| color: #dc3545; | |
| font-weight: bold; | |
| } | |
| /* Enhanced title styling - Cross-browser compatible for HF Spaces */ | |
| .main-title { | |
| /* Use solid gold color that works everywhere */ | |
| color: #ffd700 !important; | |
| /* Multiple text shadows for depth and glow effect */ | |
| text-shadow: | |
| 2px 2px 4px rgba(0, 0, 0, 0.8), | |
| 0 0 10px rgba(255, 215, 0, 0.5), | |
| 0 0 20px rgba(255, 215, 0, 0.3), | |
| 3px 3px 6px rgba(45, 95, 63, 0.6) !important; | |
| font-weight: 900 !important; | |
| font-size: 2.5rem !important; | |
| /* Add subtle animation */ | |
| animation: goldenGlow 3s ease-in-out infinite alternate; | |
| } | |
| @keyframes goldenGlow { | |
| from { | |
| text-shadow: | |
| 2px 2px 4px rgba(0, 0, 0, 0.8), | |
| 0 0 10px rgba(255, 215, 0, 0.5), | |
| 0 0 20px rgba(255, 215, 0, 0.3), | |
| 3px 3px 6px rgba(45, 95, 63, 0.6); | |
| } | |
| to { | |
| text-shadow: | |
| 2px 2px 4px rgba(0, 0, 0, 0.8), | |
| 0 0 15px rgba(255, 215, 0, 0.7), | |
| 0 0 30px rgba(255, 215, 0, 0.5), | |
| 3px 3px 8px rgba(45, 95, 63, 0.8); | |
| } | |
| } | |
| /* Enhanced input area */ | |
| .stTextArea textarea { | |
| background: rgba(255, 255, 255, 0.95); | |
| border: 2px solid #ffd700; | |
| border-radius: 8px; | |
| font-size: 16px; | |
| } | |
| /* Enhanced button styling */ | |
| .stButton > button, .stFormSubmitButton > button { | |
| background: linear-gradient(45deg, #ffd700, #ffed4e) !important; | |
| border: none !important; | |
| border-radius: 8px !important; | |
| color: #1e3c72 !important; | |
| font-weight: bold !important; | |
| padding: 10px 20px !important; | |
| transition: all 0.3s ease !important; | |
| width: auto !important; | |
| max-width: 200px !important; | |
| } | |
| .stButton > button:hover, .stFormSubmitButton > button:hover { | |
| background: linear-gradient(45deg, #ffed4e, #ffd700) !important; | |
| transform: translateY(-2px) !important; | |
| box-shadow: 0 4px 15px rgba(255, 215, 0, 0.4) !important; | |
| } | |
| /* Custom key handler styles */ | |
| #submit-hint { | |
| font-size: 0.8em; | |
| color: #666; | |
| margin-top: 5px; | |
| font-style: italic; | |
| } | |
| /* Book series section responsive design */ | |
| @media (max-width: 768px) { | |
| .book-series-container { | |
| padding: 15px !important; | |
| margin: 15px 0 !important; | |
| } | |
| .book-series-title { | |
| font-size: 1.5em !important; | |
| } | |
| .book-series-buttons { | |
| flex-direction: column !important; | |
| gap: 10px !important; | |
| } | |
| .book-series-button { | |
| width: 100% !important; | |
| text-align: center !important; | |
| } | |
| } | |
| /* Enhanced book series styling for better visibility */ | |
| .book-series-section { | |
| animation: fadeInUp 0.8s ease-out; | |
| } | |
| @keyframes fadeInUp { | |
| from { | |
| opacity: 0; | |
| transform: translateY(30px); | |
| } | |
| to { | |
| opacity: 1; | |
| transform: translateY(0); | |
| } | |
| } | |
| /* Hover effects for book series elements */ | |
| .book-series-card:hover { | |
| transform: translateY(-5px); | |
| box-shadow: 0 12px 30px rgba(0, 0, 0, 0.4); | |
| } | |
| </style> | |
| """, unsafe_allow_html=True) | |
| # Main content with enhanced styling | |
| st.markdown(''' | |
| <div style="display: flex; align-items: center; justify-content: center; gap: 15px;"> | |
| <h1 class="main-title" style="margin: 0;">π World Cup AI Assistant</h1> | |
| <span style=" | |
| background: linear-gradient(135deg, #667eea 0%, #764ba2 100%); | |
| color: white; | |
| padding: 5px 15px; | |
| border-radius: 20px; | |
| font-size: 14px; | |
| font-weight: bold; | |
| position: relative; | |
| top: -10px; | |
| ">BETA</span> | |
| </div> | |
| ''', unsafe_allow_html=True) | |
| # Beta notice banner | |
| st.info(""" | |
| **π§ Beta Version** - We're actively improving this app. Some features are still in development. | |
| Your feedback helps us make it better! Known limitations: Agent memory is missing, and response times may vary. | |
| """) | |
| # Environment badge (hidden in production) | |
| if ENVIRONMENT != "prod": | |
| st.markdown(f""" | |
| <div style="display: inline-block; margin-bottom: 15px;"> | |
| <span style=" | |
| background: {env_info['color']}; | |
| color: white; | |
| padding: 4px 12px; | |
| border-radius: 20px; | |
| font-size: 0.8em; | |
| font-weight: bold; | |
| display: inline-flex; | |
| align-items: center; | |
| gap: 5px; | |
| "> | |
| {env_info['icon']} {env_info['name']} | |
| </span> | |
| <span style=" | |
| color: #6c757d; | |
| font-size: 0.8em; | |
| margin-left: 10px; | |
| font-style: italic; | |
| "> | |
| {env_info['description']} | |
| </span> | |
| </div> | |
| """, unsafe_allow_html=True) | |
| st.write(f"Ask any question about World Cup history and get an AI-powered answer!") | |
| # Book Series Section - Toward World Cup 2026 (Clean Integration) | |
| # Header with NEW badge and title | |
| col1, col2 = st.columns([1, 4]) | |
| with col1: | |
| st.markdown('<div style="background: #ffd700; color: #1e3c72; padding: 4px 8px; border-radius: 12px; font-size: 0.7em; font-weight: bold; text-align: center;">π NEW</div>', unsafe_allow_html=True) | |
| with col2: | |
| st.markdown('<h4 style="color: #495057; margin: 0; font-size: 1.1em; font-weight: 600;">Book Series: Toward World Cup 2026</h4>', unsafe_allow_html=True) | |
| # Book description | |
| st.markdown('<p style="color: #6c757d; margin: 10px 0; font-size: 0.9em; line-height: 1.4;"><strong>Book 1: Lamine Yamal</strong> - A Golden Boy Who Becomes a Record-Breaking Star: Lessons for Kids</p>', unsafe_allow_html=True) | |
| # Buttons and teaser | |
| col1, col2, col3 = st.columns([1, 1, 2]) | |
| with col1: | |
| st.link_button("π Amazon", "https://www.amazon.com/Yamal-Golden-Becomes-Record-Breaking-Lessons/dp/B0DPCRK64H/ref=tmm_pap_swatch_0?_encoding=UTF8&dib_tag=se&dib=eyJ2IjoiMSJ9.oXOBH7gVGsbO-2qUWdAdBA.qtAKMcQoOdUgIdIDUHYywjKRDvtOJsZTgn5Pe6ds7bo&qid=1755559111&sr=8-1", use_container_width=True) | |
| with col2: | |
| st.link_button("π Blog", "https://dzungdoan.com/2025/08/18/toward-world-cup-2026-book-series-first-book-lamine-yamal/", use_container_width=True) | |
| with col3: | |
| st.markdown('<p style="color: #6c757d; font-size: 0.8em; font-style: italic; margin: 0; padding: 8px 0;">More books coming soon! π</p>', unsafe_allow_html=True) | |
| # Background music (always visible audio player) | |
| # Removed HEAD request for security - just embed directly | |
| st.markdown(f""" | |
| <div style="margin: 10px 0; padding: 15px; background: #f8f9fa; border-radius: 8px; border: 1px solid #dee2e6;"> | |
| <h4 style="margin: 0 0 10px 0; color: #495057;">π΅ Background Music</h4> | |
| <audio controls style="width: 100%;"> | |
| <source src="{AUDIO_URLS['world_cup_1990_theme']}" type="audio/mp3"> | |
| Your browser does not support the audio element. | |
| </audio> | |
| <p style="margin: 5px 0 0 0; font-size: 0.8em; color: #6c757d;">World Cup 1990 Theme</p> | |
| </div> | |
| """, unsafe_allow_html=True) | |
| # Fetch available prompts from MCP server | |
| # Cache for 5 minutes | |
| def get_available_prompts(): | |
| """Fetch available prompts from MCP server""" | |
| try: | |
| response = requests.get(f"{MCP_SERVER_URL}/prompts", timeout=5) | |
| if response.status_code == 200: | |
| return response.json().get("prompts", []) | |
| else: | |
| return [] | |
| except Exception as e: | |
| st.warning(f"Could not fetch prompts: {e}") | |
| return [] | |
| # Get prompts | |
| prompts = get_available_prompts() | |
| # Main layout with input and prompts side by side | |
| col1, col2 = st.columns([2, 1]) # 2:1 ratio for better space utilization in wide layout | |
| with col1: | |
| # Main input area with form for proper submission | |
| with st.form(key="question_form", clear_on_submit=True): | |
| user_input = st.text_area("Your question:", height=100, placeholder="Type your question here...") | |
| submit_button = st.form_submit_button("Ask") | |
| if submit_button and user_input: | |
| with st.spinner("Thinking..."): | |
| try: | |
| # Get API key from environment (optional for local development) | |
| api_key = os.getenv("AGENT_API_KEY") | |
| headers = {} | |
| if api_key: | |
| # Production mode - include API key | |
| headers = {"Authorization": f"Bearer {api_key}"} | |
| else: | |
| # Local development mode - no API key required | |
| st.info("π Local development mode - no API key required") | |
| response = requests.post( | |
| f"{AGENT_URL}/ask", | |
| json={"question": user_input}, | |
| headers=headers, | |
| timeout=15 | |
| ) | |
| response.raise_for_status() | |
| answer = response.json().get("answer", "No answer received.") | |
| except Exception as e: | |
| answer = f"Error: {e}" | |
| st.session_state.chat_history.append((user_input, answer)) | |
| # Chat history | |
| if st.session_state.chat_history: | |
| st.subheader("Chat History") | |
| for q, a in reversed(st.session_state.chat_history): | |
| st.markdown(f"**You:** {q}") | |
| st.markdown(f"**AI:** {a}") | |
| st.markdown("---") | |
| with col2: | |
| # Display prompts if available | |
| if prompts: | |
| st.markdown("### π‘ **What can I help you with?**") | |
| for prompt in prompts: | |
| with st.container(): | |
| # Highlight placeholders in templates | |
| templates_html = "" | |
| for template in prompt['templates']: | |
| # Replace [placeholder] with highlighted version | |
| highlighted_template = template.replace('[', '<span class="prompt-template-placeholder">[').replace(']', ']</span>') | |
| templates_html += f'<div class="prompt-template">π¬ {highlighted_template}</div>' | |
| st.markdown(f""" | |
| <div class="prompt-card"> | |
| <div class="prompt-title">{prompt['title']}</div> | |
| <div class="prompt-description">{prompt['description']}</div> | |
| {templates_html} | |
| </div> | |
| """, unsafe_allow_html=True) |