bluenevus commited on
Commit
832844e
·
verified ·
1 Parent(s): 23464d1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +14 -26
app.py CHANGED
@@ -4,7 +4,6 @@ import requests
4
  from PIL import Image
5
  import io
6
  import logging
7
- import base64
8
 
9
  # Set up logging
10
  logging.basicConfig(level=logging.DEBUG, format='%(asctime)s - %(levelname)s - %(message)s')
@@ -59,30 +58,24 @@ def enhance_prompt(google_api_key, prompt, style):
59
  raise
60
 
61
  def generate_image(stability_api_key, enhanced_prompt, style, negative_prompt):
62
- url = "https://api.stability.ai/v1/generation/stable-diffusion-v1-5/text-to-image"
63
 
64
  headers = {
65
- "Content-Type": "application/json",
66
- "Accept": "application/json",
67
  "Authorization": f"Bearer {stability_api_key}"
68
  }
69
 
70
  payload = {
71
- "text_prompts": [
72
- {
73
- "text": f"{enhanced_prompt}, Style: {style}",
74
- "weight": 1
75
- },
76
- {
77
- "text": negative_prompt,
78
- "weight": -1
79
- }
80
- ],
81
- "cfg_scale": 7,
82
- "height": 512,
83
- "width": 512,
84
- "samples": 1,
85
- "steps": 30
86
  }
87
 
88
  try:
@@ -92,13 +85,8 @@ def generate_image(stability_api_key, enhanced_prompt, style, negative_prompt):
92
  logging.debug(f"Response headers: {response.headers}")
93
  logging.debug(f"Response content type: {response.headers.get('content-type')}")
94
 
95
- if response.headers.get('content-type') == 'application/json':
96
- result = response.json()
97
- if 'artifacts' in result and len(result['artifacts']) > 0:
98
- image_data = result['artifacts'][0]['base64']
99
- return base64.b64decode(image_data)
100
- else:
101
- raise Exception("No image data found in the response")
102
  else:
103
  error_message = response.text
104
  logging.error(f"Unexpected content type: {response.headers.get('content-type')}. Response: {error_message}")
 
4
  from PIL import Image
5
  import io
6
  import logging
 
7
 
8
  # Set up logging
9
  logging.basicConfig(level=logging.DEBUG, format='%(asctime)s - %(levelname)s - %(message)s')
 
58
  raise
59
 
60
  def generate_image(stability_api_key, enhanced_prompt, style, negative_prompt):
61
+ url = "https://api.stability.ai/v2beta/stable-image/generate/sd3"
62
 
63
  headers = {
64
+ "Accept": "image/jpeg",
 
65
  "Authorization": f"Bearer {stability_api_key}"
66
  }
67
 
68
  payload = {
69
+ "prompt": f"{enhanced_prompt}, Style: {style}",
70
+ "negative_prompt": negative_prompt,
71
+ "model": "sd3.5-large-turbo",
72
+ "width": 1024,
73
+ "height": 1024,
74
+ "num_images": 1,
75
+ "steps": 30,
76
+ "cfg_scale": 7.5,
77
+ "seed": 0,
78
+ "output_format": "jpeg"
 
 
 
 
 
79
  }
80
 
81
  try:
 
85
  logging.debug(f"Response headers: {response.headers}")
86
  logging.debug(f"Response content type: {response.headers.get('content-type')}")
87
 
88
+ if response.headers.get('content-type') == 'image/jpeg':
89
+ return response.content
 
 
 
 
 
90
  else:
91
  error_message = response.text
92
  logging.error(f"Unexpected content type: {response.headers.get('content-type')}. Response: {error_message}")