File size: 3,364 Bytes
b3e3a0c
7ebae40
b3e3a0c
7ebae40
2ba6c15
7ebae40
 
 
 
b3e3a0c
 
 
7ebae40
 
 
 
 
 
 
 
 
b3e3a0c
7ebae40
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b3e3a0c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7ebae40
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2ba6c15
 
7ebae40
 
 
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
import logging
import os
import tempfile

import gradio as gr
from boilerplate_x.generator import ProjectGenerator

from blocks import customisation_block, github_repo_block

logging.basicConfig(level=logging.INFO)
logger = logging.getLogger("gradio")

INTRO_MD = """
<center>

# Boilerplate-X

Create project boilerplate for any programming language in minutes, with just an idea. Powered by Langchain and chatGPT API.

</center>
"""
BOOL2STR = {True: "yes", False: "no"}


def generate_boilerplate(
    api_key: str,
    prompt: str,
    unit_tests: bool,
    dockerization: bool,
    github_actions: bool,
    pre_commit_hooks: bool,
    gh_token: str,
    gh_repo_name: str,
    private: bool,
):
    """Generates project boilerplate."""
    if not api_key:
        gr.Error("Please enter your OpenAI API key!")
    os.environ["OPENAI_API_KEY"] = api_key

    with tempfile.TemporaryDirectory() as output_path:
        customisation_kwargs = {
            "unit_tests": BOOL2STR[unit_tests],
            "dockerization": BOOL2STR[dockerization],
            "github_actions": BOOL2STR[github_actions],
            "pre_commit_hooks": BOOL2STR[pre_commit_hooks],
        }

        if not gh_token:
            gr.Error("Please enter your GitHub token!")
        if not gh_repo_name:
            gr.Error("Please enter your GitHub repository name!")

        github_repo_creator_kwargs = {
            "token": gh_token,
            "repo_name": gh_repo_name,
            "private": private,
            "target_folder": output_path,
        }

        generator = ProjectGenerator(
            prompt=prompt,
            output_path=output_path,
            verbose=True,
            customisation_kwargs=customisation_kwargs,
            github_repo_creator_kwargs=github_repo_creator_kwargs,
        )

        try:
            logger.info("Generating project boilerplate...")
            generator.generate_template()
        except Exception as e:
            logger.error(e)
            gr.Error(e)

        return f"Your project is now available on {generator.github_repo_url} 🚀 !"


def build_app():
    """Builds the Gradio UI."""
    block = gr.Blocks(title="Boilerplate X")

    with block:
        gr.Markdown(INTRO_MD)
        openai_api_key = gr.Textbox(
            placeholder="Paste your OpenAI API key",
            label="OpenAI API Key",
            lines=1,
            type="password",
        )
        prompt = gr.Textbox(
            placeholder="Enter your project idea",
            label="Prompt",
            lines=1,
        )
        (
            unit_tests,
            dockerization,
            github_actions,
            pre_commit_hooks,
        ) = customisation_block()
        gh_token, gh_repo_name, private = github_repo_block()
        generate = gr.Button("⚡ Generate Boilerplate ⚡")
        output = gr.Markdown()

        generate.click(
            fn=generate_boilerplate,
            inputs=[
                openai_api_key,
                prompt,
                unit_tests,
                dockerization,
                github_actions,
                pre_commit_hooks,
                gh_token,
                gh_repo_name,
                private,
            ],
            outputs=[output],
        )

    return block


if __name__ == "__main__":
    app = build_app()
    app.queue().launch()