|
4 | 4 | # This source code is licensed under the license found in the
|
5 | 5 | # LICENSE file in the root directory of this source tree.
|
6 | 6 |
|
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 |
9 | 15 |
|
10 | 16 |
|
11 | 17 | 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))) |
12 | 89 |
|
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}) |
0 commit comments