Canstralian commited on
Commit
61cf18c
·
verified ·
1 Parent(s): 3f2205c

Create setup_project.py

Browse files
Files changed (1) hide show
  1. setup_project.py +142 -0
setup_project.py ADDED
@@ -0,0 +1,142 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import json
3
+ import torch
4
+ import random
5
+
6
+ # Define the directory structure
7
+ project_root = 'project_root'
8
+ model_dir = os.path.join(project_root, 'model')
9
+ tokenizer_dir = os.path.join(model_dir, 'tokenizer')
10
+ scripts_dir = os.path.join(project_root, 'scripts')
11
+
12
+ # Create directories
13
+ os.makedirs(tokenizer_dir, exist_ok=True)
14
+ os.makedirs(scripts_dir, exist_ok=True)
15
+
16
+ # Step 2: Create config.json
17
+ config = {
18
+ "model_type": "my_model_type",
19
+ "input_size": 100,
20
+ "hidden_size": 64,
21
+ "output_size": 10,
22
+ "num_layers": 1,
23
+ "dropout": 0.2
24
+ }
25
+
26
+ with open(os.path.join(model_dir, 'config.json'), 'w') as f:
27
+ json.dump(config, f)
28
+
29
+ # Step 3: Create a sample pytorch_model.bin
30
+ class SampleModel(torch.nn.Module):
31
+ def __init__(self):
32
+ super(SampleModel, self).__init__()
33
+ self.linear = torch.nn.Linear(100, 10)
34
+
35
+ def forward(self, x):
36
+ return self.linear(x)
37
+
38
+ # Initialize and save the model weights
39
+ model = SampleModel()
40
+ torch.save(model.state_dict(), os.path.join(model_dir, 'pytorch_model.bin'))
41
+
42
+ # Step 4: Create vocab.txt for tokenizer
43
+ vocab = ['hello', 'world', 'my', 'model', 'tokenization', 'is', 'important']
44
+ vocab_file_path = os.path.join(tokenizer_dir, 'vocab.txt')
45
+ with open(vocab_file_path, 'w') as f:
46
+ for token in vocab:
47
+ f.write(f"{token}\n")
48
+
49
+ # Step 5: Create tokenizer.json
50
+ tokenizer_config = {
51
+ "vocab_size": len(vocab),
52
+ "do_lower_case": True,
53
+ "tokenizer_type": "MyTokenizer"
54
+ }
55
+ with open(os.path.join(tokenizer_dir, 'tokenizer.json'), 'w') as f:
56
+ json.dump(tokenizer_config, f)
57
+
58
+ # Step 6: Create train.py
59
+ train_script = """import torch
60
+ import torch.nn as nn
61
+ import torch.optim as optim
62
+
63
+ class SampleModel(nn.Module):
64
+ def __init__(self):
65
+ super(SampleModel, self).__init__()
66
+ self.linear = nn.Linear(100, 10)
67
+
68
+ def forward(self, x):
69
+ return self.linear(x)
70
+
71
+ def train():
72
+ model = SampleModel()
73
+ criterion = nn.CrossEntropyLoss()
74
+ optimizer = optim.Adam(model.parameters(), lr=0.001)
75
+
76
+ # Sample data
77
+ inputs = torch.randn(100, 100) # 100 samples
78
+ targets = torch.randint(0, 10, (100,)) # 100 random labels
79
+
80
+ # Training loop (simplified)
81
+ for epoch in range(5): # 5 epochs
82
+ optimizer.zero_grad()
83
+ outputs = model(inputs)
84
+ loss = criterion(outputs, targets)
85
+ loss.backward()
86
+ optimizer.step()
87
+ print(f"Epoch {epoch+1}, Loss: {loss.item():.4f}")
88
+
89
+ if __name__ == "__main__":
90
+ train()
91
+ """
92
+
93
+ with open(os.path.join(scripts_dir, 'train.py'), 'w') as f:
94
+ f.write(train_script)
95
+
96
+ # Step 7: Create inference.py
97
+ inference_script = """import torch
98
+ import torch.nn as nn
99
+
100
+ class SampleModel(nn.Module):
101
+ def __init__(self):
102
+ super(SampleModel, self).__init__()
103
+ self.linear = nn.Linear(100, 10)
104
+
105
+ def forward(self, x):
106
+ return self.linear(x)
107
+
108
+ def inference(input_data):
109
+ model = SampleModel()
110
+ model.load_state_dict(torch.load('model/pytorch_model.bin'))
111
+ model.eval()
112
+ with torch.no_grad():
113
+ output = model(input_data)
114
+ return output
115
+
116
+ if __name__ == "__main__":
117
+ # Sample inference
118
+ input_data = torch.randn(1, 100) # Single sample
119
+ output = inference(input_data)
120
+ print(output)
121
+ """
122
+
123
+ with open(os.path.join(scripts_dir, 'inference.py'), 'w') as f:
124
+ f.write(inference_script)
125
+
126
+ # Step 8: Create utils.py
127
+ utils_script = """def load_model(model_path):
128
+ import torch
129
+ model = SampleModel()
130
+ model.load_state_dict(torch.load(model_path))
131
+ model.eval()
132
+ return model
133
+
134
+ def preprocess_input(input_data):
135
+ # Add input preprocessing logic here
136
+ return input_data
137
+ """
138
+
139
+ with open(os.path.join(scripts_dir, 'utils.py'), 'w') as f:
140
+ f.write(utils_script)
141
+
142
+ print("Project structure created successfully!")