Skip to content

Commit 66ce22d

Browse files
committed
Update browser logic to match updated chat format
1 parent 82e8cb6 commit 66ce22d

File tree

1 file changed

+43
-10
lines changed

1 file changed

+43
-10
lines changed

chat_in_browser.py

Lines changed: 43 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,37 @@ def create_app(*args):
1717

1818
# create a new process and set up pipes for communication
1919
proc = subprocess.Popen(
20-
["python3", "generate.py", *args], stdin=subprocess.PIPE, stdout=subprocess.PIPE
20+
["python", "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+
decoded = line.decode("utf-8")
35+
36+
if decoded.startswith("System Prompt") and decoded.endswith(": "):
37+
print(f"| {decoded}")
38+
proc.stdin.write("\n".encode("utf-8"))
39+
proc.stdin.flush()
40+
line = b""
41+
elif line.decode("utf-8").startswith("User: "):
42+
print(f"| {decoded}")
3143
break
32-
output += line.decode("utf-8").strip() + "\n"
44+
45+
if decoded.endswith("\r") or decoded.endswith("\n"):
46+
decoded = decoded.strip()
47+
print(f"| {decoded}")
48+
output += decoded + "\n"
49+
line = b""
50+
3351
return render_template(
3452
"chat.html",
3553
convo="Hello! What is your prompt?",
@@ -44,23 +62,38 @@ def chat():
4462
proc.stdin.write((_prompt + "\n").encode("utf-8"))
4563
proc.stdin.flush()
4664

65+
print(f"User: {_prompt}")
66+
67+
line = b""
4768
output = ""
4869
global disable_input
4970

5071
while True:
51-
line = proc.stdout.readline()
52-
if line.decode("utf-8").startswith("What is your prompt?"):
72+
buffer = proc.stdout.read(1)
73+
line += buffer
74+
decoded = line.decode("utf-8")
75+
76+
if decoded.startswith("User: "):
5377
break
54-
if line.decode("utf-8").startswith("=========="):
78+
if decoded.startswith("=========="):
5579
disable_input = True
5680
break
57-
output += line.decode("utf-8").strip() + "\n"
81+
if decoded.endswith("\r") or decoded.endswith("\n"):
82+
decoded = decoded.strip()
83+
print(f"| {decoded}")
84+
output += decoded + "\n"
85+
line = b""
86+
87+
# Strip "Model: " from output
88+
model_prefix = "Model: "
89+
if output.startswith(model_prefix):
90+
output = output[len(model_prefix):]
5891

5992
global convo
6093

6194
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"
95+
convo += "<H1>User</H1>\n<p> " + _prompt + " </p>\n\n"
96+
convo += "<H1>Model</H1>\n<p> " + output + " </p>\n\n"
6497

6598
return render_template("chat.html", convo=convo, disable_input=disable_input)
6699

0 commit comments

Comments
 (0)