Geek7 commited on
Commit
885a3c1
·
verified ·
1 Parent(s): c18e37e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +6 -4
app.py CHANGED
@@ -11,7 +11,7 @@ client = Client("Geek7/mdztxi2")
11
  @app.route('/generate-image', methods=['POST'])
12
  def generate_image():
13
  data = request.get_json()
14
-
15
  # Check for required fields in the request data
16
  model_str = data.get('model_str')
17
  prompt = data.get('prompt')
@@ -22,6 +22,7 @@ def generate_image():
22
 
23
  # Make a prediction request
24
  try:
 
25
  result = client.predict(
26
  model_str=model_str,
27
  prompt=prompt,
@@ -29,10 +30,11 @@ def generate_image():
29
  api_name="/predict"
30
  )
31
 
32
- # Check if result is a file path or image bytes
33
- # If the result is an image path, adjust accordingly
34
- if isinstance(result, str) and os.path.exists(result): # Assuming result is the path to the image
35
  return send_file(result, mimetype='image/png') # Send the generated image file
 
 
36
  else:
37
  return jsonify({"error": "Image generation failed, result not valid."}), 500
38
  except Exception as e:
 
11
  @app.route('/generate-image', methods=['POST'])
12
  def generate_image():
13
  data = request.get_json()
14
+
15
  # Check for required fields in the request data
16
  model_str = data.get('model_str')
17
  prompt = data.get('prompt')
 
22
 
23
  # Make a prediction request
24
  try:
25
+ # Making sure to capture the actual result returned
26
  result = client.predict(
27
  model_str=model_str,
28
  prompt=prompt,
 
30
  api_name="/predict"
31
  )
32
 
33
+ # If the result is a file path, send it; if it's image bytes, handle accordingly
34
+ if isinstance(result, str) and os.path.exists(result): # If the result is a path
 
35
  return send_file(result, mimetype='image/png') # Send the generated image file
36
+ elif isinstance(result, bytes): # If the result is in bytes
37
+ return send_file(io.BytesIO(result), mimetype='image/png') # Return image bytes as a file
38
  else:
39
  return jsonify({"error": "Image generation failed, result not valid."}), 500
40
  except Exception as e: