-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtree_to_gpt.py
executable file
·87 lines (73 loc) · 2.45 KB
/
tree_to_gpt.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
import os
from openai import OpenAI
# Load the OpenAI API key from environment variables
openai_api_key = os.getenv('OPENAI_API_KEY')
if not openai_api_key:
raise ValueError("OPENAI_API_KEY environment variable is not set.")
client = OpenAI(
api_key=os.environ.get("OPENAI_API_KEY")
)
# Define the input file and output file
input_file = "REPO_STRUCTURE.md"
output_file = "MERMAID_DIAGRAM.md"
# Read the repository structure from the input file
with open(input_file, 'r') as file:
repo_structure = file.read()
print(repo_structure)
# Define the prompt to send to the ChatGPT API
prompt = f"""
The following is the directory structure of a Git repository:
{repo_structure}
Please convert this directory structure into a Mermaid diagram using the 'graph TD' syntax.
- Group files logically based on their folder structure.
- Use nested nodes where appropriate.
- Apply cool styles such as different colors for folders and files, use subgraph for groups, and add meaningful labels.
- Make sure the Mermaid syntax is correct and ready to be used in a Markdown file.
- Example:
This input:
# Repository Structure
```markdown
graph-git-repo
├── .gitignore
├── README.md
├── REPO_STRUCTURE.md
├── base.sh
└── tree_to_gpt.py
```
should generate this output:
```mermaid
graph TB
%% Repository Structure
subgraph graph_git_repo
root[graph-git-repo]
root --> gitignore[.gitignore]
root --> README[README.md]
root --> REPO_STRUCTURE[REPO_STRUCTURE.md]
root --> base[base.sh]
root --> tree_to_gpt[tree_to_gpt.py]
end
style graph_git_repo fill:#e0f7fa,stroke:#333,stroke-width:2px;
```
Output only the Mermaid diagram, starting from the opening backticks with 'mermaid' specification up until including the closing backticks after the mermaid graph!
"""
response = client.chat.completions.create(
model="gpt-4o-mini",
messages=[
{
"role": "user",
"content": [
{"type": "text", "text": prompt},
],
}
],
)
# Extract the generated text from the response
gpt_output = response.choices[0].message.content.strip()
print(gpt_output)
# Write the response to the output file, wrapping it in code block formatting for Markdown
with open(output_file, 'w') as file:
file.write("# Mermaid Diagram\n\n")
# file.write("```mermaid\n")
file.write(gpt_output)
# file.write("\n```")
print(f"Mermaid diagram written to {output_file}")