soiz commited on
Commit
7c29ed9
1 Parent(s): e505e53

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +53 -1
app.py CHANGED
@@ -78,4 +78,56 @@ def gen_fn(model_str, prompt, negative_prompt=None, noise=None, cfg_scale=None,
78
 
79
  # Check if the image is completely black
80
  black = Image.new('RGB', image.size, (0, 0, 0))
81
- if ImageChops.difference(image, black).get
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
78
 
79
  # Check if the image is completely black
80
  black = Image.new('RGB', image.size, (0, 0, 0))
81
+ if ImageChops.difference(image, black).getbbox() is None:
82
+ return None, 'The image is completely black. There may be a parameter that cannot be specified, or an error may have occurred internally.'
83
+
84
+ return image, None
85
+
86
+ except Exception as e:
87
+ print("Error generating image:", e)
88
+ return None, f"Error generating image: {e}"
89
+
90
+ @app.route('/', methods=['GET'])
91
+ def home():
92
+ prompt = request.args.get('prompt', '')
93
+ model = request.args.get('model', '')
94
+ negative_prompt = request.args.get('Nprompt', None)
95
+ noise = request.args.get('noise', None)
96
+ cfg_scale = request.args.get('cfg_scale', None)
97
+ num_inference_steps = request.args.get('steps', None)
98
+
99
+ try:
100
+ if cfg_scale is not None:
101
+ cfg_scale = float(cfg_scale)
102
+ except ValueError:
103
+ return 'Invalid "cfg_scale" parameter. It should be a number.', 400
104
+
105
+ try:
106
+ if num_inference_steps is not None:
107
+ num_inference_steps = int(num_inference_steps)
108
+ except ValueError:
109
+ return 'Invalid "steps" parameter. It should be an integer.', 400
110
+
111
+ if not model:
112
+ return 'Please provide a "model" query parameter in the URL.', 400
113
+
114
+ if not prompt:
115
+ return 'Please provide a "prompt" query parameter in the URL.', 400
116
+
117
+ # Generate the image
118
+ image, error_message = gen_fn(model, prompt, negative_prompt, noise, cfg_scale, num_inference_steps)
119
+ if error_message:
120
+ return error_message, 400
121
+
122
+ if isinstance(image, Image.Image): # Ensure the result is a PIL image
123
+ # Save image to BytesIO object
124
+ img_io = BytesIO()
125
+ image.save(img_io, format='PNG')
126
+ img_io.seek(0)
127
+ return send_file(img_io, mimetype='image/png', as_attachment=False)
128
+
129
+ return 'Failed to generate image.', 500
130
+
131
+ if __name__ == '__main__':
132
+ # Launch Flask app
133
+ app.run(host='0.0.0.0', port=7860) # Run Flask app