File size: 4,092 Bytes
dbad9b8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
import os
import firebase_admin
from firebase_admin import credentials, db
from transformers import pipeline
import gradio as gr

# ----------------------------------
# Initialize Firebase
# ----------------------------------
# Replace with your Firebase credentials
firebase_cred_path = "path/to/your-serviceAccountKey.json"  # Upload to your Hugging Face space
firebase_db_url = "https://your-database-name.firebaseio.com/"  # Replace with your Realtime Database URL

cred = credentials.Certificate(firebase_cred_path)
firebase_admin.initialize_app(cred, {"databaseURL": firebase_db_url})

# ----------------------------------
# Initialize Hugging Face RAG Model
# ----------------------------------
rag_model = pipeline("question-answering", model="facebook/rag-token-base")


# ----------------------------------
# Functions for Firebase Operations
# ----------------------------------
def fetch_tasks():
    """Fetch tasks from Firebase Realtime Database."""
    ref = db.reference("projects")
    data = ref.get()
    if not data:
        return "No tasks found!"
    output = ""
    for project_id, project_data in data.items():
        output += f"Project: {project_id}\n"
        for task_id, task_data in project_data.get("tasks", {}).items():
            output += f"- Task: {task_data['title']} (Status: {task_data['status']})\n"
    return output


def add_task(project_id, task_name, description, assignee, deadline):
    """Add a new task to Firebase."""
    ref = db.reference(f"projects/{project_id}/tasks")
    new_task_ref = ref.push()  # Create a new task
    new_task_ref.set(
        {
            "title": task_name,
            "description": description,
            "status": "in-progress",
            "assignee": assignee,
            "deadline": deadline,
        }
    )
    return f"Task '{task_name}' added to project '{project_id}' successfully!"


def query_rag(question):
    """Query RAG model with project data as context."""
    ref = db.reference("projects")
    data = ref.get()
    if not data:
        return "No data available for RAG."

    # Prepare context from Firebase data
    context = ""
    for project_id, project_data in data.items():
        for task_id, task_data in project_data.get("tasks", {}).items():
            context += f"{task_data['title']}: {task_data['status']} (Assigned to: {task_data['assignee']}).\n"

    # Query RAG model
    response = rag_model(question=question, context=context)
    return response["answer"]


# ----------------------------------
# Gradio Dashboard
# ----------------------------------
def dashboard():
    """Gradio interface for Project Management Dashboard."""
    with gr.Blocks() as app:
        gr.Markdown("## Project Management Dashboard with Live Updates")
        
        # Add Task Section
        with gr.Tab("Add Task"):
            project_id = gr.Textbox(label="Project ID")
            task_name = gr.Textbox(label="Task Name")
            description = gr.Textbox(label="Description")
            assignee = gr.Textbox(label="Assignee")
            deadline = gr.Textbox(label="Deadline (YYYY-MM-DD)")
            add_task_button = gr.Button("Add Task")
            add_task_output = gr.Textbox(label="Output", interactive=False)
            add_task_button.click(
                add_task,
                [project_id, task_name, description, assignee, deadline],
                add_task_output,
            )

        # Fetch Tasks Section
        with gr.Tab("View Tasks"):
            fetch_button = gr.Button("Fetch Tasks")
            tasks_output = gr.Textbox(label="Tasks", interactive=False)
            fetch_button.click(fetch_tasks, [], tasks_output)

        # Query RAG Section
        with gr.Tab("Ask Questions"):
            question = gr.Textbox(label="Ask a question about your projects")
            query_button = gr.Button("Get Answer")
            rag_output = gr.Textbox(label="Answer", interactive=False)
            query_button.click(query_rag, [question], rag_output)

    app.launch()


# Launch the app
if __name__ == "__main__":
    dashboard()