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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +82 -75
app.py CHANGED
@@ -1,77 +1,84 @@
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
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+