JanviMl commited on
Commit
c9e7c73
Β·
verified Β·
1 Parent(s): f6e9dad

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +64 -48
app.py CHANGED
@@ -1,58 +1,74 @@
 
1
  import gradio as gr
2
  import firebase_admin
3
  from firebase_admin import credentials, firestore
4
  from datetime import datetime
 
 
5
 
6
- # Initialize Firebase
7
- cred = credentials.Certificate("firebase-key.json")
8
- firebase_admin.initialize_app(cred)
9
- db = firestore.client()
 
 
 
 
 
 
 
10
 
11
  def add_task(message, history):
12
- if not message:
13
- return "", history
14
-
15
- history = history or []
16
- current_time = datetime.now().strftime("%Y-%m-%d %H:%M")
17
-
18
- if message.startswith("/task"):
19
- task = message[6:].strip()
20
- # Add to Firebase
21
- db.collection("tasks").add({
22
- "task": task,
23
- "created": current_time,
24
- "status": "pending"
25
- })
26
- response = f"βœ… Task added: {task}\nCreated at: {current_time}"
27
-
28
- elif message == "/list":
29
- # Get from Firebase
30
- tasks_ref = db.collection("tasks").stream()
31
- tasks = [task.to_dict() for task in tasks_ref]
32
-
33
- if not tasks:
34
- response = "No tasks found."
35
- else:
36
- response = "πŸ“‹ Tasks:\n" + "\n".join([
37
- f"{i+1}. {task['task']} ({task['status']}) - {task['created']}"
38
- for i, task in enumerate(tasks)
39
- ])
40
- else:
41
- response = "Commands:\n/task [description] - Add new task\n/list - View all tasks"
42
-
43
- history.append((message, response))
44
- return "", history
 
 
 
 
 
45
 
46
  with gr.Blocks() as demo:
47
- gr.Markdown("# πŸ“ TaskMate")
48
- gr.Markdown("### Task Management Made Simple")
49
-
50
- chatbot = gr.Chatbot(height=400)
51
- msg = gr.Textbox(
52
- placeholder="Type /task [description] to add a task, or /list to view tasks",
53
- label="Input"
54
- )
55
-
56
- msg.submit(add_task, [msg, chatbot], [msg, chatbot])
57
 
58
- demo.launch()
 
 
1
+ import os
2
  import gradio as gr
3
  import firebase_admin
4
  from firebase_admin import credentials, firestore
5
  from datetime import datetime
6
+ from dotenv import load_dotenv
7
+ from pathlib import Path
8
 
9
+ load_dotenv()
10
+
11
+ BASE_DIR = Path(__file__).resolve().parent
12
+ FIREBASE_KEY = BASE_DIR / 'firebase-key.json'
13
+
14
+ try:
15
+ cred = credentials.Certificate(str(FIREBASE_KEY))
16
+ firebase_admin.initialize_app(cred)
17
+ db = firestore.client()
18
+ except Exception as e:
19
+ print(f"Firebase initialization error: {e}")
20
 
21
  def add_task(message, history):
22
+ try:
23
+ if not message:
24
+ return "", history
25
+
26
+ history = history or []
27
+ current_time = datetime.now().strftime("%Y-%m-%d %H:%M")
28
+
29
+ if message.startswith("/task"):
30
+ task = message[6:].strip()
31
+ if not task:
32
+ return "", history + [("", "Task description required")]
33
+
34
+ db.collection("tasks").add({
35
+ "task": task,
36
+ "created": current_time,
37
+ "status": "pending"
38
+ })
39
+ response = f"βœ… Task added: {task}\nCreated at: {current_time}"
40
+
41
+ elif message == "/list":
42
+ tasks_ref = db.collection("tasks").stream()
43
+ tasks = [task.to_dict() for task in tasks_ref]
44
+
45
+ if not tasks:
46
+ response = "No tasks found."
47
+ else:
48
+ response = "πŸ“‹ Tasks:\n" + "\n".join([
49
+ f"{i+1}. {task['task']} ({task['status']}) - {task['created']}"
50
+ for i, task in enumerate(tasks)
51
+ ])
52
+ else:
53
+ response = "Commands:\n/task [description] - Add new task\n/list - View all tasks"
54
+
55
+ history.append((message, response))
56
+ return "", history
57
+
58
+ except Exception as e:
59
+ return "", history + [("", f"Error: {str(e)}")]
60
 
61
  with gr.Blocks() as demo:
62
+ gr.Markdown("# πŸ“ TaskMate")
63
+ gr.Markdown("### Task Management Made Simple")
64
+
65
+ chatbot = gr.Chatbot(height=400)
66
+ msg = gr.Textbox(
67
+ placeholder="Type /task [description] to add a task, or /list to view tasks",
68
+ label="Input"
69
+ )
70
+
71
+ msg.submit(add_task, [msg, chatbot], [msg, chatbot])
72
 
73
+ if __name__ == "__main__":
74
+ demo.launch()