Update app.py
Browse files
app.py
CHANGED
|
@@ -22,6 +22,15 @@ task = "text-generation" # Change this to your model's task
|
|
| 22 |
# Load the model using the pipeline
|
| 23 |
model_pipeline = pipeline(task, model=model,tokenizer=tokenizer)
|
| 24 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
|
| 26 |
#Application
|
| 27 |
with st.container():
|
|
@@ -43,22 +52,15 @@ with st.container():
|
|
| 43 |
|
| 44 |
if 'model' not in st.session_state:
|
| 45 |
st.session_state.model = model
|
| 46 |
-
|
| 47 |
-
#renders chat history
|
| 48 |
-
for message in st.session_state.chat_history:
|
| 49 |
-
if(message["role"]!= "system"):
|
| 50 |
-
with st.chat_message(message["role"]):
|
| 51 |
-
st.write(message["content"])
|
| 52 |
-
|
| 53 |
|
| 54 |
#Set up input text field
|
| 55 |
input_text = st.chat_input(placeholder="Here you can chat with our hotel booking model.")
|
| 56 |
|
| 57 |
if input_text:
|
| 58 |
-
with st.chat_message("user"):
|
| 59 |
#st.write(input_text)
|
| 60 |
-
|
| 61 |
-
|
| 62 |
#chat_response = demo_chat.demo_chain(input_text=input_text, memory=st.session_state.memory, model= chat_model)
|
| 63 |
#first_answer = chat_response.split("Human")[0] #Because of Predict it prints the whole conversation.Here we seperate the first answer only.
|
| 64 |
tokenized_chat = tokenizer.apply_chat_template(st.session_state.chat_history, tokenize=True, add_generation_prompt=True, return_tensors="pt")
|
|
@@ -66,7 +68,10 @@ with st.container():
|
|
| 66 |
outputs = model.generate(tokenized_chat, max_new_tokens=128)
|
| 67 |
first_answer = tokenizer.decode(outputs[0][tokenized_chat.shape[1]:],skip_special_tokens=True)
|
| 68 |
|
| 69 |
-
with st.chat_message("assistant"):
|
| 70 |
#st.write(first_answer)
|
| 71 |
-
|
| 72 |
-
st.markdown('</div>', unsafe_allow_html=True)
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
# Load the model using the pipeline
|
| 23 |
model_pipeline = pipeline(task, model=model,tokenizer=tokenizer)
|
| 24 |
|
| 25 |
+
def render_chat_history(chat_history):
|
| 26 |
+
#renders chat history
|
| 27 |
+
for message in chat_history:
|
| 28 |
+
if(message["role"]!= "system"):
|
| 29 |
+
with st.chat_message(message["role"]):
|
| 30 |
+
st.write(message["content"])
|
| 31 |
+
|
| 32 |
+
|
| 33 |
+
message_container = st.empty()
|
| 34 |
|
| 35 |
#Application
|
| 36 |
with st.container():
|
|
|
|
| 52 |
|
| 53 |
if 'model' not in st.session_state:
|
| 54 |
st.session_state.model = model
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 55 |
|
| 56 |
#Set up input text field
|
| 57 |
input_text = st.chat_input(placeholder="Here you can chat with our hotel booking model.")
|
| 58 |
|
| 59 |
if input_text:
|
| 60 |
+
#with st.chat_message("user"):
|
| 61 |
#st.write(input_text)
|
| 62 |
+
st.session_state.chat_history.append({"role" : "user", "content" : input_text}) #append message to chat history
|
| 63 |
+
|
| 64 |
#chat_response = demo_chat.demo_chain(input_text=input_text, memory=st.session_state.memory, model= chat_model)
|
| 65 |
#first_answer = chat_response.split("Human")[0] #Because of Predict it prints the whole conversation.Here we seperate the first answer only.
|
| 66 |
tokenized_chat = tokenizer.apply_chat_template(st.session_state.chat_history, tokenize=True, add_generation_prompt=True, return_tensors="pt")
|
|
|
|
| 68 |
outputs = model.generate(tokenized_chat, max_new_tokens=128)
|
| 69 |
first_answer = tokenizer.decode(outputs[0][tokenized_chat.shape[1]:],skip_special_tokens=True)
|
| 70 |
|
| 71 |
+
#with st.chat_message("assistant"):
|
| 72 |
#st.write(first_answer)
|
| 73 |
+
st.session_state.chat_history.append({"role": "assistant", "content": first_answer})
|
| 74 |
+
st.markdown('</div>', unsafe_allow_html=True)
|
| 75 |
+
|
| 76 |
+
with message_container:
|
| 77 |
+
render_chat_history(st.session_state.chat_history)
|