Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,6 +3,10 @@ import google.generativeai as genai
|
|
| 3 |
import requests
|
| 4 |
from PIL import Image
|
| 5 |
import io
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
|
| 7 |
# List of popular styles
|
| 8 |
STYLES = [
|
|
@@ -37,22 +41,27 @@ def enhance_prompt(google_api_key, prompt, style):
|
|
| 37 |
Enhanced prompt:
|
| 38 |
"""
|
| 39 |
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
|
| 51 |
def generate_image(stability_api_key, enhanced_prompt, style, negative_prompt):
|
| 52 |
url = "https://api.stability.ai/v2beta/stable-image/generate/sd3"
|
| 53 |
|
| 54 |
headers = {
|
| 55 |
-
"Accept": "image
|
| 56 |
"Authorization": f"Bearer {stability_api_key}"
|
| 57 |
}
|
| 58 |
|
|
@@ -66,24 +75,41 @@ def generate_image(stability_api_key, enhanced_prompt, style, negative_prompt):
|
|
| 66 |
"steps": (None, "30"),
|
| 67 |
"cfg_scale": (None, "7.5"),
|
| 68 |
"seed": (None, "0"),
|
| 69 |
-
"output_format": (None, "
|
| 70 |
}
|
| 71 |
|
| 72 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 73 |
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
raise Exception(f"Image generation failed: {response.text}")
|
| 78 |
|
| 79 |
def process_and_generate(google_api_key, stability_api_key, prompt, style, negative_prompt):
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 87 |
|
| 88 |
with gr.Blocks() as demo:
|
| 89 |
gr.Markdown("# Stability AI Image Generator with Google Gemini Prompt Enhancement")
|
|
@@ -98,7 +124,7 @@ with gr.Blocks() as demo:
|
|
| 98 |
submit_btn = gr.Button("Generate Image")
|
| 99 |
|
| 100 |
with gr.Column(scale=1):
|
| 101 |
-
image_output = gr.Image(label="Generated Image")
|
| 102 |
enhanced_prompt_output = gr.Textbox(label="Enhanced Prompt")
|
| 103 |
|
| 104 |
submit_btn.click(
|
|
|
|
| 3 |
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')
|
| 10 |
|
| 11 |
# List of popular styles
|
| 12 |
STYLES = [
|
|
|
|
| 41 |
Enhanced prompt:
|
| 42 |
"""
|
| 43 |
|
| 44 |
+
try:
|
| 45 |
+
response = model.generate_content(enhanced_prompt_request)
|
| 46 |
+
|
| 47 |
+
enhanced_prompt = response.text.strip()
|
| 48 |
+
|
| 49 |
+
prefixes_to_remove = ["Enhanced prompt:", "Here's the enhanced prompt:", "The enhanced prompt is:"]
|
| 50 |
+
for prefix in prefixes_to_remove:
|
| 51 |
+
if enhanced_prompt.lower().startswith(prefix.lower()):
|
| 52 |
+
enhanced_prompt = enhanced_prompt[len(prefix):].strip()
|
| 53 |
+
|
| 54 |
+
logging.info(f"Enhanced prompt: {enhanced_prompt}")
|
| 55 |
+
return enhanced_prompt
|
| 56 |
+
except Exception as e:
|
| 57 |
+
logging.error(f"Error in enhance_prompt: {str(e)}")
|
| 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 |
|
|
|
|
| 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}")
|
| 93 |
+
raise Exception(f"Unexpected content type: {response.headers.get('content-type')}. Response: {error_message}")
|
| 94 |
|
| 95 |
+
except requests.exceptions.RequestException as e:
|
| 96 |
+
logging.error(f"Request failed: {str(e)}")
|
| 97 |
+
raise Exception(f"Request failed: {str(e)}")
|
|
|
|
| 98 |
|
| 99 |
def process_and_generate(google_api_key, stability_api_key, prompt, style, negative_prompt):
|
| 100 |
+
try:
|
| 101 |
+
enhanced_prompt = enhance_prompt(google_api_key, prompt, style)
|
| 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.jpg", "wb") as f:
|
| 106 |
+
f.write(image_bytes)
|
| 107 |
+
|
| 108 |
+
# Return the file path and enhanced prompt
|
| 109 |
+
return "generated_image.jpg", enhanced_prompt
|
| 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")
|
|
|
|
| 124 |
submit_btn = gr.Button("Generate Image")
|
| 125 |
|
| 126 |
with gr.Column(scale=1):
|
| 127 |
+
image_output = gr.Image(label="Generated Image", type="filepath")
|
| 128 |
enhanced_prompt_output = gr.Textbox(label="Enhanced Prompt")
|
| 129 |
|
| 130 |
submit_btn.click(
|