MalikSahib1 commited on
Commit
1d96340
·
verified ·
1 Parent(s): e5d857a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +8 -28
app.py CHANGED
@@ -241,55 +241,35 @@ def auto_enhance_image_api():
241
 
242
  return jsonify({"error": "An unknown error occurred"}), 500
243
 
244
- # --- AI IMAGE GENERATOR (TEXT-TO-IMAGE) API ENDPOINT (WITH LAZY LOADING & TINY MODEL) ---
245
 
246
- # Hum model ke liye ek khali "box" (variable) banayenge. Model start mein load nahi hoga.
247
- pipe = None
 
248
 
249
  @app.route('/generate-image', methods=['POST'])
250
  def generate_image_api():
251
- global pipe # Hum function ko bata rahe hain ke bahar wale 'pipe' box ko istemal karna hai
252
-
253
- # 1. --- API Key Authentication ---
254
  api_key_header = request.headers.get('x-api-key')
255
  if not api_key_header or api_key_header != API_KEY:
256
  return jsonify({"error": "Unauthorized. Invalid or missing API Key."}), 401
257
 
258
- # 2. --- User ka Text Prompt Haasil Karna ---
259
  if not request.is_json:
260
  return jsonify({"error": "Invalid request: JSON expected"}), 400
261
-
262
  data = request.get_json()
263
  prompt = data.get('prompt', '')
264
-
265
  if not prompt:
266
  return jsonify({"error": "Prompt is required"}), 400
267
 
268
- # 3. --- AI Model ko Load aur Istemal Karna ---
269
  try:
270
- # Check karein ke kya 'pipe' ka box khali hai?
271
- if pipe is None:
272
- print("Loading Tiny Stable Diffusion model for the first time...")
273
- # Use a much smaller and faster model designed for CPUs
274
- pipe = DiffusionPipeline.from_pretrained("hf-internal-testing/tiny-stable-diffusion-torch")
275
- print("Model loaded successfully.")
276
-
277
- # AI model ko prompt de kar image banana
278
- print(f"Generating image for prompt: {prompt}")
279
- # Tiny model ke liye kam steps zaroori hain
280
  image = pipe(prompt, num_inference_steps=10).images[0]
281
- print("Image generated.")
282
-
283
- # Ek khali "in-memory" file banana
284
- output_buffer = io.BytesIO()
285
 
286
- # Generated image ko buffer mein save karna
287
  image.save(output_buffer, format='PNG')
288
-
289
- # Image ke bytes haasil karna
290
  output_image_bytes = output_buffer.getvalue()
291
 
292
- # 4. --- Send the Response ---
293
  return send_file(
294
  io.BytesIO(output_image_bytes),
295
  mimetype='image/png',
 
241
 
242
  return jsonify({"error": "An unknown error occurred"}), 500
243
 
244
+ # --- AI IMAGE GENERATOR (TEXT-TO-IMAGE) API ENDPOINT ---
245
 
246
+ # Model ab pehle se hi download ho chuka hai. Hum usko yahan load karenge.
247
+ # Yeh ab crash nahi hoga kyunki files pehle se cache mein hain.
248
+ pipe = DiffusionPipeline.from_pretrained("hf-internal-testing/tiny-stable-diffusion-torch")
249
 
250
  @app.route('/generate-image', methods=['POST'])
251
  def generate_image_api():
252
+ # API Key Authentication
 
 
253
  api_key_header = request.headers.get('x-api-key')
254
  if not api_key_header or api_key_header != API_KEY:
255
  return jsonify({"error": "Unauthorized. Invalid or missing API Key."}), 401
256
 
257
+ # User ka Text Prompt Haasil Karna
258
  if not request.is_json:
259
  return jsonify({"error": "Invalid request: JSON expected"}), 400
 
260
  data = request.get_json()
261
  prompt = data.get('prompt', '')
 
262
  if not prompt:
263
  return jsonify({"error": "Prompt is required"}), 400
264
 
265
+ # AI Model se Image Generate Karna
266
  try:
 
 
 
 
 
 
 
 
 
 
267
  image = pipe(prompt, num_inference_steps=10).images[0]
 
 
 
 
268
 
269
+ output_buffer = io.BytesIO()
270
  image.save(output_buffer, format='PNG')
 
 
271
  output_image_bytes = output_buffer.getvalue()
272
 
 
273
  return send_file(
274
  io.BytesIO(output_image_bytes),
275
  mimetype='image/png',