Spaces:
Sleeping
Sleeping
| import os | |
| import streamlit as st | |
| from datetime import datetime | |
| from dotenv import load_dotenv | |
| from groq_api import chat_with_groq | |
| # Load environment variables | |
| load_dotenv() | |
| # Streamlit UI Setup | |
| st.set_page_config(page_title="Spotify Songs Chatbot", layout="wide") | |
| # Title with Spotify-related emojis | |
| st.title("π΅ Spotify Songs Chatbot πΆ") | |
| st.write("Ask me about songs, artists, or playlists! π§") | |
| # Chat memory | |
| if "messages" not in st.session_state: | |
| st.session_state.messages = [ | |
| {"role": "assistant", "content": "Hello! Iβm a Spotify Songs Chatbot. I can help you find songs, artists, or playlists for you. What would you like to know? π§", | |
| "timestamp": datetime.now().strftime("%H:%M")} | |
| ] | |
| # Display chat history using st.chat_message | |
| for msg in st.session_state.messages: | |
| with st.chat_message(msg["role"], avatar=("π¦π»" if msg["role"] == "user" else "π€")): | |
| content = msg["content"] | |
| timestamp = msg.get("timestamp", datetime.now().strftime("%H:%M")) | |
| st.markdown(f"{content} \n*({timestamp})*") | |
| # User input field using st.chat_input | |
| user_input = st.chat_input( | |
| "Ask me about songs, artists, playlists! πΈ" | |
| ) | |
| # Handle input submission | |
| if user_input: | |
| timestamp = datetime.now().strftime("%H:%M") | |
| # Add user message to chat | |
| st.session_state.messages.append( | |
| {"role": "user", "content": user_input, "timestamp": timestamp} | |
| ) | |
| with st.chat_message("user", avatar="π¦π»"): | |
| st.markdown(f"{user_input} \n*({timestamp})*") | |
| # Get bot response | |
| bot_reply = chat_with_groq(user_input) | |
| st.session_state.messages.append( | |
| {"role": "assistant", "content": bot_reply, "timestamp": timestamp} | |
| ) | |
| with st.chat_message("assistant", avatar="π€"): | |
| st.markdown(f"{bot_reply} \n*({timestamp})*") |