boilerplate-x / app.py
ajndkr's picture
fix exception handling
cbe25cd verified
raw
history blame
3.45 kB
import logging
import os
import tempfile
import time
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)
raise gr.Error(e)
# wait for GitHub repo to be created
time.sleep(1)
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()