Download augmentoolkit/generation_functions/extract_question_answer.py from Leon4gr45/agent-0: direct link, hf CLI and curl.
- Browser
- Download file 935 Bytes
-
https://huggingface.co/spaces/Leon4gr45/agent-0/resolve/main/augmentoolkit/generation_functions/extract_question_answer.py
- Command line
-
hf download hf://spaces/Leon4gr45/agent-0/augmentoolkit/generation_functions/extract_question_answer.py
-
curl -L -o extract_question_answer.py https://huggingface.co/spaces/Leon4gr45/agent-0/resolve/main/augmentoolkit/generation_functions/extract_question_answer.py
935 Bytes
| import re | |
| def extract_question_answer(response): | |
| # Modified pattern to handle multi-line answers | |
| pattern = ( | |
| r"### Reworded Question and Answer:\nQuestion:(.*?)\nAnswer:(.*?)(?=\n###|\Z)" | |
| ) | |
| # Search with DOTALL flag to match across newlines | |
| match = re.search(pattern, response, re.DOTALL) | |
| if match: | |
| question = match.group(1).strip() | |
| answer = match.group(2).strip() | |
| return question, answer | |
| else: | |
| # Try again after replacing escaped characters | |
| response = response.replace("\\n", "\n") | |
| response = response.replace('\\"', '"') | |
| match = re.search(pattern, response, re.DOTALL) | |
| if match: | |
| question = match.group(1).strip() | |
| answer = match.group(2).strip() | |
| return question, answer | |
| else: | |
| print("Returned none, failed to match") | |
| print(response) | |
| return None, None | |