Hev832 commited on
Commit
b72347a
·
verified ·
1 Parent(s): 7f0e907

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +7 -7
app.py CHANGED
@@ -28,9 +28,9 @@ safety_settings = [
28
 
29
  def generate_response(user_input, chat_history):
30
  """Generates a response based on user input and chat history."""
31
-
32
- # Add user input to history
33
- chat_history.append(user_input)
34
 
35
  # Limit history length to the last 10 messages
36
  chat_history = chat_history[-10:]
@@ -50,18 +50,18 @@ def generate_response(user_input, chat_history):
50
  chat_session = model.start_chat()
51
 
52
  # Format the history for the model
53
- formatted_history = "\n".join(chat_history)
54
  response = chat_session.send_message(formatted_history)
55
 
56
- # Append the assistant's response to history
57
- chat_history.append(response.text)
58
  return chat_history
59
 
60
  except Exception as e:
61
  if attempt < retry_attempts - 1:
62
  continue
63
  else:
64
- chat_history.append(f"Error after {retry_attempts} attempts: {str(e)}")
65
  return chat_history
66
 
67
  # Build the Gradio interface using Chatbot and Button
 
28
 
29
  def generate_response(user_input, chat_history):
30
  """Generates a response based on user input and chat history."""
31
+
32
+ # Add user input to history as a tuple
33
+ chat_history.append(("user", user_input))
34
 
35
  # Limit history length to the last 10 messages
36
  chat_history = chat_history[-10:]
 
50
  chat_session = model.start_chat()
51
 
52
  # Format the history for the model
53
+ formatted_history = "\n".join([f"{role}: {msg}" for role, msg in chat_history])
54
  response = chat_session.send_message(formatted_history)
55
 
56
+ # Append the assistant's response to history as a tuple
57
+ chat_history.append(("assistant", response.text))
58
  return chat_history
59
 
60
  except Exception as e:
61
  if attempt < retry_attempts - 1:
62
  continue
63
  else:
64
+ chat_history.append(("assistant", f"Error after {retry_attempts} attempts: {str(e)}"))
65
  return chat_history
66
 
67
  # Build the Gradio interface using Chatbot and Button