ajndkr commited on
Commit
7ebae40
1 Parent(s): d9ebe36

update application

Browse files
Files changed (2) hide show
  1. app.py +120 -4
  2. blocks.py +104 -0
app.py CHANGED
@@ -1,7 +1,123 @@
 
 
1
  import gradio as gr
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2
 
3
- def greet(name):
4
- return "Hello " + name + "!!"
5
 
6
- iface = gr.Interface(fn=greet, inputs="text", outputs="text")
7
- iface.launch()
 
 
1
+ import os
2
+
3
  import gradio as gr
4
+ from boilerplate_x.generator import ProjectGenerator
5
+
6
+ from blocks import customisation_block, github_repo_block
7
+
8
+ INTRO_MD = """
9
+ <center>
10
+
11
+ # Boilerplate-X
12
+
13
+ Create project boilerplate for any programming language in minutes, with just an idea. Powered by Langchain and chatGPT API.
14
+
15
+ </center>
16
+ """
17
+
18
+ OUTPUT_PATH = "outputs/"
19
+ OPT_MAP = {True: "yes", False: "no"}
20
+
21
+
22
+ def generate_boilerplate(
23
+ api_key: str,
24
+ prompt: str,
25
+ unit_tests: bool,
26
+ dockerization: bool,
27
+ github_actions: bool,
28
+ pre_commit_hooks: bool,
29
+ gh_token: str,
30
+ gh_repo_name: str,
31
+ private: bool,
32
+ ):
33
+ """Generates project boilerplate."""
34
+ if not api_key:
35
+ gr.Error("Please enter your OpenAI API key!")
36
+
37
+ os.environ["OPENAI_API_KEY"] = api_key
38
+ output_path = OUTPUT_PATH
39
+
40
+ customisation_kwargs = {
41
+ "unit_tests": OPT_MAP[unit_tests],
42
+ "dockerization": OPT_MAP[dockerization],
43
+ "github_actions": OPT_MAP[github_actions],
44
+ "pre_commit_hooks": OPT_MAP[pre_commit_hooks],
45
+ }
46
+
47
+ if not gh_token:
48
+ gr.Error("Please enter your GitHub token!")
49
+ if not gh_repo_name:
50
+ gr.Error("Please enter your GitHub repository name!")
51
+
52
+ github_repo_creator_kwargs = {
53
+ "token": gh_token,
54
+ "repo_name": gh_repo_name,
55
+ "private": private,
56
+ "target_folder": output_path,
57
+ }
58
+
59
+ generator = ProjectGenerator(
60
+ prompt=prompt,
61
+ output_path=output_path,
62
+ verbose=False,
63
+ customisation_kwargs=customisation_kwargs,
64
+ github_repo_creator_kwargs=github_repo_creator_kwargs,
65
+ )
66
+
67
+ try:
68
+ generator.generate_template()
69
+ except Exception as e:
70
+ gr.Error(e)
71
+
72
+ return f"Your project is now available on {generator.github_repo_url} 🚀 !"
73
+
74
+
75
+ def build_app():
76
+ """Builds the Gradio UI."""
77
+ block = gr.Blocks(title="Boilerplate X")
78
+
79
+ with block:
80
+ gr.Markdown(INTRO_MD)
81
+ openai_api_key = gr.Textbox(
82
+ placeholder="Paste your OpenAI API key",
83
+ label="OpenAI API Key",
84
+ lines=1,
85
+ type="password",
86
+ )
87
+ prompt = gr.Textbox(
88
+ placeholder="Enter your project idea",
89
+ label="Prompt",
90
+ lines=1,
91
+ )
92
+ (
93
+ unit_tests,
94
+ dockerization,
95
+ github_actions,
96
+ pre_commit_hooks,
97
+ ) = customisation_block()
98
+ gh_token, gh_repo_name, private = github_repo_block()
99
+ generate = gr.Button("⚡ Generate Boilerplate ⚡")
100
+ output = gr.Markdown()
101
+
102
+ generate.click(
103
+ fn=generate_boilerplate,
104
+ inputs=[
105
+ openai_api_key,
106
+ prompt,
107
+ unit_tests,
108
+ dockerization,
109
+ github_actions,
110
+ pre_commit_hooks,
111
+ gh_token,
112
+ gh_repo_name,
113
+ private,
114
+ ],
115
+ outputs=[output],
116
+ )
117
+
118
+ return block
119
 
 
 
120
 
121
+ if __name__ == "__main__":
122
+ app = build_app()
123
+ app.queue().launch()
blocks.py ADDED
@@ -0,0 +1,104 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+
3
+
4
+ def select_custom_options(customisation):
5
+ """Selects the custom options."""
6
+ if customisation:
7
+ return (
8
+ gr.update(visible=True),
9
+ gr.update(visible=True),
10
+ gr.update(visible=True),
11
+ gr.update(visible=True),
12
+ )
13
+ return (
14
+ gr.update(visible=False),
15
+ gr.update(visible=False),
16
+ gr.update(visible=False),
17
+ gr.update(visible=False),
18
+ )
19
+
20
+
21
+ def select_github_repo_options(github_repo):
22
+ """Selects the Github repo options."""
23
+ if github_repo:
24
+ return (
25
+ gr.update(visible=True),
26
+ gr.update(visible=True),
27
+ gr.update(visible=True),
28
+ )
29
+ return (
30
+ gr.update(visible=False),
31
+ gr.update(visible=False),
32
+ gr.update(visible=False),
33
+ )
34
+
35
+
36
+ def customisation_block() -> dict:
37
+ with gr.Column():
38
+ enable_customisation = gr.Checkbox(
39
+ label="Enable customisation (Click to reveal options)",
40
+ default=False,
41
+ interactive=True,
42
+ )
43
+ with gr.Row():
44
+ unit_tests = gr.Checkbox(
45
+ label="Add Unit tests",
46
+ default=False,
47
+ interactive=True,
48
+ visible=False,
49
+ )
50
+ dockerization = gr.Checkbox(
51
+ label="Add Docker support",
52
+ default=False,
53
+ interactive=True,
54
+ visible=False,
55
+ )
56
+ github_actions = gr.Checkbox(
57
+ label="Add Github Actions support",
58
+ default=False,
59
+ interactive=True,
60
+ visible=False,
61
+ )
62
+ pre_commit_hooks = gr.Checkbox(
63
+ label="Add Pre-commit hooks",
64
+ default=False,
65
+ interactive=True,
66
+ visible=False,
67
+ )
68
+
69
+ enable_customisation.change(
70
+ fn=select_custom_options,
71
+ inputs=enable_customisation,
72
+ outputs=[unit_tests, dockerization, github_actions, pre_commit_hooks],
73
+ )
74
+
75
+ return (
76
+ unit_tests,
77
+ dockerization,
78
+ github_actions,
79
+ pre_commit_hooks,
80
+ )
81
+
82
+
83
+ def github_repo_block() -> dict:
84
+ with gr.Column():
85
+ with gr.Row():
86
+ gh_token = gr.Textbox(
87
+ placeholder="Paste your Github Personal Access Token (Required scopes: repo, workflow)",
88
+ label="Github Personal Access Token",
89
+ lines=1,
90
+ type="password",
91
+ )
92
+ gh_repo_name = gr.Textbox(
93
+ placeholder="Enter Github repo name",
94
+ label="Github repo name",
95
+ lines=1,
96
+ )
97
+
98
+ private = gr.Checkbox(
99
+ label="Make repo private?",
100
+ default=False,
101
+ interactive=True,
102
+ )
103
+
104
+ return gh_token, gh_repo_name, private