Skip to content
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

streamlit based web_demo #117

Merged
merged 1 commit into from
Mar 17, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 56 additions & 0 deletions web_demo2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
from transformers import AutoModel, AutoTokenizer
import streamlit as st
from streamlit_chat import message


st.set_page_config(
page_title="ChatGLM-6b 演示",
page_icon=":robot:"
)


@st.cache_resource
def get_model():
tokenizer = AutoTokenizer.from_pretrained("/THUDM/chatglm-6b", trust_remote_code=True)
model = AutoModel.from_pretrained("THUDM/chatglm-6b", trust_remote_code=True).half().cuda()
model = model.eval()
return tokenizer, model


MAX_TURNS = 20
MAX_BOXES = MAX_TURNS * 2


def predict(input, history=None):
tokenizer, model = get_model()
if history is None:
history = []
response, history = model.chat(tokenizer, input, history)

#updates = []
for i, (query, response) in enumerate(history):
#updates.append("用户:" + query)
message(query, avatar_style="big-smile", key=str(i) + "_user")
#updates.append("ChatGLM-6B:" + response)
message(response, avatar_style="bottts", key=str(i))

# if len(updates) < MAX_BOXES:
# updates = updates + [""] * (MAX_BOXES - len(updates))

return history


# create a prompt text for the text generation
prompt_text = st.text_area(label="用户命令输入",
height = 100,
placeholder="请在这儿输入您的命令")

if 'state' not in st.session_state:
st.session_state['state'] = []

if st.button("发送", key="predict"):
with st.spinner("AI正在思考,请稍等........"):
# text generation
st.session_state["state"] = predict(prompt_text, st.session_state["state"])

st.balloons()