Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -7,6 +7,8 @@ from PIL import Image, ImageEnhance, ImageFilter
|
|
7 |
import io
|
8 |
import cv2
|
9 |
import numpy as np
|
|
|
|
|
10 |
|
11 |
# --- Create the Flask App ---
|
12 |
app = Flask(__name__)
|
@@ -239,6 +241,59 @@ def auto_enhance_image_api():
|
|
239 |
|
240 |
return jsonify({"error": "An unknown error occurred"}), 500
|
241 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
242 |
# --- Run the App ---
|
243 |
if __name__ == '__main__':
|
244 |
# For local testing, ensure the environment variable is set.
|
|
|
7 |
import io
|
8 |
import cv2
|
9 |
import numpy as np
|
10 |
+
from diffusers import DiffusionPipeline
|
11 |
+
import torch
|
12 |
|
13 |
# --- Create the Flask App ---
|
14 |
app = Flask(__name__)
|
|
|
241 |
|
242 |
return jsonify({"error": "An unknown error occurred"}), 500
|
243 |
|
244 |
+
# --- AI IMAGE GENERATOR (TEXT-TO-IMAGE) API ENDPOINT ---
|
245 |
+
|
246 |
+
# AI Model ko pehle se load karke memory mein rakhna taake har request per time na lage
|
247 |
+
# Hum ek chota aur tez model istemal kar rahe hain taake free server per chal sake
|
248 |
+
pipe = DiffusionPipeline.from_pretrained("runwayml/stable-diffusion-v1-5", torch_dtype=torch.float16)
|
249 |
+
# Agar aapke paas GPU hai (paid plan), to is line ko istemal karein:
|
250 |
+
# pipe = pipe.to("cuda")
|
251 |
+
# Lekin free CPU plan per yeh zaroori nahi hai.
|
252 |
+
|
253 |
+
@app.route('/generate-image', methods=['POST'])
|
254 |
+
def generate_image_api():
|
255 |
+
# 1. --- API Key Authentication ---
|
256 |
+
api_key_header = request.headers.get('x-api-key')
|
257 |
+
if not api_key_header or api_key_header != API_KEY:
|
258 |
+
return jsonify({"error": "Unauthorized. Invalid or missing API Key."}), 401
|
259 |
+
|
260 |
+
# 2. --- User ka Text Prompt Haasil Karna ---
|
261 |
+
# Is baar hum JSON data expect kar rahe hain, file nahi
|
262 |
+
if not request.is_json:
|
263 |
+
return jsonify({"error": "Invalid request: JSON expected"}), 400
|
264 |
+
|
265 |
+
data = request.get_json()
|
266 |
+
prompt = data.get('prompt', '')
|
267 |
+
|
268 |
+
if not prompt:
|
269 |
+
return jsonify({"error": "Prompt is required"}), 400
|
270 |
+
|
271 |
+
# 3. --- AI Model se Image Generate Karna ---
|
272 |
+
try:
|
273 |
+
# AI model ko prompt de kar image banana
|
274 |
+
# `num_inference_steps` se quality control hoti hai (kam = tez, zyada = behtar)
|
275 |
+
image = pipe(prompt, num_inference_steps=20).images[0]
|
276 |
+
|
277 |
+
# Ek khali "in-memory" file banana
|
278 |
+
output_buffer = io.BytesIO()
|
279 |
+
|
280 |
+
# Generated image ko buffer mein save karna
|
281 |
+
image.save(output_buffer, format='PNG')
|
282 |
+
|
283 |
+
# Image ke bytes haasil karna
|
284 |
+
output_image_bytes = output_buffer.getvalue()
|
285 |
+
|
286 |
+
# 4. --- Send the Response ---
|
287 |
+
# Generated image ko user ko wapas bhejna
|
288 |
+
return send_file(
|
289 |
+
io.BytesIO(output_image_bytes),
|
290 |
+
mimetype='image/png',
|
291 |
+
as_attachment=True,
|
292 |
+
download_name='generated_image.png'
|
293 |
+
)
|
294 |
+
except Exception as e:
|
295 |
+
return jsonify({"error": "Failed to generate image", "details": str(e)}), 500
|
296 |
+
|
297 |
# --- Run the App ---
|
298 |
if __name__ == '__main__':
|
299 |
# For local testing, ensure the environment variable is set.
|