Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,8 +1,6 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import google.generativeai as genai
|
| 3 |
import requests
|
| 4 |
-
from PIL import Image
|
| 5 |
-
import io
|
| 6 |
import logging
|
| 7 |
|
| 8 |
# Set up logging
|
|
@@ -58,34 +56,27 @@ 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/
|
| 62 |
|
| 63 |
headers = {
|
| 64 |
-
"Accept": "image
|
| 65 |
"Authorization": f"Bearer {stability_api_key}"
|
| 66 |
}
|
| 67 |
|
| 68 |
-
|
| 69 |
"prompt": f"{enhanced_prompt}, Style: {style}",
|
| 70 |
"negative_prompt": negative_prompt,
|
| 71 |
-
"
|
| 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:
|
| 82 |
-
response = requests.post(url, headers=headers,
|
| 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')
|
| 89 |
return response.content
|
| 90 |
else:
|
| 91 |
error_message = response.text
|
|
@@ -102,17 +93,17 @@ def process_and_generate(google_api_key, stability_api_key, prompt, style, negat
|
|
| 102 |
image_bytes = generate_image(stability_api_key, enhanced_prompt, style, negative_prompt)
|
| 103 |
|
| 104 |
# Save image to a file
|
| 105 |
-
with open("generated_image.
|
| 106 |
f.write(image_bytes)
|
| 107 |
|
| 108 |
# Return the file path and enhanced prompt
|
| 109 |
-
return "generated_image.
|
| 110 |
except Exception as e:
|
| 111 |
logging.error(f"Error in process_and_generate: {str(e)}")
|
| 112 |
return str(e), enhanced_prompt if 'enhanced_prompt' in locals() else "Error occurred before prompt enhancement"
|
| 113 |
|
| 114 |
with gr.Blocks() as demo:
|
| 115 |
-
gr.Markdown("# Stability AI Image Generator with Google Gemini Prompt Enhancement")
|
| 116 |
|
| 117 |
with gr.Row():
|
| 118 |
with gr.Column(scale=1):
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import google.generativeai as genai
|
| 3 |
import requests
|
|
|
|
|
|
|
| 4 |
import logging
|
| 5 |
|
| 6 |
# Set up logging
|
|
|
|
| 56 |
raise
|
| 57 |
|
| 58 |
def generate_image(stability_api_key, enhanced_prompt, style, negative_prompt):
|
| 59 |
+
url = "https://api.stability.ai/v2beta/stable-image/generate/ultra"
|
| 60 |
|
| 61 |
headers = {
|
| 62 |
+
"Accept": "image/*",
|
| 63 |
"Authorization": f"Bearer {stability_api_key}"
|
| 64 |
}
|
| 65 |
|
| 66 |
+
data = {
|
| 67 |
"prompt": f"{enhanced_prompt}, Style: {style}",
|
| 68 |
"negative_prompt": negative_prompt,
|
| 69 |
+
"output_format": "webp"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 70 |
}
|
| 71 |
|
| 72 |
try:
|
| 73 |
+
response = requests.post(url, headers=headers, files={"none": ''}, data=data)
|
| 74 |
response.raise_for_status()
|
| 75 |
|
| 76 |
logging.debug(f"Response headers: {response.headers}")
|
| 77 |
logging.debug(f"Response content type: {response.headers.get('content-type')}")
|
| 78 |
|
| 79 |
+
if response.headers.get('content-type').startswith('image/'):
|
| 80 |
return response.content
|
| 81 |
else:
|
| 82 |
error_message = response.text
|
|
|
|
| 93 |
image_bytes = generate_image(stability_api_key, enhanced_prompt, style, negative_prompt)
|
| 94 |
|
| 95 |
# Save image to a file
|
| 96 |
+
with open("generated_image.webp", "wb") as f:
|
| 97 |
f.write(image_bytes)
|
| 98 |
|
| 99 |
# Return the file path and enhanced prompt
|
| 100 |
+
return "generated_image.webp", enhanced_prompt
|
| 101 |
except Exception as e:
|
| 102 |
logging.error(f"Error in process_and_generate: {str(e)}")
|
| 103 |
return str(e), enhanced_prompt if 'enhanced_prompt' in locals() else "Error occurred before prompt enhancement"
|
| 104 |
|
| 105 |
with gr.Blocks() as demo:
|
| 106 |
+
gr.Markdown("# Stability AI Ultra Image Generator with Google Gemini Prompt Enhancement")
|
| 107 |
|
| 108 |
with gr.Row():
|
| 109 |
with gr.Column(scale=1):
|