@@ -20,16 +20,37 @@ def create_app(*args):
20
20
["python3" , "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
+ 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 } " )
31
46
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
+
33
54
return render_template (
34
55
"chat.html" ,
35
56
convo = "Hello! What is your prompt?" ,
@@ -44,23 +65,41 @@ def chat():
44
65
proc .stdin .write ((_prompt + "\n " ).encode ("utf-8" ))
45
66
proc .stdin .flush ()
46
67
68
+ print (f"User: { _prompt } " )
69
+
70
+ line = b""
47
71
output = ""
48
72
global disable_input
49
73
50
74
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: " ):
53
83
break
54
- if line . decode ( "utf-8" ) .startswith ("==========" ):
84
+ if decoded .startswith ("==========" ):
55
85
disable_input = True
56
86
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 ):]
58
97
59
98
global convo
60
99
61
100
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 "
64
103
65
104
return render_template ("chat.html" , convo = convo , disable_input = disable_input )
66
105
0 commit comments