Views
No views yet
1graph TB;
2 A[Start] --> B[Read Input Source];
3 B --> C{Input Source Type};
4 C -->|File Path| D[Load File Content];
5 C -->|String| E[Convert String to List Item];
6 D --> F[Create Input Data List];
7 E --> F;
8
9 F --> G[Initialize Generator Object];
10 G --> H[Set Temperature Range];
11 H --> I[Loop Over Each Prompt];
12
13 subgraph Generate Response For Each Prompt
14 direction TB;
15
16 I --> J[Get Current Prompt];
17 J --> K[Call OpenAI API];
18 K --> L{Response Unique?};
19 L --> |No| M[Increase Temperature];
20 M --> N[Retry With New Temperature];
21 L --> |Yes| O[Convert to Image];
22 O --> P[Add Entry to All Entries];
23 N --> I;
24
25 end;
26
27 I --> Q[All Prompts Processed];
28 Q --> R[Write Output to File];
29 R --> S[End];
30
31 style J fill:#ddd,stroke:#777;
32 style K fill:#ccc,stroke:#777;
33 style L fill:#eee,stroke:#777;
34 style M fill:#ff9,stroke:#777;
35 style N fill:#f99,stroke:#777;
36 style O fill:#aaf,stroke:#777;
37 style P fill:#fff,stroke:#777;def convert_to_image(self, mermaid_code, entry_number, output_number):
clean_code = self._remove_mermaid_block_markers(mermaid_code)
output_filename = f"entry_{entry_number}_{output_number}.png"
output_path = os.path.join(self._entries_dir, output_filename)
self._generate_image_from_code(clean_code, output_path)
return output_path
def _remove_mermaid_block_markers(self, code):
code_lines = code.strip().splitlines()
if code_lines[0].startswith("```mermaid") and code_lines[-1] == "```":
return "\n".join(code_lines[1:-1]).strip()
return code
def _generate_image_from_code(self, mermaid_code, output_path):
with tempfile.NamedTemporaryFile(delete=False, mode='w', suffix='.mmd') as temp_file:
temp_file.write(mermaid_code)
input_path = temp_file.name
result = subprocess.run(["mmdc", "-i", input_path, "-o", output_path, "-t", self._theme, "-b", self._background], shell=True, check=False)
os.remove(input_path)
if result.returncode != 0:
raise ValueError("Mermaid diagram generation failed.")prompt_template = """
Below is an instruction that describes a task, paired with an input that provides further context. Write a response that appropriately completes the request.
### Instruction:
Create the mermaid diagram for the following input:
### Input:
{input}
### Response:
```mermaid
""".format(input=prompt)
url = "http://127.0.0.1:5000/v1/completions"
headers = {"Content-Type": "application/json"}
dataset_entries = []
for output_number, temp in enumerate(base_temperatures, start=1):
while True:
data = {
"prompt": prompt_template,
"max_tokens": 4096,
"temperature": temp,
"top_p": 1.0,
"seed": -1,
"top_k": 4,
"repetition_penalty": 1.0,
"guidance_scale": 1.0,
"typical_p": 1.0,
"stream": stream,
}
response = requests.post(url, headers=headers, json=data, verify=False)
response_text = response.json()['choices'][0]['text'].strip()
if response_text.endswith("```"): # Check if response ends with ```
response_text = response_text[:-3].strip() # Remove ``` from the end
if response_text not in unique_outputs:
try:
image_path = generator.convert_to_image(response_text, entry_number, output_number)
print(f"Mermaid diagram generated at: {image_path}")
unique_outputs.add(response_text)
break
except ValueError as e:
print(f"Validation failed, retrying... Error: {e}")
else:
temp += 0.1 # Adjust temperature if output is not unique
dataset_entry = {
"input": prompt,
"output": f"```mermaid\n{response_text}\n```",
"temperature": temp
}
dataset_entries.append(dataset_entry)
return dataset_entriesfor entry_number, entry in enumerate(input_data, start=1):
prompt = entry.get("input", "")
if prompt:
entries = generate_response(prompt, base_temperatures, stream, generator, entry_number, unique_outputs)
all_entries.extend(entries) # Extend the list with new entries
return all_entriesall_entries = generate_unique_responses(input_data, base_temperatures, stream, generator)
# Write all entries to the JSON file at once
with open(output_file, "w") as f:
json.dump(all_entries, f, indent=4) # Dump the entire list of entries into the filemain(args.input_source, args.stream)1graph TB;
2
3
4