Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,77 +1,84 @@
|
|
1 |
import streamlit as st
|
2 |
-
import
|
3 |
-
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
#
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
st.
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
contacts[contact_name] = {"number": contact_number, "messages": []}
|
35 |
-
save_contacts(contacts)
|
36 |
-
st.sidebar.success(f"Contact '{contact_name}' added successfully!")
|
37 |
-
else:
|
38 |
-
st.sidebar.error("Please provide both name and number.")
|
39 |
-
|
40 |
-
# Main Section: Chat Functionality
|
41 |
-
st.header("Chat with Your Contacts")
|
42 |
-
|
43 |
-
selected_contact = st.selectbox(
|
44 |
-
"Select a contact to chat with", options=["Select"] + list(contacts.keys())
|
45 |
-
)
|
46 |
-
|
47 |
-
if selected_contact != "Select":
|
48 |
-
contact_data = contacts[selected_contact]
|
49 |
-
|
50 |
-
st.subheader(f"Chat with {selected_contact}")
|
51 |
-
|
52 |
-
# Display chat history
|
53 |
-
chat_history = st.empty()
|
54 |
-
if contact_data["messages"]:
|
55 |
-
chat_history.write("\n".join(contact_data["messages"]))
|
56 |
-
|
57 |
-
# Input message section
|
58 |
-
with st.form("send_message_form"):
|
59 |
-
message = st.text_input("Type your message")
|
60 |
-
send_message = st.form_submit_button("Send")
|
61 |
-
|
62 |
-
if send_message:
|
63 |
-
if message.strip():
|
64 |
-
contact_data["messages"].append(f"You: {message}")
|
65 |
-
save_contacts(contacts)
|
66 |
-
chat_history.write("\n".join(contact_data["messages"]))
|
67 |
else:
|
68 |
-
st.error("
|
69 |
-
|
70 |
-
#
|
71 |
-
|
72 |
-
st.
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import streamlit as st
|
2 |
+
from datetime import datetime
|
3 |
+
|
4 |
+
# App title
|
5 |
+
st.title("Chat and Call Application")
|
6 |
+
|
7 |
+
# Sidebar for navigation
|
8 |
+
menu = ["Home", "Add Contact", "Chat", "Call"]
|
9 |
+
choice = st.sidebar.selectbox("Menu", menu)
|
10 |
+
|
11 |
+
# Global variables (for simulation)
|
12 |
+
if 'contacts' not in st.session_state:
|
13 |
+
st.session_state['contacts'] = {}
|
14 |
+
if 'chat_history' not in st.session_state:
|
15 |
+
st.session_state['chat_history'] = {}
|
16 |
+
|
17 |
+
# Home page
|
18 |
+
if choice == "Home":
|
19 |
+
st.header("Welcome to the Chat and Call App")
|
20 |
+
st.write("Use the sidebar to navigate between features.")
|
21 |
+
|
22 |
+
# Add Contact page
|
23 |
+
elif choice == "Add Contact":
|
24 |
+
st.header("Add a New Contact")
|
25 |
+
with st.form("add_contact_form"):
|
26 |
+
name = st.text_input("Contact Name")
|
27 |
+
phone = st.text_input("Phone Number")
|
28 |
+
submit = st.form_submit_button("Add Contact")
|
29 |
+
|
30 |
+
if submit:
|
31 |
+
if name and phone:
|
32 |
+
st.session_state['contacts'][name] = phone
|
33 |
+
st.success(f"Contact {name} added successfully!")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
34 |
else:
|
35 |
+
st.error("Please provide both name and phone number.")
|
36 |
+
|
37 |
+
# Chat page
|
38 |
+
elif choice == "Chat":
|
39 |
+
st.header("Chat with Your Contacts")
|
40 |
+
|
41 |
+
if not st.session_state['contacts']:
|
42 |
+
st.warning("No contacts available. Please add contacts first.")
|
43 |
+
else:
|
44 |
+
contact = st.selectbox("Select a Contact", list(st.session_state['contacts'].keys()))
|
45 |
+
|
46 |
+
if contact:
|
47 |
+
st.write(f"Chatting with: {contact}")
|
48 |
+
chat_key = contact
|
49 |
+
|
50 |
+
# Display chat history
|
51 |
+
if chat_key not in st.session_state['chat_history']:
|
52 |
+
st.session_state['chat_history'][chat_key] = []
|
53 |
+
|
54 |
+
chat_history = st.session_state['chat_history'][chat_key]
|
55 |
+
for message in chat_history:
|
56 |
+
st.write(message)
|
57 |
+
|
58 |
+
# Input for sending a message
|
59 |
+
with st.form("send_message_form"):
|
60 |
+
message = st.text_input("Enter your message")
|
61 |
+
send = st.form_submit_button("Send")
|
62 |
+
|
63 |
+
if send:
|
64 |
+
if message:
|
65 |
+
timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
66 |
+
chat_history.append(f"You ({timestamp}): {message}")
|
67 |
+
st.session_state['chat_history'][chat_key] = chat_history
|
68 |
+
st.experimental_rerun()
|
69 |
+
else:
|
70 |
+
st.error("Message cannot be empty.")
|
71 |
+
|
72 |
+
# Call page
|
73 |
+
elif choice == "Call":
|
74 |
+
st.header("Make a Call")
|
75 |
+
|
76 |
+
if not st.session_state['contacts']:
|
77 |
+
st.warning("No contacts available. Please add contacts first.")
|
78 |
+
else:
|
79 |
+
contact = st.selectbox("Select a Contact to Call", list(st.session_state['contacts'].keys()))
|
80 |
+
|
81 |
+
if contact:
|
82 |
+
st.write(f"Calling {contact}...")
|
83 |
+
st.button("End Call", on_click=lambda: st.success(f"Call with {contact} ended."))
|
84 |
+
|