boilerplate-x / app.py
ajndkr's picture
minor bug fix
24930d9 verified
raw
history blame
3.05 kB
import os
import gradio as gr
from boilerplate_x.generator import ProjectGenerator
from blocks import customisation_block, github_repo_block
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>
"""
OUTPUT_PATH = "outputs/"
OPT_MAP = {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
output_path = OUTPUT_PATH
customisation_kwargs = {
"unit_tests": OPT_MAP[unit_tests],
"dockerization": OPT_MAP[dockerization],
"github_actions": OPT_MAP[github_actions],
"pre_commit_hooks": OPT_MAP[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:
generator.generate_template()
except Exception as 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()