Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,47 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from flask import Flask, request, send_file, jsonify
|
2 |
+
from flask_cors import CORS
|
3 |
+
from huggingface_hub import InferenceClient
|
4 |
+
from PIL import Image
|
5 |
+
import io
|
6 |
+
|
7 |
+
# Initialize Flask app
|
8 |
+
app = Flask(__name__)
|
9 |
+
CORS(app)
|
10 |
+
|
11 |
+
# Your Hugging Face API Token
|
12 |
+
API_TOKEN = "your_huggingface_api_key"
|
13 |
+
|
14 |
+
# Initialize the Inference Client
|
15 |
+
client = InferenceClient(model="stabilityai/sd-x2-latent-upscaler", token=API_TOKEN)
|
16 |
+
|
17 |
+
@app.route('/upscale', methods=['POST'])
|
18 |
+
def upscale_image():
|
19 |
+
try:
|
20 |
+
# Retrieve the image file from the request
|
21 |
+
image_file = request.files.get('image')
|
22 |
+
if not image_file:
|
23 |
+
return jsonify({"error": "No image file provided"}), 400
|
24 |
+
|
25 |
+
# Open the image
|
26 |
+
input_image = Image.open(image_file)
|
27 |
+
|
28 |
+
# Use the Hugging Face Inference Client to upscale the image
|
29 |
+
result = client.image_to_image(
|
30 |
+
image=input_image,
|
31 |
+
prompt="", # Optional: Add a guiding prompt
|
32 |
+
num_inference_steps=50, # Adjust as needed
|
33 |
+
guidance_scale=6.0 # Adjust as needed
|
34 |
+
)
|
35 |
+
|
36 |
+
# Save the upscaled image to a BytesIO object
|
37 |
+
img_io = io.BytesIO()
|
38 |
+
result.save(img_io, 'PNG')
|
39 |
+
img_io.seek(0)
|
40 |
+
|
41 |
+
return send_file(img_io, mimetype='image/png')
|
42 |
+
except Exception as e:
|
43 |
+
return jsonify({"error": str(e)}), 500
|
44 |
+
|
45 |
+
# Run the app
|
46 |
+
if __name__ == '__main__':
|
47 |
+
app.run(host='0.0.0.0', port=5000)
|