Wootang01 commited on
Commit
a4ce809
·
1 Parent(s): cf7a4e9

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +35 -0
app.py ADDED
@@ -0,0 +1,35 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import torch
3
+ import transformers
4
+ from transformers import AutoModelForCausalLM, AutoTokenizer
5
+
6
+ def load_data():
7
+ tokenizer = AutoTokenizer.from_pretrained("microsoft/DialoGPT-medium")
8
+ model = AutoModelForCausalLM.from_pretrained("microsoft/DialoGPT-medium")
9
+ return tokenizer, model
10
+
11
+ tokenizer, model = load_data()
12
+
13
+ st.title("Chatbot -- Microsoft DialoGPT Medium")
14
+ st.write("Write or paste text as if sending a text message to a person. The machine will attempt to respond appropriately.")
15
+ input = st.text_input('Your text message:')
16
+
17
+ if 'count' not in st.session_state or st.session_state.count == 6:
18
+ st.session_state.count = 0
19
+ st.session_state.chat_history_ids = None
20
+ st.session_state.old_response = ''
21
+ else:
22
+ st.session_state.count += 1
23
+
24
+ new_user_input_ids = tokenizer.encode(input + tokenizer.eos_token, return_tensors='pt')
25
+ bot_input_ids = torch.cat([st.session_state.chat_history_ids, new_user_input_ids], dim=-1) if st.session_state.count > 1 else new_user_input_ids
26
+ st.session_state.chat_history_ids = model.generate(bot_input_ids, max_length=500, pad_token_id=tokenizer.eos_token_id)
27
+ response = tokenizer.decode(st.session_state.chat_history_ids[:, bot_input_ids.shape[-1]:][0], skip_special_tokens=True)
28
+
29
+ if st.session_state.old_response == response:
30
+ bot_input_ids = new_user_input_ids
31
+ st.session_state.chat_history_ids = model.generate(bot_input_ids, max_length=5000, pad_token_id=tokenizer.eos_token_id)
32
+ response = tokenizer.decode(st.session_state.chat_history_ids[:, bot_input_ids.shape[-1]:][0], skip_special_tokens=True)
33
+
34
+ st.write(f"Machine text message: {response}")
35
+ st.session_state.old_response = response