Skip to content

Commit 6c2456b

Browse files
GregoryComermalfet
authored andcommitted
Update browser logic to match updated chat format (#478)
1 parent c5eb5b0 commit 6c2456b

File tree

1 file changed

+48
-9
lines changed

1 file changed

+48
-9
lines changed

chat_in_browser.py

Lines changed: 48 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,37 @@ def create_app(*args):
2020
["python3", "generate.py", *args], stdin=subprocess.PIPE, stdout=subprocess.PIPE
2121
)
2222

23+
2324
@app.route("/")
2425
def main():
26+
print("Starting chat session.")
27+
line = b""
2528
output = ""
2629
global disable_input
2730

2831
while True:
29-
line = proc.stdout.readline()
30-
if line.decode("utf-8").startswith("What is your prompt?"):
32+
buffer = proc.stdout.read(1)
33+
line += buffer
34+
try:
35+
decoded = line.decode("utf-8")
36+
except:
37+
continue
38+
39+
if decoded.startswith("System Prompt") and decoded.endswith(": "):
40+
print(f"| {decoded}")
41+
proc.stdin.write("\n".encode("utf-8"))
42+
proc.stdin.flush()
43+
line = b""
44+
elif line.decode("utf-8").startswith("User: "):
45+
print(f"| {decoded}")
3146
break
32-
output += line.decode("utf-8").strip() + "\n"
47+
48+
if decoded.endswith("\r") or decoded.endswith("\n"):
49+
decoded = decoded.strip()
50+
print(f"| {decoded}")
51+
output += decoded + "\n"
52+
line = b""
53+
3354
return render_template(
3455
"chat.html",
3556
convo="Hello! What is your prompt?",
@@ -44,23 +65,41 @@ def chat():
4465
proc.stdin.write((_prompt + "\n").encode("utf-8"))
4566
proc.stdin.flush()
4667

68+
print(f"User: {_prompt}")
69+
70+
line = b""
4771
output = ""
4872
global disable_input
4973

5074
while True:
51-
line = proc.stdout.readline()
52-
if line.decode("utf-8").startswith("What is your prompt?"):
75+
buffer = proc.stdout.read(1)
76+
line += buffer
77+
try:
78+
decoded = line.decode("utf-8")
79+
except:
80+
continue
81+
82+
if decoded.startswith("User: "):
5383
break
54-
if line.decode("utf-8").startswith("=========="):
84+
if decoded.startswith("=========="):
5585
disable_input = True
5686
break
57-
output += line.decode("utf-8").strip() + "\n"
87+
if decoded.endswith("\r") or decoded.endswith("\n"):
88+
decoded = decoded.strip()
89+
print(f"| {decoded}")
90+
output += decoded + "\n"
91+
line = b""
92+
93+
# Strip "Model: " from output
94+
model_prefix = "Model: "
95+
if output.startswith(model_prefix):
96+
output = output[len(model_prefix):]
5897

5998
global convo
6099

61100
if _prompt:
62-
convo += "<H1>Your prompt</H1>\n<p> " + _prompt + " </p>\n\n"
63-
convo += "<H1>My response</H1>\n<p> " + output + " </p>\n\n"
101+
convo += "<H1>User</H1>\n<p> " + _prompt + " </p>\n\n"
102+
convo += "<H1>Model</H1>\n<p> " + output + " </p>\n\n"
64103

65104
return render_template("chat.html", convo=convo, disable_input=disable_input)
66105

0 commit comments

Comments
 (0)