-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
36 lines (32 loc) · 1.12 KB
/
Copy pathapp.py
File metadata and controls
36 lines (32 loc) · 1.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import json, time
from rich.console import Console
from rich.markdown import Markdown
from rich.panel import Panel
def tail_json(path):
console = Console()
idx = 0
while True:
try:
with open(path, "r", encoding="utf-8") as f:
data = json.load(f)
if len(data) > idx:
for r, c in data[idx:]:
content = (
c.replace("<thought>", "> Thought\n")
.replace("</thought>", "")
.replace("<call>", "```xml\n")
.replace("</call>", "```")
)
console.print(
Panel(
Markdown(content),
title="User" if r == "u" else "Assistant",
border_style="green" if r == "u" else "blue",
)
)
idx = len(data)
except (json.JSONDecodeError, PermissionError, FileNotFoundError):
pass
time.sleep(0.5)
if __name__ == "__main__":
tail_json("chat.json")