Skip to content

About

AI-Based Code Generator using Google Gemini API and Streamlit

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

1 Commit

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 

Repository files navigation

AI-Based Code Generator — Setup Guide (Beginner Friendly)

This guide assumes you have never built a Python/AI project before. Follow every step in order.


Step 1: Install Python

  1. Go to https://www.python.org/downloads/
  2. Download Python 3.10 or higher
  3. During install, check the box "Add Python to PATH" (Windows) — this step is easy to miss and causes most beginner errors
  4. Verify install by opening a terminal (Command Prompt / PowerShell / Terminal) and typing:
    python --version
    
    You should see something like Python 3.11.4

Step 2: Get the project files

Put app.py, requirements.txt, and this README.md in one folder, e.g.:

ai-code-generator/
├── app.py
├── requirements.txt
└── README.md

Step 3: Open a terminal in that folder

  • Windows: open the folder, click the address bar, type cmd, press Enter
  • Mac/Linux: right-click the folder → "Open Terminal Here" (or cd into it manually)

Step 4: Create a virtual environment (recommended, keeps things clean)

python -m venv venv

Activate 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).


Step 5: Install the required libraries

pip install -r requirements.txt

This installs:

  • streamlit → for the web UI
  • openai → to talk to the AI model

Step 6: Get an OpenAI API key

  1. Go to https://platform.openai.com/signup and create an account
  2. Go to https://platform.openai.com/api-keys
  3. Click "Create new secret key" and copy it (starts with sk-...)
  4. 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.


Step 7: Run the app

In your terminal (with venv still active):

streamlit run app.py

Your browser will automatically open http://localhost:8501 showing your app.


Step 8: Use it

  1. Paste your API key into the sidebar (or set it as an environment variable — see below)
  2. Pick a language (Python, Java, C++, etc.)
  3. Type a task, e.g. "function to check if a string is a palindrome"
  4. Click Generate
  5. See your code appear with an explanation below it

Optional: Set your API key permanently (so you don't paste it every time)

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 ~/.bashrc or ~/.zshrc to make it permanent)


Free Alternative: Hugging Face (no payment needed)

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).


What to screenshot for your project report

  1. The sidebar with settings
  2. A generated Python example (simple, e.g. factorial or palindrome)
  3. A generated Java or C++ example (shows multi-language support)
  4. The History section with 2-3 past generations
  5. One "failure case" — a vague or tricky prompt that produces imperfect code (great for your "Limitations" section)

Common errors and fixes

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

Next steps / ways to extend this project (for extra marks)

  • 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-ace or st.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

About

AI-Based Code Generator using Google Gemini API and Streamlit

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages