FelixNg21 commited on
Commit
715ddef
·
1 Parent(s): 5e333b2

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +48 -0
app.py ADDED
@@ -0,0 +1,48 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # import random
2
+ # from flask import Flask, request, jsonify, send_file
3
+ # from flask_cors import CORS
4
+ # from diffusers import StableDiffusionPipeline, AutoPipelineForText2Image
5
+ # import torch
6
+ from fastapi import FastAPI
7
+ import requests
8
+
9
+ # app = Flask(__name__)
10
+ app = FastAPI()
11
+ # CORS(app)
12
+
13
+ pipeline = AutoPipelineForText2Image.from_pretrained(
14
+ "stabilityai/stable-diffusion-2", variant="fp16", use_safetensors=True
15
+ )
16
+
17
+
18
+ @app.route('/image', methods=['GET', 'POST'])
19
+ def generate_image():
20
+ print("generating image")
21
+ data = request.json
22
+ prompt = data.get('prompt')
23
+ print(prompt)
24
+
25
+ # pipeline = StableDiffusionPipeline.from_single_file('./models/768-v-ema.safetensors', torch_dtype=torch.float32, variants='fp16', use_safetensors=True)
26
+
27
+ # takes around 15-35s/iteration (and there's 10 iterations/num_inference_steps at the moment) to generate an image, so maybe we can generate a progress bar to simulate this time??
28
+
29
+ index = random.randint(0, 1000)
30
+ image_generated_path = 'image_generated_' + str(index) + '.png'
31
+
32
+ image = pipeline(prompt=prompt, num_inference_steps=10).images[0]
33
+ # image_generated_path = 'image_' + prompt + '.png'
34
+ image.save("image_generated.png")
35
+ # image_generated_path = 'image_generated_525.png'
36
+
37
+ # buffered = BytesIO()
38
+ # image.save(buffered, format="PNG")
39
+ # encoded_image = base64.b64encode(buffered.getvalue()).decode('utf-8')
40
+ # return jsonify({'image_generated_path': image_generated_path}), 201
41
+ # return jsonify({'image_generated_path': image_generated_path, 'image_base64': encoded_image}), 201
42
+ return send_file("image_generated.png", mimetype='image/png', as_attachment=True), 201
43
+
44
+
45
+ # if __name__ == '__main__':
46
+ # host = '127.0.0.1'
47
+ # port = 5000
48
+ # app.run(debug=True)