Spaces:
Sleeping
Sleeping
Commit ·
ab4dc28
1
Parent(s): aad1846
Build the load event handler with a background thread
Browse files
app.py
CHANGED
|
@@ -21,9 +21,18 @@ class QueueHandler(logging.Handler):
|
|
| 21 |
def emit(self, record):
|
| 22 |
self.log_queue.put(self.format(record))
|
| 23 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
def setup_logging(log_queue):
|
| 25 |
"""
|
| 26 |
-
|
| 27 |
"""
|
| 28 |
handler = QueueHandler(log_queue)
|
| 29 |
formatter = logging.Formatter(
|
|
@@ -51,7 +60,49 @@ class App:
|
|
| 51 |
with gr.Blocks(title="WorthBrain", fill_width=True) as ui:
|
| 52 |
log_data = gr.State([])
|
| 53 |
|
| 54 |
-
def
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 55 |
"""
|
| 56 |
Load event handler that starts a background worker thread, streaming log updates
|
| 57 |
to the UI and yields incremental UI updates.
|
|
@@ -67,13 +118,21 @@ class App:
|
|
| 67 |
|
| 68 |
def worker():
|
| 69 |
"""
|
| 70 |
-
|
|
|
|
|
|
|
| 71 |
"""
|
| 72 |
-
|
|
|
|
| 73 |
|
| 74 |
-
thread = threading.Thread(target=worker)
|
| 75 |
thread.start()
|
| 76 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 77 |
with gr.Row():
|
| 78 |
gr.Markdown(
|
| 79 |
'<div style="text-align: center;font-size:24px"><strong>WorthBrain</strong> - Autonomous Agent Framework that hunts for deals</div>'
|
|
|
|
| 21 |
def emit(self, record):
|
| 22 |
self.log_queue.put(self.format(record))
|
| 23 |
|
| 24 |
+
|
| 25 |
+
def html_for(log_data):
|
| 26 |
+
output = "<br>".join(log_data[-18:])
|
| 27 |
+
return f"""
|
| 28 |
+
<div id="scrollContent" style="height: 400px; overflow-y: auto; border: 1px solid #ccc; background-color: #222229; padding: 10px;">
|
| 29 |
+
{output}
|
| 30 |
+
</div>
|
| 31 |
+
"""
|
| 32 |
+
|
| 33 |
def setup_logging(log_queue):
|
| 34 |
"""
|
| 35 |
+
Register and configure for logging
|
| 36 |
"""
|
| 37 |
handler = QueueHandler(log_queue)
|
| 38 |
formatter = logging.Formatter(
|
|
|
|
| 60 |
with gr.Blocks(title="WorthBrain", fill_width=True) as ui:
|
| 61 |
log_data = gr.State([])
|
| 62 |
|
| 63 |
+
def table_for(opps):
|
| 64 |
+
"""
|
| 65 |
+
Format the initial/final_result
|
| 66 |
+
"""
|
| 67 |
+
return [
|
| 68 |
+
[
|
| 69 |
+
opp.deal.product_description,
|
| 70 |
+
f"${opp.deal.price:.2f}",
|
| 71 |
+
f"${opp.estimate:.2f}",
|
| 72 |
+
f"${opp.discount:.2f}",
|
| 73 |
+
opp.deal.url,
|
| 74 |
+
]
|
| 75 |
+
for opp in opps
|
| 76 |
+
]
|
| 77 |
+
|
| 78 |
+
def stream_ui_updates(log_data, log_queue, result_queue):
|
| 79 |
+
"""
|
| 80 |
+
A generator loop that streams log messages and opportunities data for the data frame in the UI
|
| 81 |
+
"""
|
| 82 |
+
|
| 83 |
+
initial_result = table_for(self.get_agent_framework().memory)
|
| 84 |
+
final_result = None
|
| 85 |
+
|
| 86 |
+
while True:
|
| 87 |
+
try:
|
| 88 |
+
message = log_queue.get_nowait()
|
| 89 |
+
log_data.append(reformat(message))
|
| 90 |
+
yield log_data, html_for(log_data), final_result or initial_result
|
| 91 |
+
except queue.Empty:
|
| 92 |
+
try:
|
| 93 |
+
final_result = result_queue.get_nowait()
|
| 94 |
+
yield log_data, html_for(log_data), final_result or initial_result
|
| 95 |
+
except queue.Empty:
|
| 96 |
+
if final_result is not None:
|
| 97 |
+
break
|
| 98 |
+
time.sleep(0.1)
|
| 99 |
+
|
| 100 |
+
def do_run():
|
| 101 |
+
new_opportunities = self.get_agent_framework().run()
|
| 102 |
+
table = table_for(new_opportunities)
|
| 103 |
+
return table
|
| 104 |
+
|
| 105 |
+
def run_with_logging(initial_log_data):
|
| 106 |
"""
|
| 107 |
Load event handler that starts a background worker thread, streaming log updates
|
| 108 |
to the UI and yields incremental UI updates.
|
|
|
|
| 118 |
|
| 119 |
def worker():
|
| 120 |
"""
|
| 121 |
+
Background task that executes the agent framework,
|
| 122 |
+
sends final results to result_queue, and emits logs
|
| 123 |
+
through the logging system.
|
| 124 |
"""
|
| 125 |
+
result = do_run()
|
| 126 |
+
result_queue.put(result)
|
| 127 |
|
| 128 |
+
thread = threading.Thread(target=worker, daemon=True)
|
| 129 |
thread.start()
|
| 130 |
|
| 131 |
+
for log_data, html_output, final_result in stream_ui_updates(
|
| 132 |
+
initial_log_data, log_queue, result_queue
|
| 133 |
+
):
|
| 134 |
+
yield log_data, html_output, final_result
|
| 135 |
+
|
| 136 |
with gr.Row():
|
| 137 |
gr.Markdown(
|
| 138 |
'<div style="text-align: center;font-size:24px"><strong>WorthBrain</strong> - Autonomous Agent Framework that hunts for deals</div>'
|