Skip to content

Commit f24f79d

Browse files
vmpuridbort
authored andcommitted
Replace browser UI with Basic Streamlit UI Implementation
Remove the existing browser UI and replace it with a UI built with Streamlit. This reduces complexity & leverages the functionality introduced in PR #906 to display chunked responses. **Testing** ``` streamlit run torchchat.py -- browser stories110M --compile --max-new-tokens 256 You can now view your Streamlit app in your browser. Local URL: http://localhost:8501 Network URL: http://192.0.0.2:8501 ``` <img width="1002" alt="image" src="https://github.com/user-attachments/assets/df305943-2326-4d01-a48b-61dd2006fa28">
1 parent b0081ed commit f24f79d

File tree

7 files changed

+105
-283
lines changed

7 files changed

+105
-283
lines changed

README.md

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

124124
### Browser
125125
This mode provides access to the model via the browser's localhost.
126+
127+
Launch an interactive chat with your model. Running the command will automatically open a tab in your browser. [Streamlit](https://streamlit.io/) should already be installed by the `install_requirements.sh` script.
128+
```
129+
streamlit run torchchat.py -- browser <model_name> <model_args>
130+
```
131+
132+
For example, to quantize and chat with LLaMA3:
126133
[skip default]: begin
127134
```
128-
python3 torchchat.py browser llama3
135+
streamlit run torchchat.py -- browser llama3 --quantize '{"precision": {"dtype":"float16"}, "executor":{"accelerator":"cpu"}}' --max-new-tokens 256 --compile
129136
```
130137
[skip default]: end
131138

132139

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

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

143143

144144

browser/browser.py

Lines changed: 81 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -4,40 +4,88 @@
44
# This source code is licensed under the license found in the
55
# LICENSE file in the root directory of this source tree.
66

7-
import subprocess
8-
import sys
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
915

1016

1117
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)))
1289

13-
# Directory Containing the server file "chat_in_browser.py"
14-
server_dir = "browser"
15-
16-
# Look for port from cmd args. Default to 5000 if not found.
17-
port = 5000
18-
i = 2
19-
while i < len(sys.argv):
20-
if sys.argv[i] == "--port":
21-
if i + 1 < len(sys.argv):
22-
# Extract the value and remove '--port' and the value from sys.argv
23-
port = sys.argv[i + 1]
24-
del sys.argv[i : i + 2]
25-
break
26-
else:
27-
i += 1
28-
29-
# Construct arguments for the flask app minus 'browser' command
30-
# plus '--chat'
31-
args_plus_chat = ["'{}'".format(s) for s in sys.argv[1:] if s != "browser"] + [
32-
'"--chat"'
33-
]
34-
formatted_args = ", ".join(args_plus_chat)
35-
command = [
36-
"flask",
37-
"--app",
38-
f"{server_dir}/chat_in_browser:create_app(" + formatted_args + ")",
39-
"run",
40-
"--port",
41-
f"{port}",
42-
]
43-
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)