benhaotang commited on
Commit
f058cc0
·
verified ·
1 Parent(s): 90b61ce

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +10 -15
app.py CHANGED
@@ -43,24 +43,19 @@ def generate_response(prompt, max_length=1024):
43
 
44
  outputs = pipe(messages, max_new_tokens=max_length)
45
 
46
- # Find assistant's response in the output
47
  try:
48
- # The output contains the full conversation history
49
- generated_text = outputs[0]["generated_text"]
50
- # Look for the last assistant message
51
- assistant_prefix = "{'role': 'assistant', 'content': '"
52
- assistant_start = generated_text.rfind(assistant_prefix)
53
- if assistant_start != -1:
54
- # Move past the prefix
55
- content_start = assistant_start + len(assistant_prefix)
56
- # Find the end of the content (before the closing quote and brace)
57
- content_end = generated_text.rfind("'}")
58
- if content_end != -1:
59
- return generated_text[content_start:content_end]
60
  except Exception as e:
61
  print(f"Error extracting response: {e}")
62
-
63
- # Fallback: return the raw generated text if extraction fails
 
64
  return outputs[0]["generated_text"]
65
 
66
  # Example with proper line breaks
 
43
 
44
  outputs = pipe(messages, max_new_tokens=max_length)
45
 
46
+ # Extract just the assistant's response
47
  try:
48
+ # Get the message list from the output
49
+ message_list = eval(outputs[0]["generated_text"]) # Safely convert string to list
50
+ # Get the last message (assistant's response)
51
+ assistant_message = message_list[-1]
52
+ if assistant_message["role"] == "assistant":
53
+ return assistant_message["content"]
 
 
 
 
 
 
54
  except Exception as e:
55
  print(f"Error extracting response: {e}")
56
+ # If extraction fails, return the raw output
57
+ return str(outputs[0]["generated_text"])
58
+
59
  return outputs[0]["generated_text"]
60
 
61
  # Example with proper line breaks