Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,133 +2,57 @@ import streamlit as st
|
|
| 2 |
from datetime import datetime, timedelta
|
| 3 |
import random
|
| 4 |
import logging
|
| 5 |
-
from huggingface_hub import InferenceClient
|
| 6 |
-
import os
|
| 7 |
|
| 8 |
-
#
|
| 9 |
-
|
| 10 |
-
st.
|
| 11 |
|
| 12 |
-
#
|
| 13 |
-
|
|
|
|
|
|
|
| 14 |
|
| 15 |
-
#
|
| 16 |
-
|
| 17 |
-
if not access_token:
|
| 18 |
-
st.warning("Hugging Face authentication token not found.")
|
| 19 |
-
client = InferenceClient(
|
| 20 |
-
model="NousResearch/Hermes-3-Llama-3.1-8B",
|
| 21 |
-
token=access_token,
|
| 22 |
-
timeout=60.0
|
| 23 |
-
)
|
| 24 |
|
| 25 |
# Helper functions
|
| 26 |
def generate_numeric_id():
|
| 27 |
return int(datetime.now().timestamp() * 1000)
|
| 28 |
|
| 29 |
-
|
| 30 |
-
today = datetime.now().date()
|
| 31 |
-
start_of_week = today - timedelta(days=today.weekday())
|
| 32 |
-
end_of_week = start_of_week + timedelta(days=6)
|
| 33 |
-
|
| 34 |
-
filtered_tasks = []
|
| 35 |
-
for task in st.session_state.user_tasks.values():
|
| 36 |
-
task_date = datetime.strptime(task['date'], '%Y-%m-%d').date()
|
| 37 |
-
if section == "myDay" and task_date == today:
|
| 38 |
-
filtered_tasks.append(task)
|
| 39 |
-
elif section == "thisWeek" and start_of_week <= task_date <= end_of_week:
|
| 40 |
-
filtered_tasks.append(task)
|
| 41 |
-
else:
|
| 42 |
-
filtered_tasks.append(task)
|
| 43 |
-
return filtered_tasks
|
| 44 |
-
|
| 45 |
-
def check_deadlines():
|
| 46 |
-
today = datetime.now().date()
|
| 47 |
-
return [task for task in st.session_state.user_tasks.values() if today <= datetime.strptime(task['date'], '%Y-%m-%d').date() <= today + timedelta(days=2)]
|
| 48 |
-
|
| 49 |
-
def alert_user(upcoming_tasks):
|
| 50 |
-
procrastination_messages = []
|
| 51 |
-
# Sample procrastination prompts
|
| 52 |
-
procrastination_prompts = [
|
| 53 |
-
"Take a break! You can do '{task}' later.",
|
| 54 |
-
"Why not relax for a while? '{task}' isn't going anywhere.",
|
| 55 |
-
"You've earned a rest! Save '{task}' for later.",
|
| 56 |
-
"Maybe watch a quick video before starting '{task}'?",
|
| 57 |
-
"A quick snack could be just what you need before tackling '{task}'.",
|
| 58 |
-
]
|
| 59 |
-
|
| 60 |
-
for task in upcoming_tasks:
|
| 61 |
-
prompt = random.choice(procrastination_prompts).format(task=task['text'])
|
| 62 |
-
try:
|
| 63 |
-
response = client.chat_completion(
|
| 64 |
-
messages=[{"role": "user", "content": prompt}],
|
| 65 |
-
max_tokens=200
|
| 66 |
-
)
|
| 67 |
-
procrastination_message = response.choices[0].message.content
|
| 68 |
-
except:
|
| 69 |
-
logging.warning("Failed to connect to chat client.")
|
| 70 |
-
procrastination_message = "Unable to retrieve response."
|
| 71 |
-
procrastination_messages.append(procrastination_message)
|
| 72 |
-
return procrastination_messages
|
| 73 |
-
|
| 74 |
-
# Initialize session state variables
|
| 75 |
-
if "logged_in" not in st.session_state:
|
| 76 |
-
st.session_state.logged_in = False
|
| 77 |
-
if "user_tasks" not in st.session_state:
|
| 78 |
-
st.session_state.user_tasks = {}
|
| 79 |
-
|
| 80 |
-
# Display tasks function
|
| 81 |
-
def display_tasks(tasks):
|
| 82 |
-
for task in tasks:
|
| 83 |
-
col1, col2, col3 = st.columns([5, 1, 1])
|
| 84 |
-
with col1:
|
| 85 |
-
st.markdown(f"**{task['text']}** - Due: {task['date']} - {'Completed' if task['completed'] else 'Pending'}")
|
| 86 |
-
with col2:
|
| 87 |
-
if st.button("Toggle Complete", key=f"toggle_{task['id']}"):
|
| 88 |
-
task["completed"] = not task["completed"]
|
| 89 |
-
st.experimental_rerun()
|
| 90 |
-
with col3:
|
| 91 |
-
if st.button("Delete", key=f"delete_{task['id']}"):
|
| 92 |
-
del st.session_state.user_tasks[task["id"]]
|
| 93 |
-
st.experimental_rerun()
|
| 94 |
-
|
| 95 |
-
# Login Page
|
| 96 |
def login():
|
| 97 |
-
st.
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
if
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
st.warning("Please enter both name and email.")
|
| 110 |
|
| 111 |
-
# Logout function
|
| 112 |
# Logout function
|
| 113 |
def logout():
|
| 114 |
st.session_state.logged_in = False
|
| 115 |
-
st.session_state.
|
| 116 |
-
st.session_state.
|
| 117 |
st.success("Logged out successfully.")
|
| 118 |
-
# Reset the main view to show the login page
|
| 119 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 120 |
|
| 121 |
-
# Main App
|
| 122 |
-
if st.session_state.
|
| 123 |
-
st.sidebar.title(f"Welcome, {st.session_state.
|
| 124 |
|
| 125 |
if st.sidebar.button("Logout"):
|
| 126 |
-
logout() #
|
| 127 |
|
| 128 |
-
# Display the rest of the app (e.g., task manager)
|
| 129 |
-
st.sidebar.title("Task Filters")
|
| 130 |
-
section = st.sidebar.radio("View Tasks", ["myDay", "thisWeek", "all"])
|
| 131 |
-
|
| 132 |
# Task Input Form
|
| 133 |
st.subheader("Add a New Task")
|
| 134 |
with st.form("add_task_form"):
|
|
@@ -146,15 +70,7 @@ if st.session_state.get('logged_in', False):
|
|
| 146 |
|
| 147 |
# Display Tasks
|
| 148 |
st.subheader("Your Tasks")
|
| 149 |
-
|
| 150 |
-
display_tasks(tasks_to_display)
|
| 151 |
-
|
| 152 |
-
# Procrastination Alerts
|
| 153 |
-
st.subheader("Upcoming Deadlines")
|
| 154 |
-
upcoming_tasks = check_deadlines()
|
| 155 |
-
if upcoming_tasks and st.button("Get Procrastination Message"):
|
| 156 |
-
messages = alert_user(upcoming_tasks)
|
| 157 |
-
st.info(random.choice(messages))
|
| 158 |
|
| 159 |
# Clear All Tasks
|
| 160 |
if st.button("Delete All Tasks"):
|
|
|
|
| 2 |
from datetime import datetime, timedelta
|
| 3 |
import random
|
| 4 |
import logging
|
|
|
|
|
|
|
| 5 |
|
| 6 |
+
# Sample data structure for user_tasks
|
| 7 |
+
if 'user_tasks' not in st.session_state:
|
| 8 |
+
st.session_state.user_tasks = {}
|
| 9 |
|
| 10 |
+
# Initialize user preferences (this can also be loaded from a database or file if needed)
|
| 11 |
+
if 'user_preferences' not in st.session_state:
|
| 12 |
+
st.session_state.user_preferences = {"name": None, "email": None}
|
| 13 |
+
st.session_state.logged_in = False
|
| 14 |
|
| 15 |
+
# Initialize the logger
|
| 16 |
+
logging.basicConfig(filename='app.log', level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
|
| 18 |
# Helper functions
|
| 19 |
def generate_numeric_id():
|
| 20 |
return int(datetime.now().timestamp() * 1000)
|
| 21 |
|
| 22 |
+
# Login form
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
def login():
|
| 24 |
+
st.title("Login")
|
| 25 |
+
username = st.text_input("Enter your name")
|
| 26 |
+
email = st.text_input("Enter your email")
|
| 27 |
+
if st.button("Login"):
|
| 28 |
+
if username and email:
|
| 29 |
+
# Set session state for logged-in user
|
| 30 |
+
st.session_state.user_preferences['name'] = username
|
| 31 |
+
st.session_state.user_preferences['email'] = email
|
| 32 |
+
st.session_state.logged_in = True
|
| 33 |
+
logging.info(f"User logged in: {username}")
|
| 34 |
+
else:
|
| 35 |
+
st.warning("Please enter both name and email.")
|
|
|
|
| 36 |
|
|
|
|
| 37 |
# Logout function
|
| 38 |
def logout():
|
| 39 |
st.session_state.logged_in = False
|
| 40 |
+
st.session_state.user_preferences['name'] = None
|
| 41 |
+
st.session_state.user_preferences['email'] = None
|
| 42 |
st.success("Logged out successfully.")
|
|
|
|
| 43 |
|
| 44 |
+
# Display tasks based on the selected section
|
| 45 |
+
def display_tasks(tasks):
|
| 46 |
+
for task in tasks:
|
| 47 |
+
st.write(f"Task: {task['text']} - Due Date: {task['date']} - Completed: {task['completed']}")
|
| 48 |
|
| 49 |
+
# Main App Logic
|
| 50 |
+
if st.session_state.logged_in:
|
| 51 |
+
st.sidebar.title(f"Welcome, {st.session_state.user_preferences['name']}!")
|
| 52 |
|
| 53 |
if st.sidebar.button("Logout"):
|
| 54 |
+
logout() # Clear session to log out
|
| 55 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
# Task Input Form
|
| 57 |
st.subheader("Add a New Task")
|
| 58 |
with st.form("add_task_form"):
|
|
|
|
| 70 |
|
| 71 |
# Display Tasks
|
| 72 |
st.subheader("Your Tasks")
|
| 73 |
+
display_tasks(st.session_state.user_tasks.values())
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 74 |
|
| 75 |
# Clear All Tasks
|
| 76 |
if st.button("Delete All Tasks"):
|