Hev832 commited on
Commit
5a4aaa7
1 Parent(s): 2b922e6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +17 -14
app.py CHANGED
@@ -28,6 +28,12 @@ safety_settings = [
28
 
29
  def generate_response(user_input, chat_history):
30
  """Generates a response based on user input and chat history."""
 
 
 
 
 
 
31
 
32
  # Create the generative model
33
  model = genai.GenerativeModel(
@@ -37,12 +43,6 @@ def generate_response(user_input, chat_history):
37
  system_instruction="You are Shadow the Hedgehog and you must act like Shadow the Hedgehog's personality.",
38
  )
39
 
40
- # Add user input to history
41
- chat_history.append({"role": "user", "content": user_input})
42
-
43
- # Limit history length to the last 10 messages
44
- chat_history = chat_history[-10:]
45
-
46
  retry_attempts = 3
47
  for attempt in range(retry_attempts):
48
  try:
@@ -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([f"{entry['role']}: {entry['content']}" for entry in chat_history])
54
  response = chat_session.send_message(formatted_history)
55
 
56
  # Append the assistant's response to history
57
- chat_history.append({"role": "assistant", "content": 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({"role": "assistant", "content": f"Error after {retry_attempts} attempts: {str(e)}"})
65
  return chat_history
66
 
67
  # Build the Gradio interface using Chatbot and Button
@@ -78,11 +78,14 @@ with gr.Blocks() as iface:
78
  outputs=chatbot
79
  )
80
 
81
- # Optional: Add an event to clear the input after submission
 
 
 
82
  user_input.submit(
83
- fn=lambda x: "", # Clear input box
84
- inputs=[user_input],
85
- outputs=[user_input]
86
- )
87
 
88
  iface.launch()
 
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", user_input))
34
+
35
+ # Limit history length to the last 10 messages
36
+ chat_history = chat_history[-10:]
37
 
38
  # Create the generative model
39
  model = genai.GenerativeModel(
 
43
  system_instruction="You are Shadow the Hedgehog and you must act like Shadow the Hedgehog's personality.",
44
  )
45
 
 
 
 
 
 
 
46
  retry_attempts = 3
47
  for attempt in range(retry_attempts):
48
  try:
 
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
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
 
78
  outputs=chatbot
79
  )
80
 
81
+ # Optional: Clear the input box after submission
82
+ def clear_input():
83
+ return ""
84
+
85
  user_input.submit(
86
+ fn=generate_response,
87
+ inputs=[user_input, chat_history_state],
88
+ outputs=chatbot
89
+ ).then(clear_input, outputs=[user_input])
90
 
91
  iface.launch()