File size: 1,511 Bytes
5ce004e
 
 
 
 
a88e407
5ce004e
 
 
 
 
 
e4f3d55
b8d0ae7
5ce004e
c13f1a8
5ce004e
d9da751
 
 
 
 
5ce004e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7500685
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
from flask import Flask, request, send_file, jsonify
from flask_cors import CORS
from huggingface_hub import InferenceClient
from PIL import Image
import io
import os

# Initialize Flask app
myapp = Flask(__name__)
CORS(myapp)

# Your Hugging Face API Token

HF_TOKEN = os.environ.get("HF_TOKEN")  
# Initialize the Inference Client
client = InferenceClient(model="pimcore/IEP__image-upscaling-2x", token=HF_TOKEN)

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


@myapp.route('/upscale', methods=['POST'])
def upscale_image():
    try:
        # Retrieve the image file from the request
        image_file = request.files.get('image')
        if not image_file:
            return jsonify({"error": "No image file provided"}), 400

        # Open the image
        input_image = Image.open(image_file)

        # Use the Hugging Face Inference Client to upscale the image
        result = client.image_to_image(
            image=input_image,
            prompt="",  # Optional: Add a guiding prompt
            num_inference_steps=50,  # Adjust as needed
            guidance_scale=6.0  # Adjust as needed
        )

        # Save the upscaled image to a BytesIO object
        img_io = io.BytesIO()
        result.save(img_io, 'PNG')
        img_io.seek(0)

        return send_file(img_io, mimetype='image/png')
    except Exception as e:
        return jsonify({"error": str(e)}), 500

# Run the app
if __name__ == '__main__':
    myapp.run(host='0.0.0.0', port=7860)