Self-paced

Explore our extensive collection of courses designed to help you master various subjects and skills. Whether you're a beginner or an advanced learner, there's something here for everyone.

Bootcamp

Learn live

Join us for our free workshops, webinars, and other events to learn more about our programs and get started on your journey to becoming a developer.

Upcoming live events

Learning library

For all the self-taught geeks out there, here is our content library with most of the learning materials we have produced throughout the years.

It makes sense to start learning by reading and watching videos about fundamentals and how things work.

Search from all Lessons


LoginGet Started

Register to 4Geeks

← Back to Projects

Code an Excuse Generator Using AI

Difficulty

  • easy

Average duration

4 hrs

Technologies

Difficulty

  • easy

Average duration

4 hrs

🌱 How to start this project?
πŸ“ Instructions

🌱 How to start this project?

Do not clone this repository because we are going to be using a different template.

We recommend opening the flask template or the vanilla.js template using a provisioning tool like Codespaces (recommended) or Gitpod. Alternatively, you can clone the github repository on your local computer using the git clone command.

These are the repositories you need to open or clone:

1🐍 For Python: 2https://github.com/4GeeksAcademy/flask-rest-hello 3 4πŸ‘©πŸ½β€πŸ’» For Javascript: 5https://github.com/4GeeksAcademy/vanillajs-hello

⚠ You will need to have Node.js or Python 3.7+ installed if you do it locally, but all of that is already installed on Codespaces or Gitpod.

πŸ“ Instructions

Step 1: Set Up the Project

Step 2: Get Access to ChatGPT's API

Step 3: Create an Input Form or use a prompt if building the project on the terminal

Step 4: Connect to ChatGPT's API

Example for Python:

1import requests 2 3@app.route('/generate_excuse', methods=['POST']) 4def generate_excuse(): 5 excuse_for = request.form['excuse_for'] 6 prompt = f"Give me an excuse for {excuse_for}:" 7 8 response = requests.post('https://api.openai.com/v1/engines/davinci-codex/completions', 9 headers={'Authorization': f'Bearer {api_key}'}, 10 json={'prompt': prompt, 'max_tokens': 50}) 11 12 excuse = response.json()['choices'][0]['text'] 13 return render_template('result.html', excuse=excuse)

Example for JavaScript:

1document.getElementById('excuseForm').addEventListener('submit', async (event) => { 2 event.preventDefault(); 3 const excuseFor = document.getElementById('excuseFor').value; 4 const prompt = `Give me an excuse for ${excuseFor}:`; 5 6 const response = await fetch('https://api.openai.com/v1/engines/davinci-codex/completions', { 7 method: 'POST', 8 headers: { 9 'Authorization': `Bearer ${apiKey}`, 10 'Content-Type': 'application/json' 11 }, 12 body: JSON.stringify({ prompt: prompt, max_tokens: 50 }) 13 }); 14 15 const data = await response.json(); 16 const excuse = data.choices[0].text; 17 // Display the excuse to the user 18});

Step 5: Display the Generated Excuse

Example for JavaScript:

1document.getElementById('excuseForm').addEventListener('submit', async (event) => { 2 event.preventDefault(); 3 const excuseFor = document.getElementById('excuseFor').value; 4 const prompt = `Give me an excuse for ${excuseFor}:`; 5 6 const response = await fetch('https://api.openai.com/v1/engines/davinci-codex/completions', { 7 method: 'POST', 8 headers: { 9 'Authorization': `Bearer ${apiKey}`, 10 'Content-Type': 'application/json' 11 }, 12 body: JSON.stringify({ prompt: prompt, max_tokens: 50 }) 13 }); 14 15 const data = await response.json(); 16 const excuse = data.choices[0].text; 17 document.getElementById('result').innerText = `Your excuse is: ${excuse}`; 18});

Bonus Section

Additional Features to Practice and Improve the Project

  1. Styling: Add CSS to style your web interface, making it more user-friendly and visually appealing.
  2. Customization: Allow users to specify different types of excuses (e.g., work, school, family).
  3. History: Store generated excuses in a database or local storage so users can view previously generated excuses.
  4. API Error Handling: Add proper error handling for API requests to improve the user experience.

Feel free to explore and add more features to make your Excuse Generator even more robust and user-friendly!

Signup and get access to similar projects

We will use it to give you access to your account.
Already have an account? Login here.

By signing up, you agree to the Terms and conditions and Privacy policy.

Difficulty

  • easy

Average duration

4 hrs

Difficulty

  • easy

Average duration

4 hrs

Difficulty

  • easy

Average duration

4 hrs

Difficulty

  • easy

Average duration

4 hrs

Signup and get access to similar projects

We will use it to give you access to your account.
Already have an account? Login here.

By signing up, you agree to the Terms and conditions and Privacy policy.

Difficulty

  • easy

Average duration

4 hrs

Difficulty

  • easy

Average duration

4 hrs

Supplementary Material

In order to prepare better for completing this exercises, we suggest the following materials

Lesson

Artificial Intelligence in Simple Terms

Exercise

Learn Python Interactively (beginner)

Exercise

Javascript Beginner Tutorial (interactive)

Project

Code an Excuse Generator in JavaScript

Project

The Excuse Generator CLI (python)