tayy786 commited on
Commit
61c57c7
Β·
verified Β·
1 Parent(s): 385171c

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +133 -0
app.py ADDED
@@ -0,0 +1,133 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import json
3
+ import os
4
+ from groq import Groq
5
+
6
+ # =========================
7
+ # πŸ” SET YOUR GROQ API KEY
8
+ # =========================
9
+ # In HuggingFace:
10
+ # Settings β†’ Secrets β†’ Add:
11
+ # Name: GROQ_API_KEY
12
+ # Value: your_api_key_here
13
+
14
+ client = Groq(api_key=os.environ.get("Habit"))
15
+
16
+ DATA_FILE = "data.json"
17
+
18
+ # =========================
19
+ # πŸ“ Data Handling
20
+ # =========================
21
+ def load_data():
22
+ if not os.path.exists(DATA_FILE):
23
+ return {"todos": [], "habits": {}}
24
+ with open(DATA_FILE, "r") as f:
25
+ return json.load(f)
26
+
27
+ def save_data(data):
28
+ with open(DATA_FILE, "w") as f:
29
+ json.dump(data, f, indent=4)
30
+
31
+ # =========================
32
+ # πŸ“ Todo Functions
33
+ # =========================
34
+ def add_todo(task):
35
+ data = load_data()
36
+ data["todos"].append({"task": task, "completed": False})
37
+ save_data(data)
38
+ return get_todos()
39
+
40
+ def complete_todo(index):
41
+ data = load_data()
42
+ if 0 <= index < len(data["todos"]):
43
+ data["todos"][index]["completed"] = True
44
+ save_data(data)
45
+ return get_todos()
46
+
47
+ def get_todos():
48
+ data = load_data()
49
+ output = ""
50
+ for i, todo in enumerate(data["todos"]):
51
+ status = "βœ…" if todo["completed"] else "❌"
52
+ output += f"{i}. {todo['task']} {status}\n"
53
+ return output
54
+
55
+ # =========================
56
+ # πŸ”₯ Habit Functions
57
+ # =========================
58
+ def add_habit(habit):
59
+ data = load_data()
60
+ if habit not in data["habits"]:
61
+ data["habits"][habit] = 0
62
+ save_data(data)
63
+ return get_habits()
64
+
65
+ def track_habit(habit):
66
+ data = load_data()
67
+ if habit in data["habits"]:
68
+ data["habits"][habit] += 1
69
+ save_data(data)
70
+ return get_habits()
71
+
72
+ def get_habits():
73
+ data = load_data()
74
+ output = ""
75
+ for habit, streak in data["habits"].items():
76
+ output += f"{habit} πŸ”₯ Streak: {streak} days\n"
77
+ return output
78
+
79
+ # =========================
80
+ # πŸ€– AI Productivity Coach
81
+ # =========================
82
+ def ai_suggestions():
83
+ data = load_data()
84
+ prompt = f"""
85
+ Here are my todos:
86
+ {data['todos']}
87
+
88
+ Here are my habits:
89
+ {data['habits']}
90
+
91
+ Give me productivity advice and motivation.
92
+ """
93
+
94
+ chat = client.chat.completions.create(
95
+ model="llama-3.1-8b-instant",
96
+ messages=[{"role": "user", "content": prompt}],
97
+ temperature=0.7,
98
+ )
99
+
100
+ return chat.choices[0].message.content
101
+
102
+ # =========================
103
+ # 🎨 Gradio UI
104
+ # =========================
105
+ with gr.Blocks() as app:
106
+ gr.Markdown("# 🧠 AI Todo & Habit Tracker")
107
+ gr.Markdown("Track your productivity with AI support πŸš€")
108
+
109
+ with gr.Tab("Todo List"):
110
+ task_input = gr.Textbox(label="New Todo")
111
+ add_btn = gr.Button("Add Todo")
112
+ todo_output = gr.Textbox(label="Your Todos")
113
+ complete_index = gr.Number(label="Todo Index to Complete")
114
+ complete_btn = gr.Button("Complete Todo")
115
+
116
+ add_btn.click(add_todo, inputs=task_input, outputs=todo_output)
117
+ complete_btn.click(complete_todo, inputs=complete_index, outputs=todo_output)
118
+
119
+ with gr.Tab("Habit Tracker"):
120
+ habit_input = gr.Textbox(label="New Habit")
121
+ add_habit_btn = gr.Button("Add Habit")
122
+ habit_output = gr.Textbox(label="Your Habits")
123
+ track_btn = gr.Button("Mark Habit Done")
124
+
125
+ add_habit_btn.click(add_habit, inputs=habit_input, outputs=habit_output)
126
+ track_btn.click(track_habit, inputs=habit_input, outputs=habit_output)
127
+
128
+ with gr.Tab("AI Coach"):
129
+ ai_btn = gr.Button("Get AI Advice")
130
+ ai_output = gr.Textbox(label="AI Suggestion")
131
+ ai_btn.click(ai_suggestions, outputs=ai_output)
132
+
133
+ app.launch()