Skip to content

Instantly share code, notes, and snippets.

@hoangdh
Last active March 17, 2025 09:00
Show Gist options
  • Save hoangdh/810ecdb3176244645cba9abe9bb075b7 to your computer and use it in GitHub Desktop.
Save hoangdh/810ecdb3176244645cba9abe9bb075b7 to your computer and use it in GitHub Desktop.
Tạo hình ảnh bằng Bing Art thông qua API.

Tạo hình ảnh bằng Bing Art AI của Microsoft thông qua API

Script API

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:
        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 = []
    try:
        results = bing_art.generate_images(prompt)
        for image in results['images']:
            imageUrl = image['url']
            images.append(imageUrl)
    except Exception as e:
        return jsonify({"error": f"An error occurred while generating images: {str(e)}", "prompt": prompt}), 500
    finally:
        bing_art.close_session()

    return jsonify({"images": list(set(images)), "prompt": prompt})

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

    2025-02-24_09-23

  • 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

    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

Kết quả trả về:

image

Tham khảo:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment