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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +28 -16
app.py CHANGED
@@ -4,6 +4,7 @@ import requests
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,35 +59,46 @@ def enhance_prompt(google_api_key, prompt, style):
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
- files = {
69
- "prompt": (None, f"{enhanced_prompt}, Style: {style}"),
70
- "negative_prompt": (None, negative_prompt),
71
- "model": (None, "sd3.5-large-turbo"),
72
- "width": (None, "1024"),
73
- "height": (None, "1024"),
74
- "num_images": (None, "1"),
75
- "steps": (None, "30"),
76
- "cfg_scale": (None, "7.5"),
77
- "seed": (None, "0"),
78
- "output_format": (None, "jpeg")
 
 
 
 
 
79
  }
80
 
81
  try:
82
- response = requests.post(url, headers=headers, files=files)
83
  response.raise_for_status()
84
 
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}")
 
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
  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:
89
+ response = requests.post(url, headers=headers, json=payload)
90
  response.raise_for_status()
91
 
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}")