Kastg commited on
Commit
0bd3eb1
·
verified ·
1 Parent(s): cc98cb7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +103 -0
app.py CHANGED
@@ -1,6 +1,7 @@
1
  from flask import Flask, request, jsonify, send_from_directory
2
  from PIL import Image
3
  from io import BytesIO
 
4
  import requests
5
  import os
6
  import psutil
@@ -126,6 +127,108 @@ def upload_image():
126
  return jsonify({'success': f'Image uploaded as {image_name}'}), 200
127
  except Exception as e:
128
  return jsonify({'error': str(e)}), 500
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
129
 
130
  if __name__ == "__main__":
131
  app.run(host="0.0.0.0", port=7860, debug=True)
 
1
  from flask import Flask, request, jsonify, send_from_directory
2
  from PIL import Image
3
  from io import BytesIO
4
+ from prodiapy import Prodia
5
  import requests
6
  import os
7
  import psutil
 
127
  return jsonify({'success': f'Image uploaded as {image_name}'}), 200
128
  except Exception as e:
129
  return jsonify({'error': str(e)}), 500
130
+
131
+
132
+
133
+
134
+ apiKeys = [
135
+ "2021e94a-1385-4ddc-905b-c050cfb5af32",
136
+ "0bfe0e6d-6bf9-4984-ab07-3a9410a551ad",
137
+ "1452e7a5-d6e2-4600-9641-1c2debde397a",
138
+ "f4b18c3c-ea4d-4b18-be47-f5ad29d70936",
139
+ "688659c2-b2e9-4524-8a91-1c72735ec068",
140
+ "aa64f14e-18d8-44df-91cc-6d4e20051ca3"
141
+ ]
142
+
143
+ # Load styles from style.json file
144
+ with open("style.json", "r") as style_file:
145
+ styleList = json.load(style_file)
146
+
147
+ def getRandomApiKey():
148
+ # Implement your logic to get a random API key here
149
+ return random.choice(apiKeys)
150
+
151
+ def getRandomSeed():
152
+ return random.randint(1, 18446744073709552000)
153
+
154
+ def getAvailableStyles():
155
+ return ', '.join([style["name"] for style in styleList])
156
+
157
+ prodia = Prodia(getRandomApiKey())
158
+
159
+ @app.route('/styles', methods=['GET'])
160
+ def get_styles():
161
+ with open("style.json", "r") as style_file:
162
+ styles = json.load(style_file)
163
+
164
+ return jsonify({"styles": [style["name"] for style in styles]})
165
+
166
+ @app.route('/generate', methods=['POST'])
167
+ async def generate_image():
168
+ try:
169
+ data = request.json
170
+
171
+ prompt = data.get('prompt', '')
172
+ userStyle = data.get('style')
173
+ seed = int(data.get('seed', getRandomSeed()))
174
+ guidance_scale = int(data.get('guidance_scale', 0))
175
+
176
+ if not userStyle:
177
+ return jsonify({"status": "error", "error": "Style is required. Available styles: " + getAvailableStyles()}), 400
178
+
179
+ selectedStyle = next((style for style in styleList if style["name"].lower() == userStyle.lower()), None)
180
+
181
+ if not selectedStyle:
182
+ return jsonify({"status": "error", "error": "Invalid style. Available styles: " + getAvailableStyles()}), 400
183
+
184
+ if guidance_scale and (guidance_scale < 1 or guidance_scale > 100):
185
+ return jsonify({"status": "error", "error": "guidance_scale must be an integer between 1 and 100."}), 400
186
+
187
+ job = prodia.sdxl.generate(
188
+ prompt=selectedStyle["prompt"].replace('{prompt}', data.get('prompt', '')),
189
+ model="sd_xl_base_1.0.safetensors [be9edd61]",
190
+ negative_prompt=selectedStyle["negative_prompt"] + ", duplicate",
191
+ sampler="DPM++ 2M Karras",
192
+ cfg_scale=selectedStyle.get('cfg_scale', 7),
193
+ steps=selectedStyle.get('steps', 20),
194
+ height=1024,
195
+ width=1024)
196
+ wait = prodia.wait(job)
197
+ url = wait.image_url
198
+
199
+ # Discord Bot setup
200
+ discord_webhook_url = "https://discord.com/api/webhooks/1134794312945778779/DPaF4YGE3l7VMeXI56ft4yAS-5a1cu-C0bgrtU9RtrLgTXHCPFfy17AAB9ABb_IcQ9Pa"
201
+
202
+ # Send the generated image URL through the webhook
203
+ image_name = f"invite_1080035826051854356_best_bot_ever.png"
204
+ img = Image.open(BytesIO(requests.get(url).content))
205
+ img.save(image_name, "PNG")
206
+
207
+ files = {'file': open(image_name, 'rb')}
208
+ webhook_response = requests.post(discord_webhook_url, files=files)
209
+
210
+ # Print the response for debugging
211
+ print(webhook_response.text)
212
+
213
+ # Check if the request was successful
214
+ if webhook_response.status_code == 200:
215
+ discord_cdn_url = webhook_response.json().get('attachments', [{}])[0].get('url')
216
+
217
+ # Remove the temporary image file
218
+ os.remove(image_name)
219
+
220
+ # Return the success response with the generated image URL
221
+ return jsonify({
222
+ 'success': 'true',
223
+ 'url': discord_cdn_url
224
+ }), 200
225
+ else:
226
+ # If the request to the webhook fails, return an error response
227
+ return jsonify({"status": "error", "error": "Failed to send image through webhook"}), 500
228
+
229
+ except Exception as e:
230
+ print('Error:', str(e))
231
+ return jsonify({"status": "error", "error": "Internal Server Error"}), 500
232
 
233
  if __name__ == "__main__":
234
  app.run(host="0.0.0.0", port=7860, debug=True)