Skip to content

Commit a24e776

Browse files
author
Varun Puri
committed
Replace browser UI with Basic Streamlit UI Implementation
1 parent c5f7f4d commit a24e776

File tree

7 files changed

+112
-285
lines changed

7 files changed

+112
-285
lines changed

README.md

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -120,22 +120,21 @@ For more information run `python3 torchchat.py generate --help`
120120

121121
### Browser
122122

123+
Use Streamlit to launch an interactive chat with your model. Running the command will automatically open a tab in your browser.
124+
```
125+
streamlit run torchchat.py -- browser <model_name> <model_args>
126+
```
127+
128+
For example, to quantise and chat with LLaMA3:
123129
[skip default]: begin
124130
```
125-
python3 torchchat.py browser llama3
131+
streamlit run torchchat.py -- browser llama3 --quantize '{"precision": {"dtype":"float16"}, "executor":{"accelerator":"cpu"}}' --max-new-tokens 256 --compile
126132
```
127133
[skip default]: end
128134

129135

130-
*Running on http://127.0.0.1:5000* should be printed out on the
131-
terminal. Click the link or go to
132-
[http://127.0.0.1:5000](http://127.0.0.1:5000) on your browser to
133-
start interacting with it.
134136

135-
Enter some text in the input box, then hit the enter key or click the
136-
“SEND” button. After a second or two, the text you entered together
137-
with the generated text will be displayed. Repeat to have a
138-
conversation.
137+
139138

140139

141140

browser/browser.py

Lines changed: 87 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,91 @@
1-
import subprocess
2-
import sys
1+
# Copyright (c) Meta Platforms, Inc. and affiliates.
2+
# All rights reserved.
3+
4+
# This source code is licensed under the license found in the
5+
# LICENSE file in the root directory of this source tree.
6+
7+
import time
8+
9+
import streamlit as st
10+
from api.api import CompletionRequest, OpenAiApiGenerator
11+
12+
from build.builder import BuilderArgs, TokenizerArgs
13+
14+
from generate import GeneratorArgs
315

416

517
def main(args):
18+
builder_args = BuilderArgs.from_args(args)
19+
speculative_builder_args = BuilderArgs.from_speculative_args(args)
20+
tokenizer_args = TokenizerArgs.from_args(args)
21+
generator_args = GeneratorArgs.from_args(args)
22+
generator_args.chat_mode = False
23+
24+
@st.cache_resource
25+
def initialize_generator() -> OpenAiApiGenerator:
26+
return OpenAiApiGenerator(
27+
builder_args,
28+
speculative_builder_args,
29+
tokenizer_args,
30+
generator_args,
31+
args.profile,
32+
args.quantize,
33+
args.draft_quantize,
34+
)
35+
36+
gen = initialize_generator()
37+
38+
st.title("TorchChat")
39+
40+
# Initialize chat history
41+
if "messages" not in st.session_state:
42+
st.session_state.messages = []
43+
44+
# Display chat messages from history on app rerun
45+
for message in st.session_state.messages:
46+
with st.chat_message(message["role"]):
47+
st.markdown(message["content"])
48+
49+
# Accept user input
50+
if prompt := st.chat_input("What is up?"):
51+
# Add user message to chat history
52+
st.session_state.messages.append({"role": "user", "content": prompt})
53+
# Display user message in chat message container
54+
with st.chat_message("user"):
55+
st.markdown(prompt)
56+
57+
# Display assistant response in chat message container
58+
with st.chat_message("assistant"), st.status(
59+
"Generating... ", expanded=True
60+
) as status:
61+
62+
req = CompletionRequest(
63+
model=gen.builder_args.checkpoint_path,
64+
prompt=prompt,
65+
temperature=generator_args.temperature,
66+
messages=[],
67+
)
68+
69+
def unwrap(completion_generator):
70+
start = time.time()
71+
tokcount = 0
72+
for chunk_response in completion_generator:
73+
content = chunk_response.choices[0].delta.content
74+
if not gen.is_llama3_model or content not in set(
75+
gen.tokenizer.special_tokens.keys()
76+
):
77+
yield content
78+
if content == gen.tokenizer.eos_id():
79+
yield "."
80+
tokcount += 1
81+
status.update(
82+
label="Done, averaged {:.2f} tokens/second".format(
83+
tokcount / (time.time() - start)
84+
),
85+
state="complete",
86+
)
87+
88+
response = st.write_stream(unwrap(gen.completion(req)))
689

7-
# Directory Containing the server file "chat_in_browser.py"
8-
server_dir = "browser"
9-
10-
# Look for port from cmd args. Default to 5000 if not found.
11-
port = 5000
12-
i = 2
13-
while i < len(sys.argv):
14-
if sys.argv[i] == "--port":
15-
if i + 1 < len(sys.argv):
16-
# Extract the value and remove '--port' and the value from sys.argv
17-
port = sys.argv[i + 1]
18-
del sys.argv[i : i + 2]
19-
break
20-
else:
21-
i += 1
22-
23-
# Construct arguments for the flask app minus 'browser' command
24-
# plus '--chat'
25-
args_plus_chat = ["'{}'".format(s) for s in sys.argv[1:] if s != "browser"] + [
26-
'"--chat"'
27-
]
28-
formatted_args = ", ".join(args_plus_chat)
29-
command = [
30-
"flask",
31-
"--app",
32-
f"{server_dir}/chat_in_browser:create_app(" + formatted_args + ")",
33-
"run",
34-
"--port",
35-
f"{port}",
36-
]
37-
subprocess.run(command)
90+
# Add assistant response to chat history
91+
st.session_state.messages.append({"role": "assistant", "content": response})

browser/chat_in_browser.py

Lines changed: 0 additions & 107 deletions
This file was deleted.

browser/static/css/style.css

Lines changed: 0 additions & 96 deletions
This file was deleted.

browser/templates/chat.html

Lines changed: 0 additions & 27 deletions
This file was deleted.

0 commit comments

Comments
 (0)