Create app.py
Browse files
    	
        app.py
    ADDED
    
    | @@ -0,0 +1,115 @@ | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | 
|  | |
| 1 | 
            +
            import os
         | 
| 2 | 
            +
            import firebase_admin
         | 
| 3 | 
            +
            from firebase_admin import credentials, db
         | 
| 4 | 
            +
            from transformers import pipeline
         | 
| 5 | 
            +
            import gradio as gr
         | 
| 6 | 
            +
             | 
| 7 | 
            +
            # ----------------------------------
         | 
| 8 | 
            +
            # Initialize Firebase
         | 
| 9 | 
            +
            # ----------------------------------
         | 
| 10 | 
            +
            # Replace with your Firebase credentials
         | 
| 11 | 
            +
            firebase_cred_path = "path/to/your-serviceAccountKey.json"  # Upload to your Hugging Face space
         | 
| 12 | 
            +
            firebase_db_url = "https://your-database-name.firebaseio.com/"  # Replace with your Realtime Database URL
         | 
| 13 | 
            +
             | 
| 14 | 
            +
            cred = credentials.Certificate(firebase_cred_path)
         | 
| 15 | 
            +
            firebase_admin.initialize_app(cred, {"databaseURL": firebase_db_url})
         | 
| 16 | 
            +
             | 
| 17 | 
            +
            # ----------------------------------
         | 
| 18 | 
            +
            # Initialize Hugging Face RAG Model
         | 
| 19 | 
            +
            # ----------------------------------
         | 
| 20 | 
            +
            rag_model = pipeline("question-answering", model="facebook/rag-token-base")
         | 
| 21 | 
            +
             | 
| 22 | 
            +
             | 
| 23 | 
            +
            # ----------------------------------
         | 
| 24 | 
            +
            # Functions for Firebase Operations
         | 
| 25 | 
            +
            # ----------------------------------
         | 
| 26 | 
            +
            def fetch_tasks():
         | 
| 27 | 
            +
                """Fetch tasks from Firebase Realtime Database."""
         | 
| 28 | 
            +
                ref = db.reference("projects")
         | 
| 29 | 
            +
                data = ref.get()
         | 
| 30 | 
            +
                if not data:
         | 
| 31 | 
            +
                    return "No tasks found!"
         | 
| 32 | 
            +
                output = ""
         | 
| 33 | 
            +
                for project_id, project_data in data.items():
         | 
| 34 | 
            +
                    output += f"Project: {project_id}\n"
         | 
| 35 | 
            +
                    for task_id, task_data in project_data.get("tasks", {}).items():
         | 
| 36 | 
            +
                        output += f"- Task: {task_data['title']} (Status: {task_data['status']})\n"
         | 
| 37 | 
            +
                return output
         | 
| 38 | 
            +
             | 
| 39 | 
            +
             | 
| 40 | 
            +
            def add_task(project_id, task_name, description, assignee, deadline):
         | 
| 41 | 
            +
                """Add a new task to Firebase."""
         | 
| 42 | 
            +
                ref = db.reference(f"projects/{project_id}/tasks")
         | 
| 43 | 
            +
                new_task_ref = ref.push()  # Create a new task
         | 
| 44 | 
            +
                new_task_ref.set(
         | 
| 45 | 
            +
                    {
         | 
| 46 | 
            +
                        "title": task_name,
         | 
| 47 | 
            +
                        "description": description,
         | 
| 48 | 
            +
                        "status": "in-progress",
         | 
| 49 | 
            +
                        "assignee": assignee,
         | 
| 50 | 
            +
                        "deadline": deadline,
         | 
| 51 | 
            +
                    }
         | 
| 52 | 
            +
                )
         | 
| 53 | 
            +
                return f"Task '{task_name}' added to project '{project_id}' successfully!"
         | 
| 54 | 
            +
             | 
| 55 | 
            +
             | 
| 56 | 
            +
            def query_rag(question):
         | 
| 57 | 
            +
                """Query RAG model with project data as context."""
         | 
| 58 | 
            +
                ref = db.reference("projects")
         | 
| 59 | 
            +
                data = ref.get()
         | 
| 60 | 
            +
                if not data:
         | 
| 61 | 
            +
                    return "No data available for RAG."
         | 
| 62 | 
            +
             | 
| 63 | 
            +
                # Prepare context from Firebase data
         | 
| 64 | 
            +
                context = ""
         | 
| 65 | 
            +
                for project_id, project_data in data.items():
         | 
| 66 | 
            +
                    for task_id, task_data in project_data.get("tasks", {}).items():
         | 
| 67 | 
            +
                        context += f"{task_data['title']}: {task_data['status']} (Assigned to: {task_data['assignee']}).\n"
         | 
| 68 | 
            +
             | 
| 69 | 
            +
                # Query RAG model
         | 
| 70 | 
            +
                response = rag_model(question=question, context=context)
         | 
| 71 | 
            +
                return response["answer"]
         | 
| 72 | 
            +
             | 
| 73 | 
            +
             | 
| 74 | 
            +
            # ----------------------------------
         | 
| 75 | 
            +
            # Gradio Dashboard
         | 
| 76 | 
            +
            # ----------------------------------
         | 
| 77 | 
            +
            def dashboard():
         | 
| 78 | 
            +
                """Gradio interface for Project Management Dashboard."""
         | 
| 79 | 
            +
                with gr.Blocks() as app:
         | 
| 80 | 
            +
                    gr.Markdown("## Project Management Dashboard with Live Updates")
         | 
| 81 | 
            +
                    
         | 
| 82 | 
            +
                    # Add Task Section
         | 
| 83 | 
            +
                    with gr.Tab("Add Task"):
         | 
| 84 | 
            +
                        project_id = gr.Textbox(label="Project ID")
         | 
| 85 | 
            +
                        task_name = gr.Textbox(label="Task Name")
         | 
| 86 | 
            +
                        description = gr.Textbox(label="Description")
         | 
| 87 | 
            +
                        assignee = gr.Textbox(label="Assignee")
         | 
| 88 | 
            +
                        deadline = gr.Textbox(label="Deadline (YYYY-MM-DD)")
         | 
| 89 | 
            +
                        add_task_button = gr.Button("Add Task")
         | 
| 90 | 
            +
                        add_task_output = gr.Textbox(label="Output", interactive=False)
         | 
| 91 | 
            +
                        add_task_button.click(
         | 
| 92 | 
            +
                            add_task,
         | 
| 93 | 
            +
                            [project_id, task_name, description, assignee, deadline],
         | 
| 94 | 
            +
                            add_task_output,
         | 
| 95 | 
            +
                        )
         | 
| 96 | 
            +
             | 
| 97 | 
            +
                    # Fetch Tasks Section
         | 
| 98 | 
            +
                    with gr.Tab("View Tasks"):
         | 
| 99 | 
            +
                        fetch_button = gr.Button("Fetch Tasks")
         | 
| 100 | 
            +
                        tasks_output = gr.Textbox(label="Tasks", interactive=False)
         | 
| 101 | 
            +
                        fetch_button.click(fetch_tasks, [], tasks_output)
         | 
| 102 | 
            +
             | 
| 103 | 
            +
                    # Query RAG Section
         | 
| 104 | 
            +
                    with gr.Tab("Ask Questions"):
         | 
| 105 | 
            +
                        question = gr.Textbox(label="Ask a question about your projects")
         | 
| 106 | 
            +
                        query_button = gr.Button("Get Answer")
         | 
| 107 | 
            +
                        rag_output = gr.Textbox(label="Answer", interactive=False)
         | 
| 108 | 
            +
                        query_button.click(query_rag, [question], rag_output)
         | 
| 109 | 
            +
             | 
| 110 | 
            +
                app.launch()
         | 
| 111 | 
            +
             | 
| 112 | 
            +
             | 
| 113 | 
            +
            # Launch the app
         | 
| 114 | 
            +
            if __name__ == "__main__":
         | 
| 115 | 
            +
                dashboard()
         |