Ahmad-01 commited on
Commit
98f352e
Β·
verified Β·
1 Parent(s): 6fa50e9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +27 -96
app.py CHANGED
@@ -1,102 +1,33 @@
1
- def main():
2
- import streamlit as st
3
- import pandas as pd
4
- import plotly.express as px
5
- from datetime import datetime, timedelta
6
- import json
7
- import os
8
 
9
- st.set_page_config(page_title="Habit & To-Do Tracker", layout="wide")
10
- st.title("πŸ“‹ Habit & To-Do Tracker")
 
 
 
 
11
 
12
- DATA_FILE = "data.json"
13
 
14
- # Load data
15
- if os.path.exists(DATA_FILE):
16
- with open(DATA_FILE, "r") as f:
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
- # Display Habits
60
- st.subheader("πŸ”₯ Habits")
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
- if habit_data:
76
- habit_df = pd.DataFrame(habit_data)
77
- st.dataframe(habit_df, use_container_width=True)
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
- st.info("No habits tracked yet.")
83
-
84
- # Daily Report
85
- st.subheader("πŸ“‘ Daily Report")
86
- today_str = datetime.today().strftime("%Y-%m-%d")
87
- today_tasks = [t for t in data["tasks"] if t["Date"] == today_str]
88
- report_df = pd.DataFrame([{
89
- "Date": today_str,
90
- "Total Tasks Today": len(today_tasks),
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}")