Spaces:
Runtime error
Runtime error
Update app.py
Browse files
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 |
-
#
|
| 47 |
try:
|
| 48 |
-
#
|
| 49 |
-
|
| 50 |
-
#
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 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 |
-
|
|
|
|
| 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
|