hertogateis commited on
Commit
016a685
·
verified ·
1 Parent(s): 45704f0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +21 -4
app.py CHANGED
@@ -1,6 +1,7 @@
1
  import streamlit as st
2
  from transformers import AutoModelForCausalLM, AutoTokenizer
3
  import torch
 
4
 
5
  # Load pre-trained DialoGPT-small model and tokenizer
6
  model_name = "microsoft/DialoGPT-small"
@@ -11,24 +12,40 @@ tokenizer = AutoTokenizer.from_pretrained(model_name)
11
  device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
12
  model.to(device)
13
 
14
- # Initialize chat history
15
  if 'history' not in st.session_state:
16
  st.session_state['history'] = []
17
  if 'conversation' not in st.session_state:
18
  st.session_state['conversation'] = []
19
 
 
 
 
 
 
 
 
 
 
 
 
 
20
  def generate_response(input_text):
 
 
 
 
 
 
 
21
  # Encode the new user input, add end of string token
22
  new_user_input_ids = tokenizer.encode(input_text + tokenizer.eos_token, return_tensors='pt').to(device)
23
 
24
  # If there is conversation history, append the new input to it
25
  if st.session_state['history']:
26
- # Convert history to a 2D tensor (batch_size x seq_len)
27
  history_tensor = torch.tensor(st.session_state['history']).unsqueeze(0).to(device)
28
- # Concatenate history with the new input
29
  bot_input_ids = torch.cat([history_tensor, new_user_input_ids], dim=-1)
30
  else:
31
- # If no history, just use the new user input
32
  bot_input_ids = new_user_input_ids
33
 
34
  # Generate a response from the model
 
1
  import streamlit as st
2
  from transformers import AutoModelForCausalLM, AutoTokenizer
3
  import torch
4
+ import random
5
 
6
  # Load pre-trained DialoGPT-small model and tokenizer
7
  model_name = "microsoft/DialoGPT-small"
 
12
  device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
13
  model.to(device)
14
 
15
+ # Initialize chat history and conversation context
16
  if 'history' not in st.session_state:
17
  st.session_state['history'] = []
18
  if 'conversation' not in st.session_state:
19
  st.session_state['conversation'] = []
20
 
21
+ # Define multiple system prompts to control bot's behavior
22
+ system_prompts = [
23
+ "You are a friendly and professional assistant. You respond in a polite and helpful manner.",
24
+ "You are a casual chatbot that likes to engage in fun and interesting conversations, but always stay respectful.",
25
+ "You are a helpful assistant. Your goal is to provide clear and precise answers to any questions.",
26
+ "You are a compassionate and empathetic listener, always responding with kindness and understanding."
27
+ ]
28
+
29
+ # Select a random system prompt to start the conversation
30
+ def get_system_prompt():
31
+ return random.choice(system_prompts)
32
+
33
  def generate_response(input_text):
34
+ # If it's the first interaction, add the system prompt to the conversation history
35
+ if not st.session_state['history']:
36
+ system_prompt = get_system_prompt()
37
+ st.session_state['conversation'].append(f"System: {system_prompt}")
38
+ system_input_ids = tokenizer.encode(system_prompt + tokenizer.eos_token, return_tensors='pt').to(device)
39
+ st.session_state['history'] = system_input_ids[0].tolist()
40
+
41
  # Encode the new user input, add end of string token
42
  new_user_input_ids = tokenizer.encode(input_text + tokenizer.eos_token, return_tensors='pt').to(device)
43
 
44
  # If there is conversation history, append the new input to it
45
  if st.session_state['history']:
 
46
  history_tensor = torch.tensor(st.session_state['history']).unsqueeze(0).to(device)
 
47
  bot_input_ids = torch.cat([history_tensor, new_user_input_ids], dim=-1)
48
  else:
 
49
  bot_input_ids = new_user_input_ids
50
 
51
  # Generate a response from the model