Refactored some Gradio UI helper functions out of rss_client.py
Browse files- client/gradio_functions.py +36 -0
- rss_client.py +9 -16
client/gradio_functions.py
ADDED
@@ -0,0 +1,36 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
'''Collections of helper functions for Gradio user interface.'''
|
2 |
+
|
3 |
+
import os
|
4 |
+
import re
|
5 |
+
|
6 |
+
def update_log(n: int = 10):
|
7 |
+
'''Gets updated logging output from disk to display to user.
|
8 |
+
|
9 |
+
Args:
|
10 |
+
n: number of most recent lines of logging output to display
|
11 |
+
|
12 |
+
Returns:
|
13 |
+
Logging output as string
|
14 |
+
'''
|
15 |
+
|
16 |
+
with open('logs/rss_client.log', 'r', encoding='utf-8') as log_file:
|
17 |
+
lines = log_file.readlines()
|
18 |
+
|
19 |
+
return ''.join(lines[-n:])
|
20 |
+
|
21 |
+
|
22 |
+
def delete_old_logs(directory:str, basename:str) -> None:
|
23 |
+
'''Deletes old log files from previous optimization sessions, if present.
|
24 |
+
|
25 |
+
Args:
|
26 |
+
directory: path to log file directory as string
|
27 |
+
basename: log file base name as string
|
28 |
+
|
29 |
+
Returns:
|
30 |
+
None
|
31 |
+
'''
|
32 |
+
|
33 |
+
for filename in os.listdir(directory):
|
34 |
+
file_path = os.path.join(directory, filename)
|
35 |
+
if re.search(basename, filename):
|
36 |
+
os.remove(file_path)
|
rss_client.py
CHANGED
@@ -6,6 +6,7 @@ from pathlib import Path
|
|
6 |
from logging.handlers import RotatingFileHandler
|
7 |
|
8 |
import gradio as gr
|
|
|
9 |
import client.interface as interface
|
10 |
from client.mcp_client import MCPClientWrapper
|
11 |
from client.anthropic_bridge import AnthropicBridge
|
@@ -13,6 +14,9 @@ from client.anthropic_bridge import AnthropicBridge
|
|
13 |
# Make sure log directory exists
|
14 |
Path('logs').mkdir(parents=True, exist_ok=True)
|
15 |
|
|
|
|
|
|
|
16 |
# Set-up logger
|
17 |
logger = logging.getLogger()
|
18 |
|
@@ -60,21 +64,6 @@ async def send_message(message: str, chat_history: list) -> str:
|
|
60 |
|
61 |
return '', chat_history
|
62 |
|
63 |
-
def update_log(n: int = 10):
|
64 |
-
'''Gets updated logging output from disk to display to user.
|
65 |
-
|
66 |
-
Args:
|
67 |
-
n: number of most recent lines of logging output to display
|
68 |
-
|
69 |
-
Returns:
|
70 |
-
Logging output as string
|
71 |
-
'''
|
72 |
-
|
73 |
-
with open('logs/rss_client.log', 'r', encoding='utf-8') as log_file:
|
74 |
-
lines = log_file.readlines()
|
75 |
-
|
76 |
-
return ''.join(lines[-n:])
|
77 |
-
|
78 |
|
79 |
with gr.Blocks(title='MCP RSS client') as demo:
|
80 |
gr.Markdown('# Agentic RSS reader')
|
@@ -94,7 +83,11 @@ with gr.Blocks(title='MCP RSS client') as demo:
|
|
94 |
# Log output
|
95 |
logs = gr.Textbox(label='Client logs', lines=10, max_lines=10)
|
96 |
timer = gr.Timer(1, active=True)
|
97 |
-
|
|
|
|
|
|
|
|
|
98 |
|
99 |
# Chat interface
|
100 |
chatbot = gr.Chatbot(
|
|
|
6 |
from logging.handlers import RotatingFileHandler
|
7 |
|
8 |
import gradio as gr
|
9 |
+
import client.gradio_functions as gradio_funcs
|
10 |
import client.interface as interface
|
11 |
from client.mcp_client import MCPClientWrapper
|
12 |
from client.anthropic_bridge import AnthropicBridge
|
|
|
14 |
# Make sure log directory exists
|
15 |
Path('logs').mkdir(parents=True, exist_ok=True)
|
16 |
|
17 |
+
# Clear old logs if present
|
18 |
+
gradio_funcs.delete_old_logs('logs', 'rss_client')
|
19 |
+
|
20 |
# Set-up logger
|
21 |
logger = logging.getLogger()
|
22 |
|
|
|
64 |
|
65 |
return '', chat_history
|
66 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
67 |
|
68 |
with gr.Blocks(title='MCP RSS client') as demo:
|
69 |
gr.Markdown('# Agentic RSS reader')
|
|
|
83 |
# Log output
|
84 |
logs = gr.Textbox(label='Client logs', lines=10, max_lines=10)
|
85 |
timer = gr.Timer(1, active=True)
|
86 |
+
|
87 |
+
timer.tick( # pylint: disable=no-member
|
88 |
+
lambda: gradio_funcs.update_log(), # pylint: disable=unnecessary-lambda
|
89 |
+
outputs=logs
|
90 |
+
)
|
91 |
|
92 |
# Chat interface
|
93 |
chatbot = gr.Chatbot(
|