Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,37 +1,77 @@
|
|
1 |
import streamlit as st
|
2 |
-
import
|
3 |
|
4 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
5 |
st.set_page_config(page_title="Chat App", layout="wide")
|
|
|
6 |
st.title("Chat App")
|
7 |
-
st.markdown("This is a simple chat application similar to WhatsApp.")
|
8 |
|
9 |
-
#
|
10 |
-
|
11 |
-
|
|
|
|
|
|
|
12 |
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
st.
|
18 |
else:
|
19 |
-
st.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
20 |
|
21 |
-
#
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
|
26 |
-
#
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
|
|
|
|
|
|
35 |
|
36 |
-
#
|
37 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import streamlit as st
|
2 |
+
import json
|
3 |
|
4 |
+
def load_contacts():
|
5 |
+
"""Load contacts from a JSON file."""
|
6 |
+
try:
|
7 |
+
with open("contacts.json", "r") as f:
|
8 |
+
return json.load(f)
|
9 |
+
except FileNotFoundError:
|
10 |
+
return {}
|
11 |
+
|
12 |
+
def save_contacts(contacts):
|
13 |
+
"""Save contacts to a JSON file."""
|
14 |
+
with open("contacts.json", "w") as f:
|
15 |
+
json.dump(contacts, f)
|
16 |
+
|
17 |
+
# Load existing contacts
|
18 |
+
contacts = load_contacts()
|
19 |
+
|
20 |
+
# Streamlit app configuration
|
21 |
st.set_page_config(page_title="Chat App", layout="wide")
|
22 |
+
|
23 |
st.title("Chat App")
|
|
|
24 |
|
25 |
+
# Sidebar: Add Contact Section
|
26 |
+
st.sidebar.header("Add a New Contact")
|
27 |
+
with st.sidebar.form("add_contact_form"):
|
28 |
+
contact_name = st.text_input("Contact Name")
|
29 |
+
contact_number = st.text_input("Phone Number")
|
30 |
+
submit_contact = st.form_submit_button("Add Contact")
|
31 |
|
32 |
+
if submit_contact:
|
33 |
+
if contact_name and contact_number:
|
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("Message cannot be empty.")
|
69 |
|
70 |
+
# Footer
|
71 |
+
st.sidebar.markdown("---")
|
72 |
+
st.sidebar.markdown("### Instructions")
|
73 |
+
st.sidebar.markdown(
|
74 |
+
"1. Use the sidebar to add new contacts.\n"
|
75 |
+
"2. Select a contact from the dropdown to start chatting.\n"
|
76 |
+
"3. Your chat history is saved locally."
|
77 |
+
)
|