Update app.py
Browse files
app.py
CHANGED
@@ -1,138 +1,32 @@
|
|
1 |
-
from flask import Flask, request,
|
2 |
from flask_cors import CORS
|
3 |
-
import os
|
4 |
from huggingface_hub import InferenceClient
|
5 |
-
from io import BytesIO
|
6 |
from PIL import Image
|
7 |
-
import
|
8 |
|
9 |
-
# Initialize the Flask app
|
10 |
app = Flask(__name__)
|
11 |
CORS(app) # Enable CORS for all routes
|
|
|
12 |
|
13 |
-
|
14 |
-
|
15 |
-
|
|
|
|
|
16 |
|
17 |
-
|
18 |
-
|
19 |
-
return "Welcome to the Image Generation Service!"
|
20 |
|
21 |
-
#
|
22 |
-
|
23 |
-
try:
|
24 |
-
# Generate the image using Hugging Face's inference API with additional parameters
|
25 |
-
image = client.text_to_image(
|
26 |
-
prompt=prompt,
|
27 |
-
negative_prompt=negative_prompt,
|
28 |
-
height=height,
|
29 |
-
width=width,
|
30 |
-
model=model,
|
31 |
-
num_inference_steps=num_inference_steps, # Control the number of inference steps
|
32 |
-
guidance_scale=guidance_scale, # Control the guidance scale
|
33 |
-
seed=seed # Control the seed for reproducibility
|
34 |
-
)
|
35 |
-
return image # Return the generated image
|
36 |
-
except Exception as e:
|
37 |
-
print(f"Error generating image: {str(e)}")
|
38 |
-
return None
|
39 |
|
40 |
-
#
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
hardcoded_model = "stabilityai/sdxl-1.0"
|
45 |
-
|
46 |
-
# Convert the base64-encoded input image to a PIL image
|
47 |
-
decoded_image = Image.open(BytesIO(base64.b64decode(input_image)))
|
48 |
-
|
49 |
-
# Modify the image using image-to-image transformation
|
50 |
-
modified_image = client.image_to_image(
|
51 |
-
image=decoded_image,
|
52 |
-
prompt=prompt,
|
53 |
-
model=hardcoded_model,
|
54 |
-
height=height,
|
55 |
-
width=width,
|
56 |
-
num_inference_steps=num_inference_steps,
|
57 |
-
guidance_scale=guidance_scale,
|
58 |
-
seed=seed
|
59 |
-
)
|
60 |
-
return modified_image
|
61 |
-
except Exception as e:
|
62 |
-
print(f"Error generating image-to-image: {str(e)}")
|
63 |
-
return None
|
64 |
|
65 |
-
#
|
66 |
-
|
67 |
-
def generate_api():
|
68 |
-
data = request.get_json()
|
69 |
|
70 |
-
# Extract common fields
|
71 |
-
prompt = data.get('prompt', '')
|
72 |
-
negative_prompt = data.get('negative_prompt', None)
|
73 |
-
num_inference_steps = data.get('num_inference_steps', 50)
|
74 |
-
guidance_scale = data.get('guidance_scale', 7.5)
|
75 |
-
model_name = data.get('model', 'stabilityai/stable-diffusion-2-1') # Model specified by the user for text-to-image
|
76 |
-
seed = data.get('seed', None)
|
77 |
-
|
78 |
-
if not prompt:
|
79 |
-
return jsonify({"error": "Prompt is required"}), 400
|
80 |
-
|
81 |
-
# Check if the request contains an image for image-to-image processing
|
82 |
-
input_image = data.get('image', None) # Expecting a base64-encoded image string
|
83 |
-
if input_image:
|
84 |
-
# Extract parameters specific to image-to-image
|
85 |
-
height = data.get('height', 1024)
|
86 |
-
width = data.get('width', 720)
|
87 |
-
|
88 |
-
# Call the image-to-image function
|
89 |
-
final_image = generate_image_to_image(
|
90 |
-
input_image, prompt, height, width, num_inference_steps, guidance_scale, seed
|
91 |
-
)
|
92 |
-
|
93 |
-
if final_image:
|
94 |
-
# Save the modified image to a BytesIO object
|
95 |
-
img_byte_arr = BytesIO()
|
96 |
-
final_image.save(img_byte_arr, format='PNG')
|
97 |
-
img_byte_arr.seek(0)
|
98 |
-
|
99 |
-
# Send the modified image as a response
|
100 |
-
return send_file(
|
101 |
-
img_byte_arr,
|
102 |
-
mimetype='image/png',
|
103 |
-
as_attachment=False,
|
104 |
-
download_name='final_image.png'
|
105 |
-
)
|
106 |
-
else:
|
107 |
-
return jsonify({"error": "Failed to generate image-to-image."}), 500
|
108 |
-
|
109 |
-
# If no image is provided, proceed with text-to-image generation
|
110 |
-
else:
|
111 |
-
# Default height and width for text-to-image
|
112 |
-
height = data.get('height', 512)
|
113 |
-
width = data.get('width', 512)
|
114 |
-
|
115 |
-
# Call the text-to-image function
|
116 |
-
generated_image = generate_text_to_image(
|
117 |
-
prompt, negative_prompt, height, width, model_name, num_inference_steps, guidance_scale, seed
|
118 |
-
)
|
119 |
-
|
120 |
-
if generated_image:
|
121 |
-
# Save the generated image to a BytesIO object
|
122 |
-
img_byte_arr = BytesIO()
|
123 |
-
generated_image.save(img_byte_arr, format='PNG')
|
124 |
-
img_byte_arr.seek(0)
|
125 |
-
|
126 |
-
# Send the generated image as a response
|
127 |
-
return send_file(
|
128 |
-
img_byte_arr,
|
129 |
-
mimetype='image/png',
|
130 |
-
as_attachment=False,
|
131 |
-
download_name='generated_image.png'
|
132 |
-
)
|
133 |
-
else:
|
134 |
-
return jsonify({"error": "Failed to generate text-to-image."}), 500
|
135 |
-
|
136 |
-
# Add this block to make sure your app runs when called
|
137 |
if __name__ == "__main__":
|
138 |
-
app.run(
|
|
|
1 |
+
from flask import Flask, request, send_file
|
2 |
from flask_cors import CORS
|
|
|
3 |
from huggingface_hub import InferenceClient
|
|
|
4 |
from PIL import Image
|
5 |
+
import io
|
6 |
|
|
|
7 |
app = Flask(__name__)
|
8 |
CORS(app) # Enable CORS for all routes
|
9 |
+
client = InferenceClient()
|
10 |
|
11 |
+
@app.route('/generate-image', methods=['POST'])
|
12 |
+
def generate_image():
|
13 |
+
# Get the image file from the request
|
14 |
+
file = request.files['image']
|
15 |
+
prompt = request.form.get('prompt', '')
|
16 |
|
17 |
+
# Read the image directly from the file stream
|
18 |
+
image = Image.open(file.stream)
|
|
|
19 |
|
20 |
+
# Generate image using the InferenceClient
|
21 |
+
generated_image = client.image_to_image(image, prompt=prompt)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
22 |
|
23 |
+
# Save the generated image to a BytesIO object
|
24 |
+
img_byte_arr = io.BytesIO()
|
25 |
+
generated_image.save(img_byte_arr, format='JPEG')
|
26 |
+
img_byte_arr.seek(0) # Move the cursor to the beginning of the BytesIO object
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
27 |
|
28 |
+
# Send the generated image back to the client
|
29 |
+
return send_file(img_byte_arr, mimetype='image/jpeg')
|
|
|
|
|
30 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
31 |
if __name__ == "__main__":
|
32 |
+
app.run(debug=True)
|