Update app.py
Browse files
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 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
|
| 11 |
def add_task(message, history):
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
|
| 46 |
with gr.Blocks() as demo:
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
|
| 58 |
-
|
|
|
|
|
|
| 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()
|