Taskmate / app.py
JanviMl's picture
Rename taskmateapp.py to app.py
c7a1f5c verified
import os
import gradio as gr
import firebase_admin
from firebase_admin import credentials, firestore
from datetime import datetime
from dotenv import load_dotenv
from pathlib import Path
load_dotenv()
BASE_DIR = Path(__file__).resolve().parent
FIREBASE_KEY = BASE_DIR / 'firebase-key.json'
try:
cred = credentials.Certificate(str(FIREBASE_KEY))
firebase_admin.initialize_app(cred)
db = firestore.client()
except Exception as e:
print(f"Firebase initialization error: {e}")
def add_task(message, history):
try:
if not message:
return "", history
history = history or []
current_time = datetime.now().strftime("%Y-%m-%d %H:%M")
if message.startswith("/task"):
task = message[6:].strip()
if not task:
return "", history + [("", "Task description required")]
db.collection("tasks").add({
"task": task,
"created": current_time,
"status": "pending"
})
response = f"βœ… Task added: {task}\nCreated at: {current_time}"
elif message == "/list":
tasks_ref = db.collection("tasks").stream()
tasks = [task.to_dict() for task in tasks_ref]
if not tasks:
response = "No tasks found."
else:
response = "πŸ“‹ Tasks:\n" + "\n".join([
f"{i+1}. {task['task']} ({task['status']}) - {task['created']}"
for i, task in enumerate(tasks)
])
else:
response = "Commands:\n/task [description] - Add new task\n/list - View all tasks"
history.append((message, response))
return "", history
except Exception as e:
return "", history + [("", f"Error: {str(e)}")]
with gr.Blocks() as demo:
gr.Markdown("# πŸ“ TaskMate")
gr.Markdown("### Task Management Made Simple")
chatbot = gr.Chatbot(height=400)
msg = gr.Textbox(
placeholder="Type /task [description] to add a task, or /list to view tasks",
label="Input"
)
msg.submit(add_task, [msg, chatbot], [msg, chatbot])
if __name__ == "__main__":
demo.launch()