Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,102 +1,33 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
import plotly.express as px
|
| 5 |
-
from datetime import datetime, timedelta
|
| 6 |
-
import json
|
| 7 |
-
import os
|
| 8 |
|
| 9 |
-
|
| 10 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
-
|
| 13 |
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
data = json.load(f)
|
| 18 |
-
else:
|
| 19 |
-
data = {"tasks": [], "habits": {}}
|
| 20 |
-
|
| 21 |
-
# Sidebar: Add Task
|
| 22 |
-
st.sidebar.header("β Add Task")
|
| 23 |
-
task_name = st.sidebar.text_input("Task Name")
|
| 24 |
-
task_completed = st.sidebar.checkbox("Completed")
|
| 25 |
-
if st.sidebar.button("Add Task") and task_name.strip():
|
| 26 |
-
today = datetime.today().strftime("%Y-%m-%d")
|
| 27 |
-
data["tasks"].append({
|
| 28 |
-
"Task": task_name,
|
| 29 |
-
"Completed": task_completed,
|
| 30 |
-
"Date": today
|
| 31 |
-
})
|
| 32 |
-
with open(DATA_FILE, "w") as f:
|
| 33 |
-
json.dump(data, f)
|
| 34 |
-
st.sidebar.success("Task added!")
|
| 35 |
-
|
| 36 |
-
# Sidebar: Track Habit
|
| 37 |
-
st.sidebar.header("π₯ Track Habit")
|
| 38 |
-
habit_name = st.sidebar.text_input("Habit Name")
|
| 39 |
-
if st.sidebar.button("Mark Habit Done") and habit_name.strip():
|
| 40 |
-
today = datetime.today().strftime("%Y-%m-%d")
|
| 41 |
-
if habit_name not in data["habits"]:
|
| 42 |
-
data["habits"][habit_name] = []
|
| 43 |
-
if today not in data["habits"][habit_name]:
|
| 44 |
-
data["habits"][habit_name].append(today)
|
| 45 |
-
with open(DATA_FILE, "w") as f:
|
| 46 |
-
json.dump(data, f)
|
| 47 |
-
st.sidebar.success("Habit marked for today!")
|
| 48 |
-
else:
|
| 49 |
-
st.sidebar.warning("Already marked today.")
|
| 50 |
-
|
| 51 |
-
# Display Tasks
|
| 52 |
-
st.subheader("π Tasks")
|
| 53 |
-
if data["tasks"]:
|
| 54 |
-
task_df = pd.DataFrame(data["tasks"])
|
| 55 |
-
st.dataframe(task_df, use_container_width=True)
|
| 56 |
-
else:
|
| 57 |
-
st.info("No tasks yet.")
|
| 58 |
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
habit_data = []
|
| 62 |
-
for habit, dates in data["habits"].items():
|
| 63 |
-
dates_sorted = sorted(dates)
|
| 64 |
-
streak = 0
|
| 65 |
-
today = datetime.today().date()
|
| 66 |
-
for i in range(21):
|
| 67 |
-
day = today - timedelta(days=i)
|
| 68 |
-
if day.strftime("%Y-%m-%d") in dates_sorted:
|
| 69 |
-
streak += 1
|
| 70 |
-
else:
|
| 71 |
-
break
|
| 72 |
-
habit_type = "π Permanent Habit" if streak >= 21 else "β³ Temporary Habit"
|
| 73 |
-
habit_data.append({"Habit": habit, "Days Followed": len(dates), "Streak": streak, "Type": habit_type})
|
| 74 |
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
st.
|
| 78 |
-
st.subheader("π Habit Analytics")
|
| 79 |
-
st.plotly_chart(px.bar(habit_df, x="Habit", y="Days Followed", color="Type"), use_container_width=True)
|
| 80 |
-
st.plotly_chart(px.pie(habit_df, names="Habit", values="Days Followed"), use_container_width=True)
|
| 81 |
else:
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
"Completed Tasks Today": len([t for t in today_tasks if t["Completed"]]),
|
| 92 |
-
"Total Habits": len(data["habits"])
|
| 93 |
-
}])
|
| 94 |
-
st.table(report_df)
|
| 95 |
-
|
| 96 |
-
# Download CSV
|
| 97 |
-
csv_data = report_df.to_csv(index=False).encode("utf-8")
|
| 98 |
-
st.download_button("β¬ Download CSV", csv_data, "daily_report.csv", "text/csv")
|
| 99 |
-
|
| 100 |
-
# Run the app
|
| 101 |
-
if __name__ == "__main__":
|
| 102 |
-
main()
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import os
|
| 3 |
+
from groq import Groq
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
|
| 5 |
+
# --- Setup Groq client using environment variable ---
|
| 6 |
+
api_key = os.environ.get("GROQ_API_KEY")
|
| 7 |
+
if not api_key:
|
| 8 |
+
st.error("Error: GROQ_API_KEY not set in environment variables.")
|
| 9 |
+
else:
|
| 10 |
+
client = Groq(api_key=api_key)
|
| 11 |
|
| 12 |
+
st.title("Habit & To-Do Tracker with Groq AI")
|
| 13 |
|
| 14 |
+
st.markdown("""
|
| 15 |
+
Welcome! Enter a prompt and get insights from the Groq API.
|
| 16 |
+
""")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
|
| 18 |
+
# --- User input ---
|
| 19 |
+
user_prompt = st.text_area("Enter your prompt:", "")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
|
| 21 |
+
if st.button("Submit"):
|
| 22 |
+
if not user_prompt.strip():
|
| 23 |
+
st.warning("Please enter a prompt!")
|
|
|
|
|
|
|
|
|
|
| 24 |
else:
|
| 25 |
+
try:
|
| 26 |
+
with st.spinner("Processing..."):
|
| 27 |
+
response = client.chat.completions.create(
|
| 28 |
+
messages=[{"role": "user", "content": user_prompt}]
|
| 29 |
+
)
|
| 30 |
+
st.subheader("Groq AI Response:")
|
| 31 |
+
st.write(response.choices[0].message.content)
|
| 32 |
+
except Exception as e:
|
| 33 |
+
st.error(f"Error calling Groq API: {e}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|