File size: 13,897 Bytes
ebf1310
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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("""

<style>

    .main-header {

        background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);

        padding: 2rem;

        border-radius: 10px;

        color: white;

        text-align: center;

        margin-bottom: 2rem;

    }

    

    .sidebar-info {

        background: linear-gradient(135deg, #f093fb 0%, #f5576c 100%);

        padding: 1.5rem;

        border-radius: 10px;

        color: white;

        margin-bottom: 1rem;

    }

    

    .chat-message {

        padding: 1rem;

        border-radius: 10px;

        margin: 0.5rem 0;

    }

    

    .user-message {

        background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);

        color: white;

        margin-left: 2rem;

    }

    

    .bot-message {

        background: #f8f9fa;

        color:#333;

        border-left: 4px solid #667eea;

        margin-right: 2rem;

    }

    

    .fact-card {

        background: linear-gradient(135deg, #ffd3a5 0%, #fd6585 100%);

        color:black;

        padding: 1.5rem;

        border-radius: 10px;

        margin: 1rem 0;

        box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);

    }

    

    .metric-card {

        background: white;

        color:black;

        padding: 1rem;

        border-radius: 8px;

        box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);

        text-align: center;

    }

</style>

""", unsafe_allow_html=True)

# Sidebar with app information
with st.sidebar:
    st.markdown("""

    <div class="sidebar-info">

        <h1 style="font-size: 2.5rem; font-weight: bold; margin-bottom: 1rem;">CultureBot</h1>

        <div style="font-size: 0.9rem; opacity: 0.9;">

            <p><strong>Team:</strong>  CultureCoders</p>

            <p><strong>Version:</strong> 1.0</p>

            <p><strong>Created:</strong> June 2025</p>

        </div>

    </div>

    """, 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"""

                <div class="fact-card">

                    <h4>{fact['country']}</h4>

                    <p>{fact['fact']}</p>

                    <small>Category: {fact['category']}</small>

                </div>

                """, 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("""

    <div class="main-header">

        <h1 style="font-size: 3rem; margin-bottom: 1rem;">Welcome to CultureBot</h1>

        <p style="font-size: 1.2rem; opacity: 0.9;">Your AI-powered guide to understanding cultures, traditions, and social customs from around the world</p>

    </div>

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

        <div class="metric-card">

            <h3>🌍 Global Insights</h3>

            <p>Discover fascinating cultural facts and customs from countries across all continents.</p>

        </div>

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

        <div class="metric-card">

            <h3>πŸ€– AI-Powered Chat</h3>

            <p>Ask questions and get personalized responses about specific countries and cultural practices.</p>

        </div>

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

        <div class="metric-card">

            <h3>πŸ“š Learn & Explore</h3>

            <p>Expand your cultural awareness and become a more informed global citizen.</p>

        </div>

        """, 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("""

    <div class="main-header">

        <h1>Chat with CultureBot</h1>

        <p>Ask me about any culture, country, or tradition!</p>

    </div>

    """, 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"""

            <div class="chat-message user-message">

                <strong>You:</strong> {message["content"]}

            </div>

            """, unsafe_allow_html=True)
        else:
            st.markdown(f"""

            <div class="chat-message bot-message">

                <strong>CultureBot:</strong> {message["content"]}

            </div>

            """, 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("""

    <div class="main-header">

        <h1>Cultural Facts Explorer</h1>

        <p>Browse our curated collection of cultural insights</p>

    </div>

    """, 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"""

                <div class="fact-card">

                    <h3>{fact['country']}</h3>

                    <p>{fact['fact']}</p>

                    <div style="display: flex; justify-content: space-between; margin-top: 1rem;">

                        <small><strong>Category:</strong> {fact['category']}</small>

                        <small><strong>Source:</strong> {fact['source']}</small>

                    </div>

                </div>

                """, 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"""

                        <div class="fact-card">

                            <h3>{fact['country']}</h3>

                            <p>{fact['fact']}</p>

                            <div style="display: flex; justify-content: space-between; margin-top: 1rem;">

                                <small><strong>Category:</strong> {fact['category']}</small>

                                <small><strong>Source:</strong> {fact['source']}</small>

                            </div>

                        </div>

                        """, 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("""

    <div class="main-header">

        <h1>About CultureBot</h1>

        <p>Bridging cultures through AI-powered conversations</p>

    </div>

    """, 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

    """)