This guide assumes you have never built a Python/AI project before. Follow every step in order.
- Go to https://www.python.org/downloads/
- Download Python 3.10 or higher
- During install, check the box "Add Python to PATH" (Windows) — this step is easy to miss and causes most beginner errors
- Verify install by opening a terminal (Command Prompt / PowerShell / Terminal) and typing:
You should see something like
python --versionPython 3.11.4
Put app.py, requirements.txt, and this README.md in one folder, e.g.:
ai-code-generator/
├── app.py
├── requirements.txt
└── README.md
- Windows: open the folder, click the address bar, type
cmd, press Enter - Mac/Linux: right-click the folder → "Open Terminal Here" (or
cdinto it manually)
python -m venv venvActivate it:
- Windows:
venv\Scripts\activate - Mac/Linux:
source venv/bin/activate
You'll know it worked because your terminal line will now start with (venv).
pip install -r requirements.txtThis installs:
streamlit→ for the web UIopenai→ to talk to the AI model
- Go to https://platform.openai.com/signup and create an account
- Go to https://platform.openai.com/api-keys
- Click "Create new secret key" and copy it (starts with
sk-...) - Important: OpenAI requires a small prepaid balance (as little as $5) to use the API. Add billing at https://platform.openai.com/account/billing
💡 Don't want to pay? See the "Free Alternative" section at the bottom — you can use Hugging Face's free models instead.
In your terminal (with venv still active):
streamlit run app.pyYour browser will automatically open http://localhost:8501 showing your app.
- Paste your API key into the sidebar (or set it as an environment variable — see below)
- Pick a language (Python, Java, C++, etc.)
- Type a task, e.g. "function to check if a string is a palindrome"
- Click Generate
- See your code appear with an explanation below it
Instead of typing it in the sidebar each time, set it as an environment variable:
-
Windows (PowerShell):
setx OPENAI_API_KEY "sk-your-key-here"(then close and reopen your terminal)
-
Mac/Linux:
export OPENAI_API_KEY="sk-your-key-here"(add this line to your
~/.bashrcor~/.zshrcto make it permanent)
If you don't want to pay OpenAI, you can swap the generate_code() function
to call a free Hugging Face model instead, e.g. bigcode/starcoder2-15b or
Salesforce/codegen-350M-mono, using their free Inference API:
import requests
def generate_code_hf(prompt, hf_token):
API_URL = "https://api-inference.huggingface.co/models/bigcode/starcoder2-15b"
headers = {"Authorization": f"Bearer {hf_token}"}
response = requests.post(API_URL, headers=headers, json={"inputs": prompt})
return response.json()[0]["generated_text"]Get a free Hugging Face token at https://huggingface.co/settings/tokens (free tier has rate limits, but is fine for a student project demo).
- The sidebar with settings
- A generated Python example (simple, e.g. factorial or palindrome)
- A generated Java or C++ example (shows multi-language support)
- The History section with 2-3 past generations
- One "failure case" — a vague or tricky prompt that produces imperfect code (great for your "Limitations" section)
| Error | Fix |
|---|---|
streamlit: command not found |
Your venv isn't activated — repeat Step 4 |
AuthenticationError |
Your API key is wrong or has no billing set up |
ModuleNotFoundError: No module named 'openai' |
Run pip install -r requirements.txt again |
| App opens but blank | Refresh the browser tab, or check terminal for red error text |
- Add a "Fix my code" mode: paste broken code, get a corrected version
- Add a "Convert language" mode: paste Python, get the same logic in Java
- Add syntax highlighting using
streamlit-aceorst.code(result, language=lang) - Deploy it for free on https://streamlit.io/cloud so you can share a live link
- Log all prompts/outputs to a CSV file to analyze accuracy for your report