-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSynth_cn2.py
137 lines (114 loc) · 5.52 KB
/
Synth_cn2.py
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
import openai
import json
import datetime
import os
from PyQt5 import QtWidgets, QtCore, QtGui
class OpenAIGenerator(QtWidgets.QWidget):
def __init__(self):
super().__init__()
self.initUI()
self.prompts = []
def initUI(self):
self.setWindowTitle("OpenAI Text Generator")
self.setGeometry(100, 100, 600, 400)
layout = QtWidgets.QVBoxLayout(self)
form_layout = QtWidgets.QFormLayout()
self.api_key = QtWidgets.QLineEdit(self)
self.api_base = QtWidgets.QLineEdit(self)
self.api_base.setText("http://127.0.0.1:5000/v1")
self.model = QtWidgets.QLineEdit(self)
self.temperature = QtWidgets.QDoubleSpinBox(self)
self.temperature.setRange(0.0, 1.0)
self.temperature.setValue(0.5)
self.temperature.setSingleStep(0.1)
self.max_tokens = QtWidgets.QSpinBox(self)
self.max_tokens.setRange(1, 4096)
self.max_tokens.setValue(100)
self.output_json = QtWidgets.QCheckBox(self)
form_layout.addRow("OpenAI API key:", self.api_key)
form_layout.addRow("API base address:", self.api_base)
form_layout.addRow("Model name (e.g. 'gpt-4'):", self.model)
form_layout.addRow("Temperature (0.0-1.0):", self.temperature)
form_layout.addRow("Maximum number of generated words:", self.max_tokens)
form_layout.addRow("Output JSON file:", self.output_json)
layout.addLayout(form_layout)
btn_load_prompts = QtWidgets.QPushButton("Select prompt file", self)
btn_generate = QtWidgets.QPushButton("Generate text", self)
layout.addWidget(btn_load_prompts)
layout.addWidget(btn_generate)
self.output_text = QtWidgets.QTextEdit(self)
self.output_text.setReadOnly(True)
layout.addWidget(self.output_text)
btn_load_prompts.clicked.connect(self.load_prompts_from_file)
btn_generate.clicked.connect(self.generate_data)
def load_prompts_from_file(self):
file_path, _ = QtWidgets.QFileDialog.getOpenFileName(
self, "Select prompt file", "", "Text Files (*.txt);;All Files (*)")
if file_path:
with open(file_path, 'r') as file:
self.prompts = [line.strip() for line in file.readlines() if line.strip()]
QtWidgets.QMessageBox.information(self, "Prompt file loading", f"{len(self.prompts)} prompts have been loaded")
def generate_data(self):
try:
openai.api_key = self.api_key.text()
openai.api_base = self.api_base.text()
prompts = self.prompts
model = self.model.text()
temperature = self.temperature.value()
max_tokens = self.max_tokens.value()
output_json = self.output_json.isChecked()
generated_texts = []
json_data = []
for prompt in prompts:
payload = {
"model": model,
"prompt": prompt,
"max_tokens": max_tokens,
"temperature": temperature,
"top_p": 1,
"stop": None,
"n": 1
}
response = openai.Completion.create(
model=model,
prompt=payload["prompt"],
max_tokens=payload["max_tokens"],
temperature=payload["temperature"],
top_p=payload["top_p"],
n=payload["n"]
)
generated_text = response.choices[0].text.strip()
generated_texts.append(generated_text)
if output_json:
json_data.append({"instruction": prompt, "input": prompt, "output": generated_text})
if output_json:
timestamp = datetime.datetime.now().strftime("%Y%m%d_%H%M%S")
json_file_name = f"generated_texts_{timestamp}.json"
with open(json_file_name, 'w', encoding='utf-8') as f:
json.dump(json_data, f, indent=4, ensure_ascii=False)
QtWidgets.QMessageBox.information(self, "JSON file saved", f"The generated JSON file has been saved to {json_file_name}")
if generated_texts:
output_file = self.create_output_file()
self.save_to_file(output_file, generated_texts)
self.output_text.append(f"The generated text has been saved to {output_file}\n")
self.output_text.append("\n".join(generated_texts) + "\n\n")
else:
QtWidgets.QMessageBox.information(self, "Generate results", "No text was generated")
except Exception as e:
QtWidgets.QMessageBox.critical(self, "Error", f"An error occurred while generating text: {e}")
def create_output_file(self):
timestamp = datetime.datetime.now().strftime("%Y%m%d_%H%M%S")
file_name = f"generated_texts_{timestamp}.txt"
return file_name
def save_to_file(self, file_path, texts):
try:
with open(file_path, 'w') as file:
for text in texts:
file.write(f"{text}\n")
except IOError as e:
QtWidgets.QMessageBox.critical(self, "Error", f"Error saving file: {e}")
if __name__ == "__main__":
app = QtWidgets.QApplication([])
window = OpenAIGenerator()
window.show()
app.exec_()