-
Notifications
You must be signed in to change notification settings - Fork 231
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
chat in browser #242
Merged
Merged
chat in browser #242
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
33db115
chat in browser
Olivia-liu cda04f5
remove jinja2 comment seems irrelavant
Olivia-liu 1fb6006
Merge branch 'main' of github.com:pytorch/torchchat
Olivia-liu bd4be81
Merge branch 'main' of github.com:pytorch/torchchat
Olivia-liu e0cfa0e
remove jinja2 comment seems irrelavant
Olivia-liu a840968
remove debug prints
Olivia-liu 59751e5
Merge branch 'chat_in_browser' of github.com:pytorch/torchchat into c…
Olivia-liu 890c296
use torchchat as entry point
Olivia-liu File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -27,3 +27,6 @@ wheel | |
cmake | ||
ninja | ||
zstd | ||
|
||
# Browser mode | ||
flask |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="utf-8"> | ||
<title>torchchat</title> | ||
</head> | ||
<body> | ||
<pre>{{ convo }}</pre> | ||
<form action="chat" method="post"> | ||
<label for="username">Prompt: </label> | ||
<input type="text" id="prompt" name="prompt"><br> | ||
<input type="submit" value="SEND"> | ||
</form> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you can use
["'--chat'"]
to avoid escapes.