-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.py
More file actions
191 lines (95 loc) · 4.23 KB
/
script.py
File metadata and controls
191 lines (95 loc) · 4.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
import os
import pandas as pd
from colorama import init, Fore, Back
# Initialize colorama
init(autoreset=True)
error = False
class CustomError(Exception):
def __init__(self, message):
self.message = message
def print_error(message):
print("\n" + Fore.RED + " " + message + "\n")
error = True
def print_success(message):
print("\n" + Fore.GREEN + " " + message + "\n")
def print_warning(message):
print("\n" + Fore.YELLOW + " " + message + "\n")
def print_info(message):
print("\n" + Fore.CYAN + " " + message + "\n")
def clear_screen():
print("\033[H\033[J")
def print_normal(message):
print("\n" + message + "\n")
def confirm_pause():
input("\nPress any key to continue...")
def help():
print_info("Usage: main.py [OPTION]")
print_normal("Options:")
print_normal(
" -d, --description Start the program in description mode")
print_normal(" -h, --help Show this help message")
print_info("Learn more about the program at: https://github.com/tensor35/Python-Prompt-Generator-Script/blob/master/README.md")
def main(description_mode: bool = False):
clear_screen()
# print_info mode of operation
if description_mode:
print_info("Generating Prompts in Description Mode")
else:
print_info("Generating Prompts in Normal Mode")
# Checking requirements
try:
with open("prompt.txt", "r", encoding="utf-8") as template:
template_data = template.read()
if template_data == "":
# throw error
raise CustomError(
"Template file is empty. Please add some text to it.")
with open("key-values.csv", "r", encoding="utf-8") as csv_values:
csv_data = csv_values.read()
if csv_data == "":
raise CustomError(
"CSV file is empty. Please add some data to it.")
except Exception as e:
print_error("Error:", e, "")
return
print_success("Checking Requirements")
# Checking for key-value pairs
print_info("Generating Prompts")
try:
df = pd.read_csv("key-values.csv")
[row_count, column_count] = df.shape
columns = df.columns.values
if columns[0] != "City" or columns[1] != "Title":
raise CustomError(
"Invalid CSV Headers. Please make sure the CSV file has the correct format.")
if column_count != 2:
raise CustomError(
"Invalid CSV columns count. Please make sure the CSV file has the correct format.")
print_info(f"{row_count} records found in CSV file. {row_count} prompts will be generated ")
confirm_pause()
# Generate prompts
if description_mode:
for index, row in df.iterrows():
prompt = template_data.replace(
"**/City/**", row["City"]).replace("**/Title/**", "")
if not os.path.exists("prompts"):
os.makedirs("prompts")
with open(f"prompts/{row['City']}_description.txt", "w", encoding="utf-8") as prompt_file:
prompt_file.write(prompt)
print_success(
f"{index + 1} out of {row_count} generated successfully.")
else:
for index, row in df.iterrows():
prompt = template_data.replace(
"**/City/**", row["City"]).replace("**/Title/**", row["Title"])
if not os.path.exists("prompts"):
os.makedirs("prompts")
with open(f"prompts/{row['City']}_{row['Title']}.txt", "w", encoding="utf-8") as prompt_file:
prompt_file.write(prompt)
print_success(
f"{index + 1} out of {row_count} generated successfully.")
except Exception as e:
print_error("Error: ", e, "")
return
print_success("Prompts Generated Successfully")
print_info("Prompts are saved in the prompts folder.")