import streamlit as st import requests import json from datetime import datetime import os from dotenv import load_dotenv # Load environment variables load_dotenv() # Configuration BACKEND_URL = os.getenv("BACKEND_URL", "http://localhost:8000") # Page configuration st.set_page_config( page_title="CultureBot - Your AI Cultural Guide", page_icon="🌍", layout="wide", initial_sidebar_state="expanded" ) # Custom CSS for beautiful styling st.markdown(""" """, unsafe_allow_html=True) # Sidebar with app information with st.sidebar: st.markdown(""" """, unsafe_allow_html=True) st.markdown("---") # Navigation st.markdown("### 🧭 Navigation") page = st.selectbox( "Choose a page:", ["🏠 Home", "đŸ’Ŧ Chat with CultureBot", "📚 Cultural Facts", "â„šī¸ About"], key="navigation" ) st.markdown("---") # Quick stats st.markdown("### 📊 Quick Stats") try: response = requests.get(f"{BACKEND_URL}/health", timeout=5) if response.status_code == 200: st.success("đŸŸĸ Backend Online") else: st.error("🔴 Backend Offline") except: st.error("🔴 Backend Offline") # Random fact st.markdown("### 🎲 Random Fact") if st.button("Get Random Fact"): try: response = requests.get(f"{BACKEND_URL}/facts/random", timeout=10) if response.status_code == 200: fact = response.json() st.markdown(f"""

{fact['country']}

{fact['fact']}

Category: {fact['category']}
""", unsafe_allow_html=True) except Exception as e: st.error("Unable to fetch random fact") # Main content area if page == "🏠 Home": # Home page st.markdown("""

Welcome to CultureBot

Your AI-powered guide to understanding cultures, traditions, and social customs from around the world

""", unsafe_allow_html=True) # Feature cards col1, col2, col3 = st.columns(3) with col1: st.markdown("""

🌍 Global Insights

Discover fascinating cultural facts and customs from countries across all continents.

""", unsafe_allow_html=True) with col2: st.markdown("""

🤖 AI-Powered Chat

Ask questions and get personalized responses about specific countries and cultural practices.

""", unsafe_allow_html=True) with col3: st.markdown("""

📚 Learn & Explore

Expand your cultural awareness and become a more informed global citizen.

""", unsafe_allow_html=True) st.markdown("---") # Getting started st.markdown("## 🚀 Getting Started") st.markdown(""" 1. **Chat with CultureBot**: Ask questions about any culture or country 2. **Explore Facts**: Browse our curated collection of cultural insights 3. **Learn Continuously**: Discover new perspectives and traditions """) if st.button("Start Chatting Now", type="primary"): st.session_state.navigation = "đŸ’Ŧ Chat with CultureBot" st.rerun() elif page == "đŸ’Ŧ Chat with CultureBot": # Chat page st.markdown("""

Chat with CultureBot

Ask me about any culture, country, or tradition!

""", unsafe_allow_html=True) # Initialize chat history if "messages" not in st.session_state: st.session_state.messages = [] # Display chat messages for message in st.session_state.messages: if message["role"] == "user": st.markdown(f"""
You: {message["content"]}
""", unsafe_allow_html=True) else: st.markdown(f"""
CultureBot: {message["content"]}
""", unsafe_allow_html=True) # Chat input user_input = st.chat_input("Ask about any culture or country...") if user_input: # Add user message to chat history st.session_state.messages.append({"role": "user", "content": user_input}) # Get bot response with st.spinner("CultureBot is thinking..."): try: response = requests.post( f"{BACKEND_URL}/chat", json={"message": user_input}, timeout=30 ) if response.status_code == 200: bot_response = response.json() st.session_state.messages.append({ "role": "assistant", "content": bot_response["response"] }) else: st.session_state.messages.append({ "role": "assistant", "content": "I'm sorry, I'm having trouble connecting to my knowledge base right now. Please try again later." }) except Exception as e: st.session_state.messages.append({ "role": "assistant", "content": "I'm experiencing some technical difficulties. Please make sure the backend server is running and try again." }) st.rerun() # Suggested questions st.markdown("### 💡 Try asking about:") suggestions = [ "Tell me about Japanese business etiquette", "What are some Indian greeting customs?", "How do Germans view punctuality?", "What should I know about dining in France?", "Explain Chinese lucky numbers" ] cols = st.columns(len(suggestions)) for i, suggestion in enumerate(suggestions): with cols[i]: if st.button(suggestion, key=f"suggestion_{i}"): st.session_state.messages.append({"role": "user", "content": suggestion}) st.rerun() elif page == "📚 Cultural Facts": # Cultural facts page st.markdown("""

Cultural Facts Explorer

Browse our curated collection of cultural insights

""", unsafe_allow_html=True) # Filters col1, col2 = st.columns(2) with col1: country_filter = st.selectbox( "Filter by Country:", ["All Countries", "Japan", "India", "Brazil", "Germany", "China", "France", "South Korea", "Mexico", "Egypt", "Russia", "Thailand", "Italy"] ) with col2: category_filter = st.selectbox( "Filter by Category:", ["All Categories", "etiquette", "business", "greeting", "food", "language", "family", "beliefs", "general"] ) # Fetch and display facts try: if country_filter != "All Countries": response = requests.get(f"{BACKEND_URL}/facts/country/{country_filter}", timeout=10) elif category_filter != "All Categories": response = requests.get(f"{BACKEND_URL}/facts/category/{category_filter}", timeout=10) else: # Get random facts for display facts = [] for _ in range(10): fact_response = requests.get(f"{BACKEND_URL}/facts/random", timeout=5) if fact_response.status_code == 200: facts.append(fact_response.json()) for fact in facts: st.markdown(f"""

{fact['country']}

{fact['fact']}

Category: {fact['category']} Source: {fact['source']}
""", unsafe_allow_html=True) if country_filter != "All Countries" or category_filter != "All Categories": if response.status_code == 200: facts = response.json() if facts: for fact in facts: st.markdown(f"""

{fact['country']}

{fact['fact']}

Category: {fact['category']} Source: {fact['source']}
""", unsafe_allow_html=True) else: st.info("No facts found for the selected filters.") else: st.error("Unable to fetch facts from the server.") except Exception as e: st.error("Unable to connect to the backend server. Please make sure it's running.") else: # About page st.markdown("""

About CultureBot

Bridging cultures through AI-powered conversations

""", unsafe_allow_html=True) # Mission and features col1, col2 = st.columns(2) with col1: st.markdown(""" ### đŸŽ¯ Our Mission CultureBot was created to help people understand and appreciate the rich diversity of cultures around the world. In our increasingly connected world, cultural awareness is more important than ever. ### 🔧 Technology Stack - **Frontend**: Streamlit (Python) - **Backend**: FastAPI (Python) - **AI Engine**: OpenAI GPT-3.5-turbo - **Database**: In-memory cultural facts database """) with col2: st.markdown(""" ### ✨ Features - **Global Coverage**: Cultural insights from countries across all continents - **AI-Powered Chat**: Natural conversation interface powered by OpenAI - **Curated Content**: Carefully researched and verified cultural facts - **Smart Responses**: Context-aware answers tailored to your questions - **Real-time Interaction**: Fast and responsive chat experience """) st.markdown("---") # Technical details st.markdown("### đŸ—ī¸ Architecture") st.markdown(""" CultureBot follows a modern microservices architecture: 1. **Streamlit Frontend**: Beautiful, interactive user interface 2. **FastAPI Backend**: High-performance API server 3. **OpenAI Integration**: Advanced natural language processing 4. **Cultural Database**: Curated collection of cultural facts and insights """) # Contact and support st.markdown("### 📞 Support") st.markdown(""" For technical support or cultural content suggestions, please contact: - **Team**: Cultural AI Labs - **Version**: 2.1.0 - **Last Updated**: January 15, 2024 """)