Update app.py
Browse files
app.py
CHANGED
|
@@ -11,7 +11,6 @@ from report_generator import (
|
|
| 11 |
import cohere
|
| 12 |
|
| 13 |
# Initialize the Cohere client using your provided API key.
|
| 14 |
-
# (For production, load the API key from a secure environment variable.)
|
| 15 |
COHERE_API_KEY = "ffwtfyfLwqSOtw65psZardfqhUxMr1h7u5vzYotI"
|
| 16 |
co = cohere.Client(COHERE_API_KEY)
|
| 17 |
|
|
@@ -31,7 +30,6 @@ def cohere_parse_command(command):
|
|
| 31 |
inputs=[command],
|
| 32 |
examples=cohere_examples
|
| 33 |
)
|
| 34 |
-
# Get the prediction for the first input.
|
| 35 |
prediction = response.classifications[0].prediction
|
| 36 |
return prediction
|
| 37 |
except Exception as e:
|
|
@@ -52,15 +50,12 @@ bucket_lock = threading.Lock()
|
|
| 52 |
def process_single_task(task):
|
| 53 |
"""Process one task with a countdown timer and update its details."""
|
| 54 |
print(f"[{datetime.now()}] Processing task: {task['command']}")
|
| 55 |
-
# Simulate processing delay
|
| 56 |
for remaining in range(5, 0, -1):
|
| 57 |
task["timer"] = remaining
|
| 58 |
time.sleep(1)
|
| 59 |
-
# Use Cohere (or fallback) to classify the intent.
|
| 60 |
intent = cohere_parse_command(task["command"])
|
| 61 |
cmd = task["command"].lower()
|
| 62 |
-
|
| 63 |
-
# Process based on the detected intent.
|
| 64 |
if intent == "report":
|
| 65 |
if "csv" in cmd:
|
| 66 |
filename = generate_csv_report()
|
|
@@ -69,7 +64,6 @@ def process_single_task(task):
|
|
| 69 |
filename = generate_xlsx_report()
|
| 70 |
task["result"] = f"XLSX report generated for '{task['command']}'"
|
| 71 |
else:
|
| 72 |
-
# Default to PDF (with subject included)
|
| 73 |
filename = generate_pdf_report(subject=task["command"])
|
| 74 |
task["result"] = f"PDF report generated for '{task['command']}'"
|
| 75 |
elif intent == "diagram":
|
|
@@ -89,7 +83,7 @@ def process_single_task(task):
|
|
| 89 |
def background_task_processor():
|
| 90 |
"""Continuously monitors the task bucket and spawns a new thread per task."""
|
| 91 |
while True:
|
| 92 |
-
time.sleep(1)
|
| 93 |
task = None
|
| 94 |
with bucket_lock:
|
| 95 |
if task_bucket:
|
|
@@ -99,10 +93,7 @@ def background_task_processor():
|
|
| 99 |
threading.Thread(target=process_single_task, args=(task,), daemon=True).start()
|
| 100 |
|
| 101 |
def submit_task(command, current_tasks):
|
| 102 |
-
"""
|
| 103 |
-
Called when the user clicks Submit.
|
| 104 |
-
Adds a new task (without clearing the textbox) so you can review it.
|
| 105 |
-
"""
|
| 106 |
if command.strip() == "":
|
| 107 |
return current_tasks, generated_files, command
|
| 108 |
new_task = {
|
|
@@ -118,10 +109,10 @@ def submit_task(command, current_tasks):
|
|
| 118 |
with bucket_lock:
|
| 119 |
task_bucket.append(new_task)
|
| 120 |
current_tasks.append(new_task)
|
| 121 |
-
return current_tasks, generated_files, command
|
| 122 |
|
| 123 |
def build_tasks_html(tasks):
|
| 124 |
-
"""
|
| 125 |
html = """
|
| 126 |
<style>
|
| 127 |
table {width: 100%; border-collapse: collapse; margin: 10px 0; font-family: Arial, sans-serif;}
|
|
@@ -166,73 +157,122 @@ def build_tasks_html(tasks):
|
|
| 166 |
return html
|
| 167 |
|
| 168 |
def refresh_ui(tasks, files):
|
| 169 |
-
"""
|
| 170 |
tasks_html = build_tasks_html(tasks)
|
| 171 |
return tasks, files, tasks_html
|
| 172 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 173 |
def main():
|
| 174 |
-
# Start the background task processor.
|
| 175 |
threading.Thread(target=background_task_processor, daemon=True).start()
|
| 176 |
|
| 177 |
-
with gr.Blocks(
|
| 178 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 179 |
gr.Markdown(
|
| 180 |
-
"Enter
|
| 181 |
-
"
|
| 182 |
-
"
|
| 183 |
-
"Multiple tasks run concurrently and you can watch their live progress below."
|
| 184 |
)
|
| 185 |
|
| 186 |
with gr.Row():
|
| 187 |
with gr.Column(scale=8):
|
| 188 |
-
command_input = gr.Textbox(label="
|
| 189 |
with gr.Column(scale=2):
|
| 190 |
-
submit_btn = gr.Button("Submit")
|
| 191 |
|
| 192 |
-
gr.Markdown("
|
| 193 |
with gr.Row():
|
| 194 |
sample1 = gr.Button("Report on US Unemployment 2024")
|
| 195 |
sample2 = gr.Button("Diagram of Sales Data")
|
| 196 |
sample3 = gr.Button("CSV Report of User Activity")
|
| 197 |
|
| 198 |
-
gr.Markdown("
|
| 199 |
tasks_html_output = gr.HTML(label="Tasks Overview")
|
| 200 |
|
| 201 |
-
gr.Markdown("
|
| 202 |
-
file_output = gr.Files(label="
|
| 203 |
|
| 204 |
-
|
| 205 |
-
|
| 206 |
-
|
|
|
|
|
|
|
| 207 |
|
| 208 |
with gr.Row():
|
| 209 |
-
refresh_btn = gr.Button("
|
|
|
|
|
|
|
|
|
|
|
|
|
| 210 |
|
| 211 |
-
# When Submit is clicked, add a task.
|
| 212 |
submit_btn.click(
|
| 213 |
submit_task,
|
| 214 |
inputs=[command_input, tasks_state],
|
| 215 |
outputs=[tasks_state, files_state, command_input]
|
| 216 |
)
|
| 217 |
-
# Sample buttons populate the command textbox.
|
| 218 |
sample1.click(lambda: "generate a report on unemployment in the United States in 2024", None, command_input)
|
| 219 |
sample2.click(lambda: "generate diagram of sales data", None, command_input)
|
| 220 |
sample3.click(lambda: "generate csv report of user activity", None, command_input)
|
| 221 |
-
# Manual refresh button.
|
| 222 |
refresh_btn.click(
|
| 223 |
refresh_ui,
|
| 224 |
inputs=[tasks_state, files_state],
|
| 225 |
outputs=[tasks_state, files_state, tasks_html_output]
|
| 226 |
)
|
| 227 |
-
#
|
| 228 |
auto_refresh_btn = gr.Button("Auto Refresh", visible=False, elem_id="auto_refresh_btn")
|
| 229 |
auto_refresh_btn.click(
|
| 230 |
refresh_ui,
|
| 231 |
inputs=[tasks_state, files_state],
|
| 232 |
outputs=[tasks_state, files_state, tasks_html_output]
|
| 233 |
)
|
| 234 |
-
|
| 235 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 236 |
|
| 237 |
demo.launch(server_name="0.0.0.0", server_port=7860)
|
| 238 |
|
|
|
|
| 11 |
import cohere
|
| 12 |
|
| 13 |
# Initialize the Cohere client using your provided API key.
|
|
|
|
| 14 |
COHERE_API_KEY = "ffwtfyfLwqSOtw65psZardfqhUxMr1h7u5vzYotI"
|
| 15 |
co = cohere.Client(COHERE_API_KEY)
|
| 16 |
|
|
|
|
| 30 |
inputs=[command],
|
| 31 |
examples=cohere_examples
|
| 32 |
)
|
|
|
|
| 33 |
prediction = response.classifications[0].prediction
|
| 34 |
return prediction
|
| 35 |
except Exception as e:
|
|
|
|
| 50 |
def process_single_task(task):
|
| 51 |
"""Process one task with a countdown timer and update its details."""
|
| 52 |
print(f"[{datetime.now()}] Processing task: {task['command']}")
|
| 53 |
+
# Simulate processing delay.
|
| 54 |
for remaining in range(5, 0, -1):
|
| 55 |
task["timer"] = remaining
|
| 56 |
time.sleep(1)
|
|
|
|
| 57 |
intent = cohere_parse_command(task["command"])
|
| 58 |
cmd = task["command"].lower()
|
|
|
|
|
|
|
| 59 |
if intent == "report":
|
| 60 |
if "csv" in cmd:
|
| 61 |
filename = generate_csv_report()
|
|
|
|
| 64 |
filename = generate_xlsx_report()
|
| 65 |
task["result"] = f"XLSX report generated for '{task['command']}'"
|
| 66 |
else:
|
|
|
|
| 67 |
filename = generate_pdf_report(subject=task["command"])
|
| 68 |
task["result"] = f"PDF report generated for '{task['command']}'"
|
| 69 |
elif intent == "diagram":
|
|
|
|
| 83 |
def background_task_processor():
|
| 84 |
"""Continuously monitors the task bucket and spawns a new thread per task."""
|
| 85 |
while True:
|
| 86 |
+
time.sleep(1)
|
| 87 |
task = None
|
| 88 |
with bucket_lock:
|
| 89 |
if task_bucket:
|
|
|
|
| 93 |
threading.Thread(target=process_single_task, args=(task,), daemon=True).start()
|
| 94 |
|
| 95 |
def submit_task(command, current_tasks):
|
| 96 |
+
"""Adds a new task when the user clicks Submit (command remains visible)."""
|
|
|
|
|
|
|
|
|
|
| 97 |
if command.strip() == "":
|
| 98 |
return current_tasks, generated_files, command
|
| 99 |
new_task = {
|
|
|
|
| 109 |
with bucket_lock:
|
| 110 |
task_bucket.append(new_task)
|
| 111 |
current_tasks.append(new_task)
|
| 112 |
+
return current_tasks, generated_files, command
|
| 113 |
|
| 114 |
def build_tasks_html(tasks):
|
| 115 |
+
"""Builds an HTML table showing task details."""
|
| 116 |
html = """
|
| 117 |
<style>
|
| 118 |
table {width: 100%; border-collapse: collapse; margin: 10px 0; font-family: Arial, sans-serif;}
|
|
|
|
| 157 |
return html
|
| 158 |
|
| 159 |
def refresh_ui(tasks, files):
|
| 160 |
+
"""Refreshes the UI by rebuilding the task list HTML."""
|
| 161 |
tasks_html = build_tasks_html(tasks)
|
| 162 |
return tasks, files, tasks_html
|
| 163 |
|
| 164 |
+
def update_dropdown(files):
|
| 165 |
+
"""Updates the dropdown choices for preview from the list of generated files."""
|
| 166 |
+
return files
|
| 167 |
+
|
| 168 |
+
def preview_file(file_path):
|
| 169 |
+
"""Returns HTML for previewing the file based on its type."""
|
| 170 |
+
if not file_path:
|
| 171 |
+
return "No file selected."
|
| 172 |
+
if file_path.endswith(".png"):
|
| 173 |
+
return f'<img src="{file_path}" style="max-width:100%;">'
|
| 174 |
+
elif file_path.endswith(".pdf"):
|
| 175 |
+
return f'<embed src="{file_path}" type="application/pdf" width="100%" height="600px">'
|
| 176 |
+
elif file_path.endswith(".csv") or file_path.endswith(".xlsx"):
|
| 177 |
+
return f'<a href="{file_path}" target="_blank">Download and view file: {file_path}</a>'
|
| 178 |
+
else:
|
| 179 |
+
return f'<a href="{file_path}" target="_blank">Download file: {file_path}</a>'
|
| 180 |
+
|
| 181 |
def main():
|
|
|
|
| 182 |
threading.Thread(target=background_task_processor, daemon=True).start()
|
| 183 |
|
| 184 |
+
with gr.Blocks(css="""
|
| 185 |
+
body {background-color: #121212; font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif; color: #fdfdfd; margin: 0; padding: 0;}
|
| 186 |
+
.gradio-container {max-width: 900px; margin: auto; padding: 20px; background-color: #1e1e1e; border-radius: 8px; box-shadow: 0px 0px 15px rgba(0,0,0,0.5);}
|
| 187 |
+
.title {text-align: center; color: #fdfdfd; margin-bottom: 20px;}
|
| 188 |
+
.section-title {color: #fdfdfd; margin-top: 30px; border-bottom: 2px solid #fdfdfd; padding-bottom: 5px;}
|
| 189 |
+
""") as demo:
|
| 190 |
+
gr.Markdown("<h1 class='title'>Advanced Multi-Task Application</h1>")
|
| 191 |
gr.Markdown(
|
| 192 |
+
"Enter a task command below or click one of the sample commands. For example: <i>generate a report on unemployment in the United States in 2024</i>.<br>"
|
| 193 |
+
"Tasks run concurrently and once complete, files will appear in the <b>Completed Downloads</b> section.<br>"
|
| 194 |
+
"You can also preview a completed file in the <b>File Preview</b> section."
|
|
|
|
| 195 |
)
|
| 196 |
|
| 197 |
with gr.Row():
|
| 198 |
with gr.Column(scale=8):
|
| 199 |
+
command_input = gr.Textbox(label="Task Command", placeholder="Type your command here...", lines=2)
|
| 200 |
with gr.Column(scale=2):
|
| 201 |
+
submit_btn = gr.Button("Submit", variant="primary")
|
| 202 |
|
| 203 |
+
gr.Markdown("<h3 class='section-title'>Sample Commands</h3>")
|
| 204 |
with gr.Row():
|
| 205 |
sample1 = gr.Button("Report on US Unemployment 2024")
|
| 206 |
sample2 = gr.Button("Diagram of Sales Data")
|
| 207 |
sample3 = gr.Button("CSV Report of User Activity")
|
| 208 |
|
| 209 |
+
gr.Markdown("<h3 class='section-title'>Task List</h3>")
|
| 210 |
tasks_html_output = gr.HTML(label="Tasks Overview")
|
| 211 |
|
| 212 |
+
gr.Markdown("<h3 class='section-title'>Completed Downloads</h3>")
|
| 213 |
+
file_output = gr.Files(label="Download Files", file_count="multiple")
|
| 214 |
|
| 215 |
+
gr.Markdown("<h3 class='section-title'>File Preview</h3>")
|
| 216 |
+
with gr.Row():
|
| 217 |
+
file_dropdown = gr.Dropdown(choices=[], label="Select File for Preview")
|
| 218 |
+
preview_btn = gr.Button("Preview File")
|
| 219 |
+
preview_output = gr.HTML(label="File Preview")
|
| 220 |
|
| 221 |
with gr.Row():
|
| 222 |
+
refresh_btn = gr.Button("Manual Refresh")
|
| 223 |
+
auto_refresh_toggle = gr.Checkbox(value=True, label="Auto Refresh Task List")
|
| 224 |
+
|
| 225 |
+
tasks_state = gr.State([])
|
| 226 |
+
files_state = gr.State([])
|
| 227 |
|
|
|
|
| 228 |
submit_btn.click(
|
| 229 |
submit_task,
|
| 230 |
inputs=[command_input, tasks_state],
|
| 231 |
outputs=[tasks_state, files_state, command_input]
|
| 232 |
)
|
|
|
|
| 233 |
sample1.click(lambda: "generate a report on unemployment in the United States in 2024", None, command_input)
|
| 234 |
sample2.click(lambda: "generate diagram of sales data", None, command_input)
|
| 235 |
sample3.click(lambda: "generate csv report of user activity", None, command_input)
|
|
|
|
| 236 |
refresh_btn.click(
|
| 237 |
refresh_ui,
|
| 238 |
inputs=[tasks_state, files_state],
|
| 239 |
outputs=[tasks_state, files_state, tasks_html_output]
|
| 240 |
)
|
| 241 |
+
# Hidden auto-refresh button.
|
| 242 |
auto_refresh_btn = gr.Button("Auto Refresh", visible=False, elem_id="auto_refresh_btn")
|
| 243 |
auto_refresh_btn.click(
|
| 244 |
refresh_ui,
|
| 245 |
inputs=[tasks_state, files_state],
|
| 246 |
outputs=[tasks_state, files_state, tasks_html_output]
|
| 247 |
)
|
| 248 |
+
gr.HTML("""
|
| 249 |
+
<script>
|
| 250 |
+
setInterval(function(){
|
| 251 |
+
if(document.getElementById('auto_refresh_toggle').checked){
|
| 252 |
+
document.getElementById('auto_refresh_btn').click();
|
| 253 |
+
}
|
| 254 |
+
}, 5000);
|
| 255 |
+
</script>
|
| 256 |
+
""")
|
| 257 |
+
auto_refresh_toggle.elem_id = "auto_refresh_toggle"
|
| 258 |
+
|
| 259 |
+
# Update the file preview dropdown when refreshing tasks.
|
| 260 |
+
refresh_btn.click(
|
| 261 |
+
update_dropdown,
|
| 262 |
+
inputs=[files_state],
|
| 263 |
+
outputs=[file_dropdown]
|
| 264 |
+
)
|
| 265 |
+
auto_refresh_btn.click(
|
| 266 |
+
update_dropdown,
|
| 267 |
+
inputs=[files_state],
|
| 268 |
+
outputs=[file_dropdown]
|
| 269 |
+
)
|
| 270 |
+
# Preview button: shows the preview for the selected file.
|
| 271 |
+
preview_btn.click(
|
| 272 |
+
preview_file,
|
| 273 |
+
inputs=[file_dropdown],
|
| 274 |
+
outputs=[preview_output]
|
| 275 |
+
)
|
| 276 |
|
| 277 |
demo.launch(server_name="0.0.0.0", server_port=7860)
|
| 278 |
|