TextToGame / app.py
Jumper-Clown's picture
add asyncio hint when not using code example
4b4be2a
import gradio as gr
import ai
import github_manager
import webbrowser
def main():
pre_prompt = """using pygame and no sprites, create: """
initial_prompt = """Conway's game of life, but the user controls one of the squares. If the square dies, the game is over."""
add_example = False
post_prompt = """
run the game with asyncio.run(main())
"""
if add_example:
example_code_file_path = "resources/main.py"
with open(example_code_file_path, "r") as file:
example_code = file.read()
post_prompt = example_code
with gr.Blocks() as app:
inp = gr.Textbox(label="Input Prompt",
value=initial_prompt)
with gr.Row():
model_choice = gr.Dropdown(label="Select Model",
choices=[m for m in ai.models],
value=ai.models[0],
type="index",
interactive=True)
no_tokens = gr.Number(label="Tokens", value=1000)
btn = gr.Button(value="Generate Game")
with gr.Column(visible=False) as save_col:
save_button = gr.Button("Build The Game")
with gr.Column(visible=False) as play_col:
play_button = gr.Button("Play The Game")
out = gr.Code(label="Generated Code")
def generate(prompt, model_index, max_tokens):
final_prompt = f"""{pre_prompt} {prompt}. {post_prompt}"""
generated_code = ai.generate_code(prompt=final_prompt,
model_index=model_index,
max_tokens=max_tokens)
return {
out: generated_code,
save_col: gr.Column(visible=True),
}
def save(text):
github_manager.update_repo(text)
return {
play_col: gr.Column(visible=True)
}
def play():
webbrowser.open(github_manager.game_link())
btn.click(generate, inputs=[inp, model_choice, no_tokens], outputs=[out, save_col], queue=True)
save_button.click(save, inputs=[out], outputs=[play_col])
play_button.click(play, inputs=[], outputs=[])
app.launch(share=True)
if __name__ == "__main__":
main()