Views
No views yet
1<libs>pytorch,wandb</libs>
2<planning>PLANNING AS MARKDOWN FORMAT</planning>
3<requirements>>CONTENT FOR THE REQS FILE HERE</requirements>
4<output><file1>src/dataset.py<content>YOUR PYTHON CODE HERE</content></file1>
5<file2>src/model.py<content>YOUR PYTHON CODE HERE</content></file2>
6<bashfile>run.sh<content>python3 src/model.py</content></bashfile></output>
71Repository generated at: ./output_dir/demo2
2demo2/
3 run.sh
4 src/
5 visualize_timeseries.py1import torch
2from transformers import AutoModelForCausalLM, AutoTokenizer
3import fire
4from pathlib import Path
5import os
6import re
7
8def generate_repo_from_string(input_str: str, output_dir: str) -> None:
9 """
10 Parse <output> tags in the input string and write files (and bashfiles) to the specified output directory.
11
12 - Searches for <output>...</output> section.
13 - Within that, finds all <fileX> or <bashfile> tags:
14 <file1>path/to/file.ext<content>...file content...</content></file1>
15 <bashfile>script.sh<content>...script content...</content></bashfile>
16
17 Args:
18 input_str: The full string containing <output> markup.
19 output_dir: Directory where files will be created. Existing files will be overwritten.
20 """
21 # Extract the content inside <output>...</output>
22 out_match = re.search(r"<output>(.*?)</output>", input_str, re.DOTALL)
23 if not out_match:
24 raise ValueError("No <output> section found in input.")
25 output_section = out_match.group(1)
26
27 # Regex to find file tags: file1, file2, file3, ... and bashfile
28 pattern = re.compile(
29 r"<(file\d+|bashfile)>([^<]+?)<content>(.*?)</content></\1>",
30 re.DOTALL
31 )
32
33 for tag, filename, content in pattern.findall(output_section):
34 # Determine full path
35 file_path = os.path.join(output_dir, filename.strip())
36 # Ensure parent directory exists
37 parent = os.path.dirname(file_path)
38 if parent:
39 os.makedirs(parent, exist_ok=True)
40 # Write content to file
41 with open(file_path, 'w', encoding='utf-8') as f:
42 # Strip only one leading newline if present
43 f.write(content.lstrip('\n'))
44
45 print(f"Repository generated at: {output_dir}")
46
47
48def main(model_path:str="./models_dir/repo_coder_v1",
49 prompt:str="Generate a small python repo for matplotlib to visualize timeseries data to read from timeseries.csv file using polars."
50 ,output_path="./output_dir/demo2"):
51 input_prompt = "###Instruction: {prompt}".format(prompt=prompt)
52
53 def load_model(model_path):
54 """
55 Load the model and tokenizer from the specified path.
56 """
57 tokenizer = AutoTokenizer.from_pretrained(model_path)
58 model = AutoModelForCausalLM.from_pretrained(model_path, torch_dtype="auto").to("cuda:0")
59 model.eval()
60 return model, tokenizer
61
62
63 model, tokenizer = load_model(model_path)
64 print(f"Loaded model from {model_path}.")
65
66 input = tokenizer(input_prompt, return_tensors="pt").to(model.device)
67 with torch.no_grad():
68 output = model.generate(**input, max_length=1024, do_sample=True, temperature=0.9, top_p=0.95, top_k=50)
69 generated_code_repo = tokenizer.decode(output[0], skip_special_tokens=True)
70 print(f"Generated code repo: {generated_code_repo}")
71 Path(output_path).mkdir(parents=True, exist_ok=True)
72 generate_repo_from_string(generated_code_repo, output_path)
73
74 def list_files(startpath):
75 for root, dirs, files in os.walk(startpath):
76 level = root.replace(startpath, '').count(os.sep)
77 indent = ' ' * 4 * (level)
78 print('{}{}/'.format(indent, os.path.basename(root)))
79 subindent = ' ' * 4 * (level + 1)
80 for f in files:
81 print('{}{}'.format(subindent, f))
82 list_files(output_path)
83
84
85if __name__ == "__main__":
86 fire.Fire(main)
87