Spaces:
Running
Running
Download src/stata/explain.py from CGIAR/PERI-AI-dev: direct link, hf CLI and curl.
- Browser
- Download file 2.05 kB
-
https://huggingface.co/spaces/CGIAR/PERI-AI-dev/resolve/main/src/stata/explain.py
- Command line
-
hf download hf://spaces/CGIAR/PERI-AI-dev/src/stata/explain.py
-
curl -L -o explain.py https://huggingface.co/spaces/CGIAR/PERI-AI-dev/resolve/main/src/stata/explain.py
2.05 kB
| import sys | |
| from src.llm import generate | |
| from src.prompts import build_stata_prompt | |
| def linenums(options): | |
| i = options.find("lines:") | |
| if i != -1: | |
| lines = options[i+6:] | |
| if lines == "": | |
| lines = None | |
| else: | |
| lines = None | |
| return lines | |
| def linenums(options): | |
| i = options.find("lines:") | |
| if i != -1: | |
| lines = options[i+6:] | |
| if lines == "": | |
| lines = None | |
| else: | |
| lines = None | |
| return lines | |
| def read_file(dofile, lines=None): | |
| # print(f"Reading file: {dofile}") | |
| if dofile is not None and dofile != "": | |
| try: | |
| with open(dofile, "r") as f: | |
| file_lines = f.readlines() | |
| # print(file_lines) | |
| except Exception as e: | |
| print("Error reading do-file: " + str(e)) | |
| sys.exit(1) | |
| if lines is not None: | |
| if "-" in lines: | |
| lines = lines.split("-") | |
| start = int(lines[0]) | |
| end = int(lines[1]) | |
| file_lines = file_lines[start-1:end] | |
| file_lines = [f.strip('\n') for f in file_lines] # Stata is 1-indexed, adjust to 0-indexed | |
| return "\n".join(file_lines) | |
| else: | |
| file_lines = file_lines[int(lines)-1] # line number as written in Stata | |
| return file_lines | |
| else: | |
| return "" | |
| def explain_(subcmd, query, cfg, dofile, opts): | |
| dofx = read_file(dofile, opts['lines']) | |
| context = build_stata_prompt(query, subcmd, dofx, opts) | |
| return generate( | |
| """ | |
| You are an expert in the Stata statistical program. | |
| Your task is to answer questions about Stata code. Respond as succinctly and briefly as possible. | |
| Prefer to use bullets if appropriate. Do not lie. Do not make-up information. | |
| If you do not know the answer, say so. You are speaking to your peers. | |
| Your response must be professional yet comfortable in tone.""", | |
| context, | |
| cfg, | |
| max_tokens=1024 | |
| ) |