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.
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});
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});
Feel free to explore and add more features to make your Excuse Generator even more robust and user-friendly!
In order to prepare better for completing this exercises, we suggest the following materials