Exploring AI's Capability to Generate Python Applications
Written on
Chapter 1: Introduction to AI-Generated Applications
The potential of artificial intelligence is continually evolving, making tasks that once seemed unattainable now feasible. Recently, AI has ventured into the realm of programming, particularly with tools like GitHub Copilot. This article explores whether AI can not only create short code snippets but can also generate complete applications based on broad instructions. The focus will be on the GPT-3 model. Be sure to read the concluding section for a broader analysis of the experiments conducted and my reflections on the results.
Section 1.1: Starting Simple - A To-Do Application
To begin, I opted to create a straightforward to-do application. The prompt I provided was:
"Generate the complete code with spaces for tabs for a Python application that allows adding items to a to-do list and checking them off."
The output was then copied into a file named main.py, which I executed using the command python3 main.py:
The application functioned correctly! While it lacked persistent storage and was quite basic, it achieved its purpose. I specified spaces for tabs in the prompt to ensure proper indentation in the generated code, although I still encountered issues with indentation in some instances. Below is the entire code:
Section 1.2: Increasing Complexity - A Streamlit Text Analyzer
Next, I aimed to challenge GPT-3 by creating a text analysis application using Streamlit, a framework in Python designed for rapid data science dashboard development. The prompt used was:
"Generate the complete code with spaces for tabs for a Streamlit application that analyzes and visualizes text data."
Here’s the resulting application:
This application allows users to paste any text and click "analyze," producing a word cloud along with two bar charts displaying the most frequent words and bigrams. The text inputted here was sourced from the Wikipedia entry for “London.”
Challenges Faced:
- The initial code contained several errors that needed correction.
- A function call was omitted.
These adjustments had to be made manually, along with ensuring that all necessary packages were installed. Despite the extra effort required to troubleshoot the code, the application was impressive, albeit not overly complex. The final, refined code can be executed by placing it into a file, for instance, main.py, followed by the installation of the required packages and running streamlit run main.py.
Chapter 2: Creating an API for Notes with FastAPI
For my next experiment, I aimed to create a note-taking API using FastAPI, a framework designed for building RESTful APIs, along with SQLite for persistent storage. The prompt employed was:
"Generate the complete code in Python for a FastAPI server with SQLite that manages notes."
The generated code was as follows:
I saved this code into a file named api.py and executed it using the command uvicorn api:app. Testing it confirmed that the API functioned correctly, allowing users to add and retrieve notes, although editing and deleting features were not included.
My Assessment
While the applications generated were visually appealing, my experiments with various prompts revealed inconsistencies in the results. The initial to-do application was straightforward to create, requiring only a few attempts. In contrast, the Streamlit application proved to be more challenging, often producing trivial outputs with minimal functionality. To achieve a satisfactory result, I needed to rerun the process multiple times and adjust my prompts accordingly. Additionally, various errors in the generated code necessitated manual fixes.
Some of the code produced was extensive, consisting of several hundred lines. However, I often found that the components did not cohesively fit together. Overall, GPT-3's ability to generate complete applications fell short of expectations. While it can produce usable code for simpler tasks, it struggles with more complex projects, leading to frustration when the generated code fails to work correctly. The applications created typically contained fewer lines of code than anticipated, a consequence of its limitations in generating accurate code for intricate applications.
By fine-tuning GPT-3 for specific coding tasks, I believe it can be more effective. In conclusion, while GPT-3 has potential, it tends to falter with larger code bases, often yielding subpar results.
Thanks for reading! If you're keen on exploring more articles about AI or Python, be sure to check out my curated reading lists below:
Python Reading List
In this collection, I've compiled my finest articles on Python. Whether you're just starting or have experience, you'll find valuable insights here.
AI Reading List
Explore my selection of articles focused on artificial intelligence for further learning and inspiration.
This first video, "How to Build a Practical AI App with Python, Redis, and OpenAI," delves into the process of creating an AI application using these tools. It provides a detailed walkthrough that complements the concepts discussed in this article.
The second video, "GPT-4 Unleashed - Builds Full Application in 10 Seconds," showcases the capabilities of GPT-4 in generating full applications rapidly. It serves as an exciting glimpse into the advancements in AI application development.