Spaces:
Running
on
Zero
Running
on
Zero
Commit
·
3c8d247
1
Parent(s):
649eda6
[HF Maintenance] Fix the json errors (#2)
Browse files- [HF Maintenance] Fix the json errors (4eb73651d1388d0e2b98c20359203a134f2a4adc)
- prompt_augment.py +20 -1
prompt_augment.py
CHANGED
|
@@ -212,7 +212,26 @@ Please strictly follow the rewriting rules below:
|
|
| 212 |
time.sleep(1)
|
| 213 |
|
| 214 |
# polished_prompt = json.loads(completion.choices[0].message.content)['Rewritten']
|
| 215 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 216 |
|
| 217 |
return polished_prompt # + magic_prompt
|
| 218 |
|
|
|
|
| 212 |
time.sleep(1)
|
| 213 |
|
| 214 |
# polished_prompt = json.loads(completion.choices[0].message.content)['Rewritten']
|
| 215 |
+
try:
|
| 216 |
+
raw_text = response.output.choices[0].message.content[0]['text']
|
| 217 |
+
print(f"Raw API response: {repr(raw_text[:500])}")
|
| 218 |
+
|
| 219 |
+
# The thinking model may wrap JSON in markdown or include <think> tags
|
| 220 |
+
# Try to extract JSON from the response
|
| 221 |
+
json_match = re.search(r'\{[^{}]*"Rewritten"\s*:\s*"[^"]*"[^{}]*\}', raw_text, re.DOTALL)
|
| 222 |
+
if json_match:
|
| 223 |
+
polished_prompt = json.loads(json_match.group())['Rewritten']
|
| 224 |
+
else:
|
| 225 |
+
# More flexible: find any JSON block (possibly in ```json ... ```)
|
| 226 |
+
json_block = re.search(r'```json\s*(\{.*?\})\s*```', raw_text, re.DOTALL)
|
| 227 |
+
if json_block:
|
| 228 |
+
polished_prompt = json.loads(json_block.group(1))['Rewritten']
|
| 229 |
+
else:
|
| 230 |
+
print("Could not extract JSON from response, using original prompt")
|
| 231 |
+
polished_prompt = original_prompt
|
| 232 |
+
except Exception as e:
|
| 233 |
+
print(f"Prompt rewrite failed: {e}, using original prompt")
|
| 234 |
+
polished_prompt = original_prompt
|
| 235 |
|
| 236 |
return polished_prompt # + magic_prompt
|
| 237 |
|