Update app.py
Browse files
app.py
CHANGED
|
@@ -2,88 +2,134 @@ import gradio as gr
|
|
| 2 |
import threading
|
| 3 |
import time
|
| 4 |
from datetime import datetime
|
| 5 |
-
from report_generator import
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
|
| 7 |
-
|
| 8 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
while True:
|
| 10 |
-
|
| 11 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
|
| 13 |
-
def
|
| 14 |
"""
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
Commands:
|
| 18 |
-
- If the text includes 'csv', generate a CSV report.
|
| 19 |
-
- If the text includes 'xlsx', generate an XLSX report.
|
| 20 |
-
- If the text includes 'pdf', generate a PDF report.
|
| 21 |
-
- Otherwise, echo the user's input with a friendly response.
|
| 22 |
-
|
| 23 |
-
Returns:
|
| 24 |
-
- Updated chat history (list of tuples).
|
| 25 |
-
- A list of generated file paths (if any).
|
| 26 |
"""
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
file_links.append(filename)
|
| 42 |
-
else:
|
| 43 |
-
response = f"You said: {user_input}. How can I help further?"
|
| 44 |
-
|
| 45 |
-
chat_history.append((user_input, response))
|
| 46 |
-
return chat_history, file_links
|
| 47 |
|
| 48 |
-
def
|
| 49 |
"""
|
| 50 |
-
|
| 51 |
-
|
| 52 |
"""
|
| 53 |
-
|
| 54 |
-
# Return updated conversation, the generated file(s), and update the state.
|
| 55 |
-
return chat_history, file_links, chat_history
|
| 56 |
|
| 57 |
def main():
|
| 58 |
-
# Start the background
|
| 59 |
-
|
| 60 |
-
|
| 61 |
|
| 62 |
with gr.Blocks() as demo:
|
| 63 |
-
gr.Markdown("#
|
| 64 |
gr.Markdown(
|
| 65 |
-
"
|
| 66 |
-
"
|
|
|
|
| 67 |
)
|
| 68 |
|
| 69 |
with gr.Row():
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
# This component will display generated files as downloadable links.
|
| 75 |
-
file_output = gr.File(label="Generated Files", file_count="multiple")
|
| 76 |
|
| 77 |
-
#
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 86 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 87 |
demo.launch(server_name="0.0.0.0", server_port=7860)
|
| 88 |
|
| 89 |
if __name__ == "__main__":
|
|
|
|
| 2 |
import threading
|
| 3 |
import time
|
| 4 |
from datetime import datetime
|
| 5 |
+
from report_generator import (
|
| 6 |
+
generate_csv_report,
|
| 7 |
+
generate_xlsx_report,
|
| 8 |
+
generate_pdf_report,
|
| 9 |
+
generate_diagram_report,
|
| 10 |
+
)
|
| 11 |
|
| 12 |
+
# Global containers for pending tasks and generated files.
|
| 13 |
+
task_bucket = []
|
| 14 |
+
generated_files = []
|
| 15 |
+
|
| 16 |
+
# Lock for thread-safe updates.
|
| 17 |
+
bucket_lock = threading.Lock()
|
| 18 |
+
|
| 19 |
+
def background_task_processor():
|
| 20 |
+
"""Background thread that processes tasks from the bucket every few seconds."""
|
| 21 |
while True:
|
| 22 |
+
time.sleep(2) # Check for tasks every 2 seconds.
|
| 23 |
+
with bucket_lock:
|
| 24 |
+
if task_bucket:
|
| 25 |
+
# Pop the first task and simulate processing delay.
|
| 26 |
+
task = task_bucket.pop(0)
|
| 27 |
+
if task:
|
| 28 |
+
print(f"[{datetime.now()}] Processing task: {task['command']}")
|
| 29 |
+
# Simulate timer countdown for processing (optimized delay)
|
| 30 |
+
for remaining in range(5, 0, -1):
|
| 31 |
+
task["timer"] = remaining
|
| 32 |
+
time.sleep(1)
|
| 33 |
+
# Process task based on command keywords.
|
| 34 |
+
cmd = task["command"].lower()
|
| 35 |
+
if "csv" in cmd:
|
| 36 |
+
filename = generate_csv_report()
|
| 37 |
+
task["result"] = "CSV report generated."
|
| 38 |
+
elif "xlsx" in cmd:
|
| 39 |
+
filename = generate_xlsx_report()
|
| 40 |
+
task["result"] = "XLSX report generated."
|
| 41 |
+
elif "pdf" in cmd:
|
| 42 |
+
filename = generate_pdf_report()
|
| 43 |
+
task["result"] = "PDF report generated."
|
| 44 |
+
elif "diagram" in cmd:
|
| 45 |
+
filename = generate_diagram_report()
|
| 46 |
+
task["result"] = "Diagram generated."
|
| 47 |
+
else:
|
| 48 |
+
# For unrecognized commands, simply echo back.
|
| 49 |
+
filename = None
|
| 50 |
+
task["result"] = f"Unrecognized command: {task['command']}"
|
| 51 |
+
if filename:
|
| 52 |
+
task["file"] = filename
|
| 53 |
+
generated_files.append(filename)
|
| 54 |
+
# Update the task status.
|
| 55 |
+
task["completed_at"] = datetime.now().strftime("%H:%M:%S")
|
| 56 |
+
# Signal the change (in a real app you might use websockets or polling)
|
| 57 |
+
print(f"[{datetime.now()}] Task completed: {task}")
|
| 58 |
+
# Clear the local variable so we don’t reprocess the same task.
|
| 59 |
+
task = None
|
| 60 |
|
| 61 |
+
def submit_task(command, current_bucket):
|
| 62 |
"""
|
| 63 |
+
Called when the user clicks Submit.
|
| 64 |
+
Adds the command to the task bucket along with a timestamp.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 65 |
"""
|
| 66 |
+
if command.strip() == "":
|
| 67 |
+
return current_bucket, generated_files, ""
|
| 68 |
+
new_task = {
|
| 69 |
+
"command": command,
|
| 70 |
+
"submitted_at": datetime.now().strftime("%H:%M:%S"),
|
| 71 |
+
"timer": None,
|
| 72 |
+
"result": None,
|
| 73 |
+
"file": None,
|
| 74 |
+
"completed_at": None,
|
| 75 |
+
}
|
| 76 |
+
with bucket_lock:
|
| 77 |
+
task_bucket.append(new_task)
|
| 78 |
+
current_bucket.append(new_task)
|
| 79 |
+
return current_bucket, generated_files, ""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 80 |
|
| 81 |
+
def refresh_status(bucket_state, file_state):
|
| 82 |
"""
|
| 83 |
+
Returns updated task bucket and file list.
|
| 84 |
+
This function can be bound to an interval to update the UI.
|
| 85 |
"""
|
| 86 |
+
return bucket_state, file_state
|
|
|
|
|
|
|
| 87 |
|
| 88 |
def main():
|
| 89 |
+
# Start the background processor thread.
|
| 90 |
+
processor_thread = threading.Thread(target=background_task_processor, daemon=True)
|
| 91 |
+
processor_thread.start()
|
| 92 |
|
| 93 |
with gr.Blocks() as demo:
|
| 94 |
+
gr.Markdown("# Advanced Task & Report Generator")
|
| 95 |
gr.Markdown(
|
| 96 |
+
"Submit your tasks (e.g., `generate csv report`, `generate diagram`, `generate pdf report`) "
|
| 97 |
+
"via the text input below. Tasks are added to the bucket, processed with an optimization timer, "
|
| 98 |
+
"and completed tasks generate downloadable files."
|
| 99 |
)
|
| 100 |
|
| 101 |
with gr.Row():
|
| 102 |
+
with gr.Column(scale=8):
|
| 103 |
+
command_input = gr.Textbox(label="Enter Task Command", placeholder="e.g. generate xlsx report")
|
| 104 |
+
with gr.Column(scale=2):
|
| 105 |
+
submit_btn = gr.Button("Submit")
|
|
|
|
|
|
|
| 106 |
|
| 107 |
+
# Display for task bucket.
|
| 108 |
+
bucket_output = gr.Dataframe(
|
| 109 |
+
headers=["submitted_at", "command", "timer", "result", "completed_at"],
|
| 110 |
+
label="Task Bucket",
|
| 111 |
+
interactive=False,
|
| 112 |
+
)
|
| 113 |
+
|
| 114 |
+
# Timer display (this can show the global last task timer if needed)
|
| 115 |
+
timer_display = gr.Textbox(label="Latest Task Timer", interactive=False)
|
| 116 |
+
|
| 117 |
+
# File output area: show downloadable files.
|
| 118 |
+
file_output = gr.Files(label="Generated Files", file_count="multiple")
|
| 119 |
+
|
| 120 |
+
# Hidden states for tasks and file list.
|
| 121 |
+
bucket_state = gr.State([])
|
| 122 |
+
file_state = gr.State([])
|
| 123 |
|
| 124 |
+
# Define interactions:
|
| 125 |
+
submit_btn.click(
|
| 126 |
+
submit_task,
|
| 127 |
+
inputs=[command_input, bucket_state],
|
| 128 |
+
outputs=[bucket_output, file_output, command_input],
|
| 129 |
+
)
|
| 130 |
+
# Refresh status every few seconds (simulate a timer update).
|
| 131 |
+
demo.load(refresh_status, inputs=[bucket_state, file_state], outputs=[bucket_output, file_output])
|
| 132 |
+
|
| 133 |
demo.launch(server_name="0.0.0.0", server_port=7860)
|
| 134 |
|
| 135 |
if __name__ == "__main__":
|