Spaces:
Sleeping
Sleeping
File size: 1,839 Bytes
601d457 |
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 |
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})*") |