Skip to content

Commit 16544b1

Browse files
Olivia-liumalfet
authored andcommitted
Disable the SEND button when number of conversations goes beyond num_samples (#300)
* chat in browser * remove jinja2 comment seems irrelavant * remove jinja2 comment seems irrelavant * remove debug prints * use torchchat as entry point * fix num-samples breaking browser * hard-code a really big num-samples; have a way to disable input when num_samples is reached * fix num-samples breaking browser * hard-code a really big num-samples; have a way to disable input when num_samples is reached * remove a debug print
1 parent ba783a0 commit 16544b1

File tree

3 files changed

+23
-15
lines changed

3 files changed

+23
-15
lines changed

chat_in_browser.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88

99
convo = ""
10+
disable_input = False
1011

1112
def create_app(*args):
1213
app = Flask(__name__)
@@ -20,12 +21,14 @@ def create_app(*args):
2021
@app.route('/')
2122
def main():
2223
output = ""
24+
global disable_input
25+
2326
while True:
2427
line = proc.stdout.readline()
2528
if line.decode('utf-8').startswith("What is your prompt?"):
2629
break
2730
output += line.decode('utf-8').strip() + "\n"
28-
return render_template('chat.html', convo="Hello! What is your prompt?")
31+
return render_template('chat.html', convo="Hello! What is your prompt?", disable_input=disable_input)
2932

3033
@app.route('/chat', methods=['POST'])
3134
def chat():
@@ -35,10 +38,15 @@ def chat():
3538
proc.stdin.flush()
3639

3740
output = ""
41+
global disable_input
42+
3843
while True:
3944
line = proc.stdout.readline()
4045
if line.decode('utf-8').startswith("What is your prompt?"):
4146
break
47+
if line.decode('utf-8').startswith("=========="):
48+
disable_input = True
49+
break
4250
output += line.decode('utf-8').strip() + "\n"
4351

4452
global convo
@@ -47,6 +55,6 @@ def chat():
4755
convo += "Your prompt:\n" + _prompt + "\n\n"
4856
convo += "My response:\n" + output + "\n\n"
4957

50-
return render_template('chat.html', convo=convo)
58+
return render_template('chat.html', convo=convo, disable_input=disable_input)
5159

5260
return app

templates/chat.html

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
<!DOCTYPE html>
22
<html lang="en">
3-
<head>
4-
<meta charset="utf-8">
5-
<title>torchchat</title>
6-
</head>
7-
<body>
8-
<pre>{{ convo }}</pre>
9-
<form action="chat" method="post">
10-
<label for="username">Prompt: </label>
11-
<input type="text" id="prompt" name="prompt"><br>
12-
<input type="submit" value="SEND">
13-
</form>
14-
</body>
3+
<head>
4+
<meta charset="utf-8">
5+
<title>torchchat</title>
6+
</head>
7+
<body>
8+
<pre>{{ convo }}</pre>
9+
<form action="chat" method="post">
10+
<label for="username">Prompt: </label>
11+
<input type="text" {{ 'disabled' if disable_input else '' }} id="prompt" name="prompt"><br>
12+
<input type="submit" value="SEND">
13+
</form>
14+
</body>
1515
</html>

torchchat.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@
6464
# TODO: add check_args()
6565

6666
# Assume the user wants "chat" when entering "browser". TODO: add support for "generate" as well
67-
args_plus_chat = ['"{}"'.format(s) for s in sys.argv[2:]] + ["\"--chat\""]
67+
args_plus_chat = ['"{}"'.format(s) for s in sys.argv[2:]] + ['"--chat"'] + ['"--num-samples"'] + ['"1000000"']
6868
formatted_args = ", ".join(args_plus_chat)
6969
command = ["flask", "--app", "chat_in_browser:create_app(" + formatted_args + ")", "run"]
7070
subprocess.run(command)

0 commit comments

Comments
 (0)