yasserrmd commited on
Commit
7cccfe4
·
verified ·
1 Parent(s): 90bb291

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -12
app.py CHANGED
@@ -37,17 +37,20 @@ def generate_response(history, user_input):
37
  """
38
  Generates a response from the model based on the chat history and user input.
39
  """
40
- # Append user input to the chat history
41
- history.append({"role": "user", "content": user_input})
42
 
43
- # Build messages for the model
44
- messages = [{"role": "system", "content": SYSTEM_INSTRUCTION}] + history
 
 
 
45
 
46
- # Tokenize input for the model
47
  text = apply_chat_template(messages)
48
  model_inputs = tokenizer([text], return_tensors="pt").to(device)
49
 
50
- # Generate response
51
  generated_ids = model.generate(
52
  **model_inputs,
53
  max_new_tokens=512
@@ -57,13 +60,11 @@ def generate_response(history, user_input):
57
  ]
58
  response = tokenizer.batch_decode(generated_ids, skip_special_tokens=True)[0]
59
 
60
- # Append the assistant's response to history
61
- history.append({"role": "assistant", "content": response})
62
-
63
- # Format the conversation for display
64
- formatted_history = format_chat_history(history)
65
 
66
- return formatted_history, history
 
67
 
68
  def format_chat_history(history):
69
  """
 
37
  """
38
  Generates a response from the model based on the chat history and user input.
39
  """
40
+ # Append user input to chat history
41
+ chat_history.append(("User", user_input))
42
 
43
+ # Prepare messages for the model
44
+ messages = [{"role": "system", "content": SYSTEM_INSTRUCTION}] + [
45
+ {"role": "user", "content": msg[1]} if msg[0] == "User" else {"role": "assistant", "content": msg[1]}
46
+ for msg in chat_history
47
+ ]
48
 
49
+ # Tokenize the input for the model
50
  text = apply_chat_template(messages)
51
  model_inputs = tokenizer([text], return_tensors="pt").to(device)
52
 
53
+ # Generate the model's response
54
  generated_ids = model.generate(
55
  **model_inputs,
56
  max_new_tokens=512
 
60
  ]
61
  response = tokenizer.batch_decode(generated_ids, skip_special_tokens=True)[0]
62
 
63
+ # Append AI response to chat history
64
+ chat_history.append(("MathTutor", response))
 
 
 
65
 
66
+ # Return updated chat history
67
+ return chat_history
68
 
69
  def format_chat_history(history):
70
  """