forked from pytorch/torchchat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
chat_in_browser.py
52 lines (41 loc) · 1.58 KB
/
chat_in_browser.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# -*- coding: UTF-8 -*-
from flask import Flask, render_template, request
from cli import add_arguments_for_generate, arg_init, check_args
from generate import main as generate_main
import subprocess
import sys
convo = ""
def create_app(*args):
app = Flask(__name__)
import subprocess
# create a new process and set up pipes for communication
proc = subprocess.Popen(["python", "generate.py", *args],
stdin=subprocess.PIPE,
stdout=subprocess.PIPE)
@app.route('/')
def main():
output = ""
while True:
line = proc.stdout.readline()
if line.decode('utf-8').startswith("What is your prompt?"):
break
output += line.decode('utf-8').strip() + "\n"
return render_template('chat.html', convo="Hello! What is your prompt?")
@app.route('/chat', methods=['POST'])
def chat():
# Retrieve the HTTP POST request parameter value from 'request.form' dictionary
_prompt = request.form.get('prompt')
proc.stdin.write((_prompt + "\n").encode('utf-8'))
proc.stdin.flush()
output = ""
while True:
line = proc.stdout.readline()
if line.decode('utf-8').startswith("What is your prompt?"):
break
output += line.decode('utf-8').strip() + "\n"
global convo
if _prompt:
convo += "Your prompt:\n" + _prompt + "\n\n"
convo += "My response:\n" + output + "\n\n"
return render_template('chat.html', convo=convo)
return app