MuhammadHananKhan123 commited on
Commit
04345b7
·
verified ·
1 Parent(s): 1c744f3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +66 -26
app.py CHANGED
@@ -1,37 +1,77 @@
1
  import streamlit as st
2
- import time
3
 
4
- # App layout
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
- # Initialize session state to store chat messages
10
- if "messages" not in st.session_state:
11
- st.session_state.messages = []
 
 
 
12
 
13
- # Function to display chat messages
14
- def display_messages():
15
- for message in st.session_state.messages:
16
- if message["user"] == "You":
17
- st.markdown(f"**You:** {message['text']}")
18
  else:
19
- st.markdown(f"**Friend:** {message['text']}")
 
 
 
 
 
 
 
 
 
 
 
 
20
 
21
- # Input area
22
- with st.form(key="chat_form"):
23
- user_input = st.text_input("Enter your message:", "")
24
- submitted = st.form_submit_button("Send")
25
 
26
- # Handle message submission
27
- if submitted and user_input.strip():
28
- # Append user message
29
- st.session_state.messages.append({"user": "You", "text": user_input})
30
 
31
- # Simulate a friend response after a short delay
32
- time.sleep(1)
33
- friend_response = f"I received: {user_input}"
34
- st.session_state.messages.append({"user": "Friend", "text": friend_response})
 
 
 
35
 
36
- # Display chat messages
37
- display_messages()
 
 
 
 
 
 
 
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
+ )