Skip to content

Forum in maintenance, we will back soon 🙂

Course: Build with ...
 
Notifications
Clear all

Course: Build with AI

12 Posts
4 Users
7 Reactions
586 Views
SSAdvisor
(@ssadvisor)
Posts: 1139
Noble Member
Topic starter
 

I want to give you both what ChatGPT gave me as a working example and what ChatGPT gave Hasan during his video. They are very different as I am sure they are for you. Comparing both I hope will help you understand the Python code and the uses of the API URI you construct.

First I will give you what Hasan shared:

# File: app.py

from flask import Flask, jsonify
import requests

app = Flask(__name__)

@app.route('/<query>', methods=['GET'])
def autocomplete(query):
    url = f'https://suggestqueries.google.com/complete/search?client=firefox&q={query}'
    response = requests.get(url)
    suggestions = response.json()[1]

    return jsonify(suggestions)

if __name__ == '__main__':
    app.run(debug=True)

To execute this

#For Windows
python -m venv venv
venv/bin/activate
pip install flask requests
flask run

#For Linux or MAC
python -m venv venv
source venv/bin/activate
pip install flask requests
flask run

Now in your browser you enter http://127.0.0.1:5000/KEYWORD

Replace KEYWORD with your search term

=====>8==================8<=====

Second is what ChatGPT gave to me but with the code corrected.

# File: app.py

from flask import Flask, request, jsonify
from googlesearch import search

app = Flask(__name__)

@app.route('/suggestions', methods=['GET'])
def get_suggestions():
    query = request.args.get('query')

    if not query:
        return jsonify({'error': 'Query parameter is required'}), 400

    suggestions = []
    try:
        # Use Google search to get auto-suggestions
        for suggestion in search(query, num_results=5):
            suggestions.append(suggestion)

        return jsonify({'suggestions': suggestions})
    except Exception as e:
        return jsonify({'error': f'Failed to fetch suggestions {e}'}), 500

if __name__ == '__main__':
    app.run(debug=True)

To execute this

#For Windows
python -m venv venv
venv/bin/activate
pip install flask googlesearch-python
flask run

#For Linux or MAC
python -m venv venv
source venv/bin/activate
pip install flask googlesearch-python
flask run

Now in your browser you would type https://127.0.0.1:5000/suggestions?query=KEYWORD

Replace the KEYWORD with your search term.

=====>8==================8<=====

As you can see, ChatGPT can give very different results for the same prompt. I did use the prompt exactly as Hasan did in the course video.

write the code of a flask Python API, it uses Google autosuggestions to generate keyword ideas and suggestions

One of the main points of Hasan's video was completely ignored by ChatGPT for me but not for Hasan and that was to use https://suggestqueries.google.com for the URL in the code.

Both methods for the URL of your created API are correct, I prefer the one that was in Hasan's code but you need to know both methods of obtaining a query string.

Regards,
Earnie Boyd, CEO
Seasoned Solutions Advisor LLC
Schedule 1-on-1 help
Join me on Slack

 
Posted : 10/15/2023 4:50 pm
(@husein)
Posts: 550
Member Moderator
 

Thanks for sharing this! Coding is an interesting thing aint it haha

 
Posted : 10/16/2023 10:49 am
SSAdvisor
(@ssadvisor)
Posts: 1139
Noble Member
Topic starter
 

@husein-aboul-hasan yes it is and I've been doing it for more than 3 decades.

Regards,
Earnie Boyd, CEO
Seasoned Solutions Advisor LLC
Schedule 1-on-1 help
Join me on Slack

 
Posted : 10/16/2023 2:06 pm
(@sai1)
Posts: 3
New Member
 

Hi @husein-aboul-hasan , I followed you're instructions but had no luck. I'm new to coding, and I'm getting an error. I followed all the instructions given in the video but had no luck. Can you please help me with the mistake I made?

This post was modified 1 year ago 2 times by D SAI KUMAR
 
Posted : 10/21/2023 4:56 pm
SSAdvisor
(@ssadvisor)
Posts: 1139
Noble Member
Topic starter
 

@sai1 did you install the virtualvenv package with pip?

After creating your virtual environment you want to use:

venv\Scripts\activate

Regards,
Earnie Boyd, CEO
Seasoned Solutions Advisor LLC
Schedule 1-on-1 help
Join me on Slack

 
Posted : 10/21/2023 5:55 pm
(@sai1)
Posts: 3
New Member
 

hey @ssadvisor im just following you're instructions


, first instruction was applied but the second was not. can you please see the snippet of error?

venv\Scripts\activate
 
Posted : 10/21/2023 6:17 pm
SSAdvisor
(@ssadvisor)
Posts: 1139
Noble Member
Topic starter
 

@sai1 is this computer supplied by an employer or does it belong to you? If it's owned by an employer then you'll have to request access to run scripts from them else you'll have to research the article the error gave you to resolve it.

Regards,
Earnie Boyd, CEO
Seasoned Solutions Advisor LLC
Schedule 1-on-1 help
Join me on Slack

 
Posted : 10/21/2023 8:01 pm
Hasan Aboul Hasan
(@admin)
Posts: 1276
Member Admin
 

@sai1 is this computer yours?

 
Posted : 10/23/2023 3:45 am
(@sai1)
Posts: 3
New Member
 

@  Hasan Aboul Hasan yes computer is mine

 
Posted : 10/23/2023 6:50 am
SSAdvisor
(@ssadvisor)
Posts: 1139
Noble Member
Topic starter
 

@sai1 then follow the link given in the error and take actions to correct the issue.

Regards,
Earnie Boyd, CEO
Seasoned Solutions Advisor LLC
Schedule 1-on-1 help
Join me on Slack

 
Posted : 10/23/2023 4:29 pm
Hasan Aboul Hasan
(@admin)
Posts: 1276
Member Admin
 

scripts are disabled on your system. it related to your computer policies and powershell execution policy

first try opening VS code as administrator and try again. if didn't work, you need to enable script execution.

Check the current execution policy:

Type the following command:

Get-ExecutionPolicy

  1. This will return the current policy. It might be one of the following:

    • Restricted: No scripts can run, regardless of signing.
    • AllSigned: Only scripts signed by a trusted publisher can run.
    • RemoteSigned: Scripts created locally can run. Scripts downloaded from the internet or received via email need to be signed by a trusted publisher.
    • Unrestricted: All scripts can run, whether they're signed or not.
    • Bypass: Nothing is blocked and no warnings are given.
    • Undefined: No execution policy is set.
  2. Change the execution policy:

    If you want to change the execution policy to allow scripts to run (for example, to "RemoteSigned"), you would use:

Set-ExecutionPolicy RemoteSigned

  1. You'll be prompted to confirm the change. Press Y to confirm.

    ⚠️ Warning: Be cautious when setting the execution policy to Unrestricted as it may expose your system to potential security risks. RemoteSigned is often a safer compromise, but always be sure of the source and content of scripts you're executing.

  2. Close and reopen PowerShell:

    For the change to take effect, you should close and then reopen the PowerShell window.

  3. Run your script:

    With the new policy in place, you should be able to run your script.

 
Posted : 10/24/2023 4:17 pm
SSAdvisor reacted
SSAdvisor
(@ssadvisor)
Posts: 1139
Noble Member
Topic starter
 

@admin I've despised using Windows as a development tool for decades. 😳 I'm currently using Chrome OS with its Linux container enabled to do my programming.

Regards,
Earnie Boyd, CEO
Seasoned Solutions Advisor LLC
Schedule 1-on-1 help
Join me on Slack

 
Posted : 10/25/2023 3:51 pm
Share:
[the_ad_group id="312"]