@@ -17,19 +17,37 @@ def create_app(*args):
17
17
18
18
# create a new process and set up pipes for communication
19
19
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
21
21
)
22
22
23
+
23
24
@app .route ("/" )
24
25
def main ():
26
+ print ("Starting chat session." )
27
+ line = b""
25
28
output = ""
26
29
global disable_input
27
30
28
31
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 } " )
31
43
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
+
33
51
return render_template (
34
52
"chat.html" ,
35
53
convo = "Hello! What is your prompt?" ,
@@ -44,23 +62,38 @@ def chat():
44
62
proc .stdin .write ((_prompt + "\n " ).encode ("utf-8" ))
45
63
proc .stdin .flush ()
46
64
65
+ print (f"User: { _prompt } " )
66
+
67
+ line = b""
47
68
output = ""
48
69
global disable_input
49
70
50
71
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: " ):
53
77
break
54
- if line . decode ( "utf-8" ) .startswith ("==========" ):
78
+ if decoded .startswith ("==========" ):
55
79
disable_input = True
56
80
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 ):]
58
91
59
92
global convo
60
93
61
94
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 "
64
97
65
98
return render_template ("chat.html" , convo = convo , disable_input = disable_input )
66
99
0 commit comments