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)