Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -78,24 +78,45 @@ def enhance_for_gradio(prompt_text, api_key):
|
|
| 78 |
]
|
| 79 |
|
| 80 |
try:
|
| 81 |
-
result = pipe(messages, max_new_tokens=256, temperature=0.7, do_sample=True)
|
| 82 |
full_response = result[0]["generated_text"]
|
| 83 |
|
| 84 |
-
# Extract only the
|
| 85 |
if isinstance(full_response, str):
|
| 86 |
-
#
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 90 |
else:
|
| 91 |
-
# Fallback: try to
|
| 92 |
-
|
| 93 |
-
if
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
|
|
|
| 97 |
else:
|
| 98 |
-
enhanced_prompt =
|
| 99 |
else:
|
| 100 |
enhanced_prompt = str(full_response)
|
| 101 |
|
|
|
|
| 78 |
]
|
| 79 |
|
| 80 |
try:
|
| 81 |
+
result = pipe(messages, max_new_tokens=256, temperature=0.7, do_sample=True, pad_token_id=pipe.tokenizer.eos_token_id)
|
| 82 |
full_response = result[0]["generated_text"]
|
| 83 |
|
| 84 |
+
# Extract only the assistant's response
|
| 85 |
if isinstance(full_response, str):
|
| 86 |
+
# Split by the conversation structure and get the last assistant response
|
| 87 |
+
if "assistant" in full_response:
|
| 88 |
+
# Find the last occurrence of assistant role
|
| 89 |
+
assistant_parts = full_response.split('"role": "assistant"')
|
| 90 |
+
if len(assistant_parts) > 1:
|
| 91 |
+
last_part = assistant_parts[-1]
|
| 92 |
+
# Extract content between quotes
|
| 93 |
+
if '"content":' in last_part:
|
| 94 |
+
content_start = last_part.find('"content":') + len('"content":')
|
| 95 |
+
content_part = last_part[content_start:].strip()
|
| 96 |
+
# Find the actual content between quotes
|
| 97 |
+
if content_part.startswith('"') or content_part.startswith("'"):
|
| 98 |
+
quote_char = content_part[0]
|
| 99 |
+
end_quote = content_part.find(quote_char, 1)
|
| 100 |
+
if end_quote > 0:
|
| 101 |
+
enhanced_prompt = content_part[1:end_quote]
|
| 102 |
+
else:
|
| 103 |
+
enhanced_prompt = content_part[1:].split('}')[0].strip()
|
| 104 |
+
else:
|
| 105 |
+
enhanced_prompt = content_part.split('}')[0].strip(' "\'')
|
| 106 |
+
else:
|
| 107 |
+
enhanced_prompt = "Unable to extract enhanced prompt"
|
| 108 |
+
else:
|
| 109 |
+
enhanced_prompt = "No assistant response found"
|
| 110 |
else:
|
| 111 |
+
# Fallback: try to get content after user input
|
| 112 |
+
user_content = prompt_text
|
| 113 |
+
if user_content in full_response:
|
| 114 |
+
response_start = full_response.rfind(user_content) + len(user_content)
|
| 115 |
+
remaining = full_response[response_start:].strip()
|
| 116 |
+
# Clean up response
|
| 117 |
+
enhanced_prompt = remaining.replace('"', '').replace("'", "").replace("}", "").strip()[:500]
|
| 118 |
else:
|
| 119 |
+
enhanced_prompt = "Unable to parse response"
|
| 120 |
else:
|
| 121 |
enhanced_prompt = str(full_response)
|
| 122 |
|