jayendra19 commited on
Commit
404f0d6
·
verified ·
1 Parent(s): ca868de

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +60 -25
app.py CHANGED
@@ -1,6 +1,6 @@
1
- import base64
2
- import re
3
- import os
4
  import random
5
  import requests
6
  import time
@@ -12,6 +12,8 @@ import torch
12
  from flask import Flask, request, jsonify
13
  app = Flask(__name__)
14
 
 
 
15
  style_list = [
16
  {
17
  "name": "(No style)",
@@ -20,7 +22,7 @@ style_list = [
20
  },
21
  {
22
  "name": "Cinematic",
23
- "prompt": "cinematic still {prompt} . emotional, harmonious, vignette, highly detailed, high budget, bokeh, cinemascope, moody, epic, gorgeous, film grain, grainy",
24
  "negative_prompt": "anime, cartoon, graphic, text, painting, crayon, graphite, abstract, glitch, deformed, mutated, ugly, disfigured",
25
  },
26
  {
@@ -63,33 +65,62 @@ style_list = [
63
  "prompt": "professional 3d model {prompt} . octane render, highly detailed, volumetric, dramatic lighting",
64
  "negative_prompt": "ugly, deformed, noisy, low poly, blurry, painting",
65
  },
 
 
 
 
 
66
  ]
67
 
68
- def infer(prompt, negative="low_quality", style_name=None, guidance_scale=None):
 
 
 
 
 
 
69
 
70
 
71
- seed = random.randint(0,4294967295)
72
- prompt, negative = apply_style(style_name, prompt, negative)
73
- print(prompt)
 
 
74
  print(negative)
75
 
76
- # Load the Stable Diffusion model
77
- pipe = DiffusionPipeline.from_pretrained("stabilityai/stable-diffusion-xl-base-1.0", torch_dtype=torch.float16, use_safetensors=True,variant="fp16")
78
- pipe.to("cuda")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
79
 
80
- # Generate the images
81
- images = pipe(prompt=prompt, negative_prompt=negative, guidance_scale=guidance_scale, seed=seed).images
82
 
83
- # Convert the images to base64-encoded strings
84
- image_urls = []
85
- for i, image in enumerate(images):
86
- buffered = BytesIO()
87
- image.save(buffered, format="JPEG")
88
- image_b64 = base64.b64encode(buffered.getvalue()).decode("utf-8")
89
- #image_url = f"data:image/jpeg;base64,{image_b64}"
90
- #image_urls.append(image_url)
91
 
92
- return image_b64
93
 
94
 
95
  @app.route('/', methods=['POST'])
@@ -100,15 +131,19 @@ def generate_image():
100
  style_name = data['style_name']
101
  guidance_scale = data['guidance_scale']
102
 
103
- image_urls = infer(prompt, style_name=style_name, guidance_scale=guidance_scale)
104
 
105
  # Convert the first generated image to base64
106
- image_b64 = image_urls[0].split(",")[1]
107
 
108
- return jsonify({'image_base64': image_b64})
 
109
  else:
110
  return jsonify({'error': 'Missing required parameters'}), 400
111
 
112
 
 
 
 
113
  if __name__ == '__main__':
114
  app.run(host='0.0.0.0',port=7860,debug=True)
 
1
+ import requests
2
+ import random
3
+ from typing import Tuple
4
  import random
5
  import requests
6
  import time
 
12
  from flask import Flask, request, jsonify
13
  app = Flask(__name__)
14
 
15
+
16
+
17
  style_list = [
18
  {
19
  "name": "(No style)",
 
22
  },
23
  {
24
  "name": "Cinematic",
25
+ "prompt": "cinematic still {prompt} . emotional, harmonious, vignette, highly detailed, high budget, bokeh,realistic,textured, cinemascope, moody, epic, gorgeous, film grain, grainy",
26
  "negative_prompt": "anime, cartoon, graphic, text, painting, crayon, graphite, abstract, glitch, deformed, mutated, ugly, disfigured",
27
  },
28
  {
 
65
  "prompt": "professional 3d model {prompt} . octane render, highly detailed, volumetric, dramatic lighting",
66
  "negative_prompt": "ugly, deformed, noisy, low poly, blurry, painting",
67
  },
68
+ {
69
+ "name":"Portrait",
70
+ "prompt":"Portrait Art {prompt}.soft light, sharp, exposure blend, medium shot, bokeh, (hdr:1.4), high contrast, (cinematic, teal and orange:0.85), (muted colors, dim colors, soothing tones:1.3), low saturation, (hyperdetailed:1.2), (noir:0.4), (natural skin texture, hyperrealism, soft light, sharp:1.2)",
71
+ "negative_prompt":""
72
+ }
73
  ]
74
 
75
+ styles = {k["name"]: (k["prompt"], k["negative_prompt"]) for k in style_list}
76
+ STYLE_NAMES = list(styles.keys())
77
+ DEFAULT_STYLE_NAME = "(No style)"
78
+
79
+ def apply_style(style_name: str, positive: str, negative: str = "") -> Tuple[str, str]:
80
+ p, n = styles.get(style_name, styles[DEFAULT_STYLE_NAME])
81
+ return p.replace("{prompt}", positive), n + negative
82
 
83
 
84
+ def text_to_image(prompt,guidance,style_name):
85
+ url = "https://api.getimg.ai/v1/stable-diffusion-xl/text-to-image"
86
+ #seed = random.randint(0,4294967295)
87
+ prompts, negative = apply_style(style_name, prompt)
88
+ print(prompts)
89
  print(negative)
90
 
91
+ payload = {
92
+ "model": "stable-diffusion-xl-v1-0",
93
+ "prompt": prompts,
94
+ "width": 1024,
95
+ "height": 1024,
96
+ "steps": 90,
97
+ "guidance": guidance,
98
+ "output_format": "jpeg",
99
+ "response_format": "url",
100
+ "negative_prompt": negative,
101
+
102
+
103
+ }
104
+ headers = {
105
+ "accept": "application/json",
106
+ "content-type": "application/json",
107
+ "authorization": "Bearer key-3ryTG9YlTHzB0hkjgFHoL7yhsOQkWUbodn1sOU7WwOkhDXNSWVL8heMXWwNqCliAbdpm9xNtrITY3WInbRdC98bZ5QVdfOoi"
108
+ }
109
+
110
+ response = requests.post(url, json=payload, headers=headers)
111
+ #image_url =response.json()['url']
112
+ # Fetch the image data directly from the URL
113
+ #image_datas = requests.get(image_url).content
114
+
115
+ # Save the image to a local file (e.g., 'my_image.jpg')
116
+ #with open('my_image.jpg', 'wb') as f:
117
+ # f.write(image_datas)
118
+
119
 
 
 
120
 
121
+ #print(response.text)
122
+ return response.text
 
 
 
 
 
 
123
 
 
124
 
125
 
126
  @app.route('/', methods=['POST'])
 
131
  style_name = data['style_name']
132
  guidance_scale = data['guidance_scale']
133
 
134
+ image_urls = text_to_image(prompt=prompt, style_name=style_name, guidance=guidance_scale)
135
 
136
  # Convert the first generated image to base64
137
+ #image_b64 = image_urls[0].split(",")[1]
138
 
139
+ #return jsonify({'image_base64': image_b64})
140
+ return jsonify({'image_urls':image_urls})
141
  else:
142
  return jsonify({'error': 'Missing required parameters'}), 400
143
 
144
 
145
+
146
+
147
+
148
  if __name__ == '__main__':
149
  app.run(host='0.0.0.0',port=7860,debug=True)