Update app.py
Browse files
app.py
CHANGED
|
@@ -1,117 +1,55 @@
|
|
| 1 |
-
from transformers import pipeline
|
| 2 |
-
import firebase_admin
|
| 3 |
-
from firebase_admin import credentials, db
|
| 4 |
import os
|
| 5 |
import json
|
| 6 |
-
|
|
|
|
|
|
|
|
|
|
| 7 |
|
| 8 |
-
#
|
| 9 |
firebase_credential = os.getenv("FIREBASE_CREDENTIALS")
|
|
|
|
|
|
|
| 10 |
|
| 11 |
-
# Save
|
| 12 |
with open("serviceAccountKey.json", "w") as f:
|
| 13 |
f.write(firebase_credential)
|
| 14 |
|
| 15 |
-
# Initialize Firebase
|
| 16 |
cred = credentials.Certificate("serviceAccountKey.json")
|
| 17 |
firebase_admin.initialize_app(cred, {"databaseURL": "https://your-database-name.firebaseio.com/"})
|
| 18 |
|
| 19 |
-
#
|
| 20 |
-
|
| 21 |
-
#
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
#
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
return
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
def
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
"assignee": assignee,
|
| 52 |
-
"deadline": deadline,
|
| 53 |
-
}
|
| 54 |
-
)
|
| 55 |
-
return f"Task '{task_name}' added to project '{project_id}' successfully!"
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
def query_rag(question):
|
| 59 |
-
"""Query RAG model with project data as context."""
|
| 60 |
-
ref = db.reference("projects")
|
| 61 |
-
data = ref.get()
|
| 62 |
-
if not data:
|
| 63 |
-
return "No data available for RAG."
|
| 64 |
-
|
| 65 |
-
# Prepare context from Firebase data
|
| 66 |
-
context = ""
|
| 67 |
-
for project_id, project_data in data.items():
|
| 68 |
-
for task_id, task_data in project_data.get("tasks", {}).items():
|
| 69 |
-
context += f"{task_data['title']}: {task_data['status']} (Assigned to: {task_data['assignee']}).\n"
|
| 70 |
-
|
| 71 |
-
# Query RAG model
|
| 72 |
-
response = rag_model(question=question, context=context)
|
| 73 |
-
return response["answer"]
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
# ----------------------------------
|
| 77 |
-
# Gradio Dashboard
|
| 78 |
-
# ----------------------------------
|
| 79 |
-
def dashboard():
|
| 80 |
-
"""Gradio interface for Project Management Dashboard."""
|
| 81 |
-
with gr.Blocks() as app:
|
| 82 |
-
gr.Markdown("## Project Management Dashboard with Live Updates")
|
| 83 |
-
|
| 84 |
-
# Add Task Section
|
| 85 |
-
with gr.Tab("Add Task"):
|
| 86 |
-
project_id = gr.Textbox(label="Project ID")
|
| 87 |
-
task_name = gr.Textbox(label="Task Name")
|
| 88 |
-
description = gr.Textbox(label="Description")
|
| 89 |
-
assignee = gr.Textbox(label="Assignee")
|
| 90 |
-
deadline = gr.Textbox(label="Deadline (YYYY-MM-DD)")
|
| 91 |
-
add_task_button = gr.Button("Add Task")
|
| 92 |
-
add_task_output = gr.Textbox(label="Output", interactive=False)
|
| 93 |
-
add_task_button.click(
|
| 94 |
-
add_task,
|
| 95 |
-
[project_id, task_name, description, assignee, deadline],
|
| 96 |
-
add_task_output,
|
| 97 |
-
)
|
| 98 |
-
|
| 99 |
-
# Fetch Tasks Section
|
| 100 |
-
with gr.Tab("View Tasks"):
|
| 101 |
-
fetch_button = gr.Button("Fetch Tasks")
|
| 102 |
-
tasks_output = gr.Textbox(label="Tasks", interactive=False)
|
| 103 |
-
fetch_button.click(fetch_tasks, [], tasks_output)
|
| 104 |
-
|
| 105 |
-
# Query RAG Section
|
| 106 |
-
with gr.Tab("Ask Questions"):
|
| 107 |
-
question = gr.Textbox(label="Ask a question about your projects")
|
| 108 |
-
query_button = gr.Button("Get Answer")
|
| 109 |
-
rag_output = gr.Textbox(label="Answer", interactive=False)
|
| 110 |
-
query_button.click(query_rag, [question], rag_output)
|
| 111 |
-
|
| 112 |
-
app.launch()
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
# Launch the app
|
| 116 |
if __name__ == "__main__":
|
| 117 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import os
|
| 2 |
import json
|
| 3 |
+
import firebase_admin
|
| 4 |
+
from firebase_admin import credentials, db
|
| 5 |
+
from transformers import RagTokenizer, RagRetriever, RagSequenceForGeneration
|
| 6 |
+
import gradio as gr
|
| 7 |
|
| 8 |
+
# Initialize Firebase Admin SDK
|
| 9 |
firebase_credential = os.getenv("FIREBASE_CREDENTIALS")
|
| 10 |
+
if not firebase_credential:
|
| 11 |
+
raise RuntimeError("FIREBASE_CREDENTIALS environment variable is not set.")
|
| 12 |
|
| 13 |
+
# Save Firebase credentials to a temporary file
|
| 14 |
with open("serviceAccountKey.json", "w") as f:
|
| 15 |
f.write(firebase_credential)
|
| 16 |
|
| 17 |
+
# Initialize Firebase App
|
| 18 |
cred = credentials.Certificate("serviceAccountKey.json")
|
| 19 |
firebase_admin.initialize_app(cred, {"databaseURL": "https://your-database-name.firebaseio.com/"})
|
| 20 |
|
| 21 |
+
# Load the RAG model, tokenizer, and retriever
|
| 22 |
+
tokenizer = RagTokenizer.from_pretrained("facebook/rag-token-base")
|
| 23 |
+
retriever = RagRetriever.from_pretrained("facebook/rag-token-base", use_dummy_dataset=True) # Use a dummy dataset for now
|
| 24 |
+
model = RagSequenceForGeneration.from_pretrained("facebook/rag-token-base")
|
| 25 |
+
|
| 26 |
+
# Function to generate answers using the RAG model
|
| 27 |
+
def generate_answer(question, context=""):
|
| 28 |
+
# Tokenize the question and context
|
| 29 |
+
inputs = tokenizer(question, return_tensors="pt")
|
| 30 |
+
|
| 31 |
+
# Retrieve relevant documents (dummy dataset for this example)
|
| 32 |
+
# In a real-world case, you would provide a proper knowledge base or corpus
|
| 33 |
+
retrieved_docs = retriever(question=question, input_ids=inputs["input_ids"])
|
| 34 |
+
|
| 35 |
+
# Generate the answer using the RAG model
|
| 36 |
+
outputs = model.generate(input_ids=inputs["input_ids"],
|
| 37 |
+
context_input_ids=retrieved_docs["context_input_ids"])
|
| 38 |
+
|
| 39 |
+
# Decode the generated answer
|
| 40 |
+
answer = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 41 |
+
return answer
|
| 42 |
+
|
| 43 |
+
# Gradio interface function
|
| 44 |
+
def dashboard(question):
|
| 45 |
+
# Generate the answer from the RAG model
|
| 46 |
+
answer = generate_answer(question)
|
| 47 |
+
return answer
|
| 48 |
+
|
| 49 |
+
# Gradio Interface Setup
|
| 50 |
+
interface = gr.Interface(fn=dashboard, inputs="text", outputs="text")
|
| 51 |
+
|
| 52 |
+
# Launch the Gradio app
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
if __name__ == "__main__":
|
| 54 |
+
interface.launch()
|
| 55 |
+
|