File size: 1,199 Bytes
2b70e04
c615408
462b6f1
 
2b70e04
df9e081
c615408
 
 
2b70e04
462b6f1
cd230ae
 
 
 
2b70e04
 
df9e081
 
 
462b6f1
df9e081
 
 
6d2d42c
2b70e04
 
0796b9d
2b70e04
 
df9e081
2b70e04
a322656
2b70e04
df9e081
eedc86a
c615408
df9e081
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
from flask import Flask, request, send_file
from flask_cors import CORS
from huggingface_hub import InferenceClient
from PIL import Image
import io
import base64

app = Flask(__name__)
CORS(app)  # Enable CORS for all routes
client = InferenceClient()

@app.route('/')
def home():
    return "Welcome to the Image Background Remover!"

@app.route('/generate-image', methods=['POST'])
def generate_image():
    data = request.json  # Get the JSON data from the request
    base64_image = data['image']  # Get the base64 image string
    prompt = data['prompt']  # Get the prompt

    # Decode the base64 image
    image_data = base64.b64decode(base64_image)
    image = Image.open(io.BytesIO(image_data))

    # Generate image using the InferenceClient
    generated_image = client.image_to_image(image, prompt=prompt)

    # Save the generated image to a BytesIO object
    img_byte_arr = io.BytesIO()
    generated_image.save(img_byte_arr, format='JPEG')
    img_byte_arr.seek(0)  # Move the cursor to the beginning of the BytesIO object

    # Send the generated image back to the client
    return send_file(img_byte_arr, mimetype='image/jpeg')

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