Skip to content

Instantly share code, notes, and snippets.

@ken2190
Forked from hoangdh/bingart-api.md
Created February 27, 2025 20:54
Show Gist options
  • Select an option

  • Save ken2190/037e6cd66e4f5521e6583c0e7049c37d to your computer and use it in GitHub Desktop.

Select an option

Save ken2190/037e6cd66e4f5521e6583c0e7049c37d to your computer and use it in GitHub Desktop.

Revisions

  1. @hoangdh hoangdh revised this gist Feb 24, 2025. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion bingart-api.md
    Original file line number Diff line number Diff line change
    @@ -43,7 +43,7 @@ if __name__ == '__main__':
    pip install flask bingart
    ```
    - **Bước 2:** Lấy Cookie của Bing:
    - Dùng trình duyệt đăng nhập vào https://bing.com
    - Dùng trình duyệt đăng nhập Tài khoản vào https://bing.com
    - Bấm F12, chọn tab **Console**
    - Thực hiện lệnh: `cookieStore.get("_U").then(result => console.log(result.value))`
    - Lấy thông tin và điền vào biến `myCookie` trong script
  2. @hoangdh hoangdh revised this gist Feb 24, 2025. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions bingart-api.md
    Original file line number Diff line number Diff line change
    @@ -52,6 +52,7 @@ pip install flask bingart

    - **Bước 2**: Điền Cookie vừa lấy và sao chép script trên vào máy chủ của bạn
    - **Bước 3**: Chạy script

    Giả sử, ta đã đưa script vào thư mục `/opt/bingart-api.py`

    ```
  3. @hoangdh hoangdh revised this gist Feb 24, 2025. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion bingart-api.md
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,4 @@
    # Tạo hình ảnh bằng Bing Art AI của Microsoft bằng API
    # Tạo hình ảnh bằng Bing Art AI của Microsoft thông qua API

    ## Script API

  4. @hoangdh hoangdh revised this gist Feb 24, 2025. 1 changed file with 11 additions and 2 deletions.
    13 changes: 11 additions & 2 deletions bingart-api.md
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,5 @@
    # Tạo hình ảnh bằng Bing Art AI của Microsoft bằng API

    ## Script API

    ```python
    @@ -16,7 +18,7 @@ def generate_images():
    return jsonify({"error": "Please provide a prompt via the GET parameter 'prompt'"}), 400

    # Replace 'myCooko' with your valid _U cookie value
    bing_art = BingArt(auth_cookie_U='myCooko')
    bing_art = BingArt(auth_cookie_U=myCookie)
    images = []
    try:
    results = bing_art.generate_images(prompt)
    @@ -48,7 +50,7 @@ pip install flask bingart

    ![2025-02-24_09-23](https://gist.github.com/user-attachments/assets/62042e65-3065-4f72-80b1-47d76404bfec)

    - **Bước 2**: Sao chép script trên vào máy chủ của bạn
    - **Bước 2**: Điền Cookie vừa lấy và sao chép script trên vào máy chủ của bạn
    - **Bước 3**: Chạy script
    Giả sử, ta đã đưa script vào thư mục `/opt/bingart-api.py`

    @@ -66,5 +68,12 @@ Truy cập vào địa chỉ bằng trình duyệt như sau:
    http://127.0.0.1:5000/bingart?prompt=a cute dog
    ```

    Kết quả trả về:

    ![image](https://gist.github.com/user-attachments/assets/dfb8ae50-b47a-493c-ade6-d7697c34f5d9)


    ## Tham khảo:
    - Code: Genini AI
    - https://github.com/DedInc/bingart

  5. @hoangdh hoangdh revised this gist Feb 24, 2025. 1 changed file with 6 additions and 13 deletions.
    19 changes: 6 additions & 13 deletions bingart-api.md
    Original file line number Diff line number Diff line change
    @@ -13,29 +13,22 @@ app = Flask(__name__)
    def generate_images():
    prompt = request.args.get('prompt')
    if not prompt:
    if request.accept_mimetypes.accept_html:
    return render_template('index.html', error="Please provide a prompt via the GET parameter 'prompt'", images=[], prompt="")
    else:
    return jsonify({"error": "Please provide a prompt via the GET parameter 'prompt'"}), 400
    return jsonify({"error": "Please provide a prompt via the GET parameter 'prompt'"}), 400

    # Replace 'myCooko' with your valid _U cookie value
    bing_art = BingArt(auth_cookie_U=myCookie)
    bing_art = BingArt(auth_cookie_U='myCooko')
    images = []
    error_message = None
    try:
    results = bing_art.generate_images(prompt)
    for image in results['images']:
    imageUrl = image['url']
    images.append(imageUrl)
    except Exception as e:
    error_message = f"An error occurred while generating images: {str(e)}"
    if not request.accept_mimetypes.accept_html:
    return jsonify({"error": error_message, "prompt": prompt}), 500
    return jsonify({"error": f"An error occurred while generating images: {str(e)}", "prompt": prompt}), 500
    finally:
    bing_art.close_session()

    if request.accept_mimetypes.accept_html:
    return render_template('index.html', images=list(set(images)), prompt=prompt, error=error_message)
    else:
    return jsonify({"images": list(set(images)), "prompt": prompt, "error": error_message})
    return jsonify({"images": list(set(images)), "prompt": prompt})

    if __name__ == '__main__':
    app.run(debug=True, host='0.0.0.0', port=5000)
  6. @hoangdh hoangdh created this gist Feb 24, 2025.
    77 changes: 77 additions & 0 deletions bingart-api.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,77 @@
    ## Script API

    ```python
    from flask import Flask, request, jsonify, render_template
    from bingart import BingArt

    # Replace your cookie
    myCookie = ''

    app = Flask(__name__)

    @app.route('/bingart', methods=['GET'])
    def generate_images():
    prompt = request.args.get('prompt')
    if not prompt:
    if request.accept_mimetypes.accept_html:
    return render_template('index.html', error="Please provide a prompt via the GET parameter 'prompt'", images=[], prompt="")
    else:
    return jsonify({"error": "Please provide a prompt via the GET parameter 'prompt'"}), 400

    # Replace 'myCooko' with your valid _U cookie value
    bing_art = BingArt(auth_cookie_U=myCookie)
    images = []
    error_message = None
    try:
    results = bing_art.generate_images(prompt)
    for image in results['images']:
    imageUrl = image['url']
    images.append(imageUrl)
    except Exception as e:
    error_message = f"An error occurred while generating images: {str(e)}"
    if not request.accept_mimetypes.accept_html:
    return jsonify({"error": error_message, "prompt": prompt}), 500

    if request.accept_mimetypes.accept_html:
    return render_template('index.html', images=list(set(images)), prompt=prompt, error=error_message)
    else:
    return jsonify({"images": list(set(images)), "prompt": prompt, "error": error_message})

    if __name__ == '__main__':
    app.run(debug=True, host='0.0.0.0', port=5000)
    ```
    ## Cài đặt môi trường

    - **Bước 1**: Cài đặt các thư viện cần thiết

    ```
    pip install flask bingart
    ```
    - **Bước 2:** Lấy Cookie của Bing:
    - Dùng trình duyệt đăng nhập vào https://bing.com
    - Bấm F12, chọn tab **Console**
    - Thực hiện lệnh: `cookieStore.get("_U").then(result => console.log(result.value))`
    - Lấy thông tin và điền vào biến `myCookie` trong script

    ![2025-02-24_09-23](https://gist.github.com/user-attachments/assets/62042e65-3065-4f72-80b1-47d76404bfec)

    - **Bước 2**: Sao chép script trên vào máy chủ của bạn
    - **Bước 3**: Chạy script
    Giả sử, ta đã đưa script vào thư mục `/opt/bingart-api.py`

    ```
    python3 /opt/bingart-api.py
    ```

    ## Sử dụng:

    Giả sử, ta chạy ứng dụng trên localhost với địa chỉ `http://127.0.0.1:5000`.

    Truy cập vào địa chỉ bằng trình duyệt như sau:

    ```
    http://127.0.0.1:5000/bingart?prompt=a cute dog
    ```