Chap-app / app.py
MuhammadHananKhan123's picture
Update app.py
0a50190 verified
import streamlit as st
from datetime import datetime
# App title
st.title("Chat and Call Application")
# Sidebar for navigation
menu = ["Home", "Add Contact", "Chat", "Call"]
choice = st.sidebar.selectbox("Menu", menu)
# Global variables (for simulation)
if 'contacts' not in st.session_state:
st.session_state['contacts'] = {}
if 'chat_history' not in st.session_state:
st.session_state['chat_history'] = {}
# Home page
if choice == "Home":
st.header("Welcome to the Chat and Call App")
st.write("Use the sidebar to navigate between features.")
# Add Contact page
elif choice == "Add Contact":
st.header("Add a New Contact")
with st.form("add_contact_form"):
name = st.text_input("Contact Name")
phone = st.text_input("Phone Number")
submit = st.form_submit_button("Add Contact")
if submit:
if name and phone:
st.session_state['contacts'][name] = phone
st.success(f"Contact {name} added successfully!")
else:
st.error("Please provide both name and phone number.")
# Chat page
elif choice == "Chat":
st.header("Chat with Your Contacts")
if not st.session_state['contacts']:
st.warning("No contacts available. Please add contacts first.")
else:
contact = st.selectbox("Select a Contact", list(st.session_state['contacts'].keys()))
if contact:
st.write(f"Chatting with: {contact}")
chat_key = contact
# Display chat history
if chat_key not in st.session_state['chat_history']:
st.session_state['chat_history'][chat_key] = []
chat_history = st.session_state['chat_history'][chat_key]
for message in chat_history:
st.write(message)
# Input for sending a message
with st.form("send_message_form"):
message = st.text_input("Enter your message")
send = st.form_submit_button("Send")
if send:
if message:
timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
chat_history.append(f"You ({timestamp}): {message}")
st.session_state['chat_history'][chat_key] = chat_history
st.experimental_rerun()
else:
st.error("Message cannot be empty.")
# Call page
elif choice == "Call":
st.header("Make a Call")
if not st.session_state['contacts']:
st.warning("No contacts available. Please add contacts first.")
else:
contact = st.selectbox("Select a Contact to Call", list(st.session_state['contacts'].keys()))
if contact:
st.write(f"Calling {contact}...")
st.button("End Call", on_click=lambda: st.success(f"Call with {contact} ended."))