|
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 |
|
|
|
|
|
myapp = Flask(__name__) |
|
CORS(myapp) |
|
|
|
|
|
|
|
HF_TOKEN = os.environ.get("HF_TOKEN") |
|
|
|
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: |
|
|
|
image_file = request.files.get('image') |
|
if not image_file: |
|
return jsonify({"error": "No image file provided"}), 400 |
|
|
|
|
|
input_image = Image.open(image_file) |
|
|
|
|
|
result = client.image_to_image( |
|
image=input_image, |
|
prompt="", |
|
num_inference_steps=50, |
|
guidance_scale=6.0 |
|
) |
|
|
|
|
|
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 |
|
|
|
|
|
if __name__ == '__main__': |
|
myapp.run(host='0.0.0.0', port=7860) |