pszemraj commited on
Commit
5eed9b0
·
verified ·
1 Parent(s): 006fc23

sonnets last attempt

Browse files

before I cut my losses

Files changed (1) hide show
  1. app.py +27 -30
app.py CHANGED
@@ -103,15 +103,17 @@ def is_valid_length(text: str, min_words: int = 3, max_words: int = 50) -> bool:
103
  def is_verbatim_repetition(
104
  new_text: str, history: List[Tuple[str, str]], system_prompt: str
105
  ) -> bool:
106
- """Check if text is exact repetition of prior user turn or system prompt (Guardrail 4)."""
 
 
 
107
  new_text_normalized = new_text.strip().lower()
108
 
109
- # Check against system prompt
110
  if new_text_normalized == system_prompt.strip().lower():
111
  return True
112
 
113
- # Check against previous user messages (UserLM's prior outputs)
114
- for user_msg, _ in history:
115
  if user_msg and new_text_normalized == user_msg.strip().lower():
116
  return True
117
 
@@ -179,32 +181,31 @@ def generate_next_turn(
179
  top_p: float,
180
  ):
181
  """
182
- Generate the next user message from UserLM.
183
-
184
- Flow:
185
- - If chat_history is empty: Generate first user message
186
- - If chat_history exists:
187
- 1. Add assistant's response to last turn
188
- 2. Generate next user message
189
-
190
- Tuple structure: (user_message_from_userlm, assistant_response_from_human)
191
- - Position 0 (left): UserLM's messages
192
- - Position 1 (right): Human's assistant responses
193
  """
194
 
195
- # If we have an assistant response, add it to the last turn
196
  if assistant_response.strip() and len(chat_history) > 0:
197
- last_user_msg, _ = chat_history[-1]
198
- chat_history = chat_history[:-1] + [(last_user_msg, assistant_response.strip())]
 
 
 
 
 
199
 
200
- # Build messages for UserLM
201
- messages = build_messages_for_userlm(system_prompt, chat_history)
 
 
 
202
 
203
  # Generate next user message
204
  try:
205
  user_msg = generate_user_message(
206
  messages,
207
- chat_history,
208
  system_prompt,
209
  max_new_tokens=max_new_tokens,
210
  temperature=temperature,
@@ -213,14 +214,10 @@ def generate_next_turn(
213
  except Exception as e:
214
  user_msg = f"(Generation error: {e})"
215
 
216
- # Add new user message to history (with empty assistant slot)
217
- new_history = chat_history + [(user_msg, "")]
218
-
219
- # Determine button text for next action
220
- needs_assistant_response = True
221
- button_text = "Generate Next User Message"
222
 
223
- return "", new_history, button_text
224
 
225
 
226
  def clear_conversation():
@@ -256,8 +253,8 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
256
  chatbot = gr.Chatbot(
257
  height=420,
258
  label="Conversation",
259
- type="tuples",
260
- # Left side = UserLM (simulated user), Right side = You (playing assistant)
261
  )
262
 
263
  with gr.Row():
 
103
  def is_verbatim_repetition(
104
  new_text: str, history: List[Tuple[str, str]], system_prompt: str
105
  ) -> bool:
106
+ """Check if text is exact repetition of prior user turn or system prompt (Guardrail 4).
107
+
108
+ History format: (assistant_msg, user_msg) - so user messages are in position 1
109
+ """
110
  new_text_normalized = new_text.strip().lower()
111
 
 
112
  if new_text_normalized == system_prompt.strip().lower():
113
  return True
114
 
115
+ # User messages are now in position 1 of the tuple
116
+ for _, user_msg in history:
117
  if user_msg and new_text_normalized == user_msg.strip().lower():
118
  return True
119
 
 
181
  top_p: float,
182
  ):
183
  """
184
+ Tuple: (human_assistant, userlm_user)
185
+ Testing if Gradio shows first element on RIGHT not LEFT
 
 
 
 
 
 
 
 
 
186
  """
187
 
188
+ # If we have an assistant response, fill in position 0 of last turn
189
  if assistant_response.strip() and len(chat_history) > 0:
190
+ _, last_user_msg = chat_history[-1]
191
+ chat_history = chat_history[:-1] + [(assistant_response.strip(), last_user_msg)]
192
+
193
+ # Build messages for UserLM - user msgs are now in position 1
194
+ messages = []
195
+ if system_prompt.strip():
196
+ messages.append({"role": "system", "content": system_prompt.strip()})
197
 
198
+ for asst_msg, user_msg in chat_history:
199
+ if user_msg:
200
+ messages.append({"role": "user", "content": user_msg})
201
+ if asst_msg:
202
+ messages.append({"role": "assistant", "content": asst_msg})
203
 
204
  # Generate next user message
205
  try:
206
  user_msg = generate_user_message(
207
  messages,
208
+ [(u, a) for a, u in chat_history], # Swap for repetition check
209
  system_prompt,
210
  max_new_tokens=max_new_tokens,
211
  temperature=temperature,
 
214
  except Exception as e:
215
  user_msg = f"(Generation error: {e})"
216
 
217
+ # Add: (empty_assistant, new_user_msg)
218
+ new_history = chat_history + [("", user_msg)]
 
 
 
 
219
 
220
+ return "", new_history, "Generate Next User Message"
221
 
222
 
223
  def clear_conversation():
 
253
  chatbot = gr.Chatbot(
254
  height=420,
255
  label="Conversation",
256
+ type="messages", # Changed from tuples to have more control
257
+ # Will manually format messages with role attribute
258
  )
259
 
260
  with gr.Row():