AI & Automation (vnROM)

Mascot
Mascot

Posted on

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

Chia sẻ của bạn hoangdh

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)
Enter fullscreen mode Exit fullscreen mode

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 
Enter fullscreen mode Exit fullscreen mode
  • 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

  • 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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

Kết quả trả về:

Tham khảo:

Top comments (0)