Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,6 +3,7 @@ import google.generativeai as genai
|
|
| 3 |
from PIL import Image
|
| 4 |
import io
|
| 5 |
import base64
|
|
|
|
| 6 |
|
| 7 |
# List of popular styles
|
| 8 |
STYLES = [
|
|
@@ -27,40 +28,48 @@ def enhance_prompt(prompt, style):
|
|
| 27 |
|
| 28 |
return response.text
|
| 29 |
|
| 30 |
-
def generate_image(enhanced_prompt, style, negative_prompt):
|
| 31 |
-
|
| 32 |
-
full_prompt = f"{enhanced_prompt}\nStyle: {style}\nNegative prompt: {negative_prompt}"
|
| 33 |
|
| 34 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
return f"data:image/png;base64,{img_str}"
|
| 48 |
|
| 49 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
|
| 51 |
-
def process_and_generate(
|
| 52 |
-
genai.configure(api_key=
|
| 53 |
|
| 54 |
enhanced_prompt = enhance_prompt(prompt, style)
|
| 55 |
-
image_url = generate_image(enhanced_prompt, style, negative_prompt)
|
| 56 |
return image_url, enhanced_prompt
|
| 57 |
|
| 58 |
with gr.Blocks() as demo:
|
| 59 |
-
gr.Markdown("#
|
| 60 |
|
| 61 |
with gr.Row():
|
| 62 |
with gr.Column(scale=1):
|
| 63 |
-
|
|
|
|
| 64 |
prompt = gr.Textbox(label="Prompt")
|
| 65 |
style = gr.Dropdown(label="Style", choices=STYLES)
|
| 66 |
negative_prompt = gr.Textbox(label="Negative Prompt", value=DEFAULT_NEGATIVE_PROMPT)
|
|
@@ -72,7 +81,7 @@ with gr.Blocks() as demo:
|
|
| 72 |
|
| 73 |
generate_btn.click(
|
| 74 |
process_and_generate,
|
| 75 |
-
inputs=[
|
| 76 |
outputs=[image_output, enhanced_prompt_output]
|
| 77 |
)
|
| 78 |
|
|
|
|
| 3 |
from PIL import Image
|
| 4 |
import io
|
| 5 |
import base64
|
| 6 |
+
import requests
|
| 7 |
|
| 8 |
# List of popular styles
|
| 9 |
STYLES = [
|
|
|
|
| 28 |
|
| 29 |
return response.text
|
| 30 |
|
| 31 |
+
def generate_image(stability_api_key, enhanced_prompt, style, negative_prompt):
|
| 32 |
+
url = "https://api.stability.ai/v2beta/stable-image/generate/sd3"
|
|
|
|
| 33 |
|
| 34 |
+
headers = {
|
| 35 |
+
"Accept": "application/json",
|
| 36 |
+
"Content-Type": "application/json",
|
| 37 |
+
"Authorization": f"Bearer {stability_api_key}"
|
| 38 |
+
}
|
| 39 |
|
| 40 |
+
payload = {
|
| 41 |
+
"model": "sd3.5-large-turbo",
|
| 42 |
+
"prompt": f"{enhanced_prompt}, Style: {style}",
|
| 43 |
+
"negative_prompt": negative_prompt,
|
| 44 |
+
"width": 1024,
|
| 45 |
+
"height": 1024,
|
| 46 |
+
"num_images": 1,
|
| 47 |
+
"steps": 30,
|
| 48 |
+
"cfg_scale": 7.5
|
| 49 |
+
}
|
|
|
|
|
|
|
| 50 |
|
| 51 |
+
response = requests.post(url, json=payload, headers=headers)
|
| 52 |
+
|
| 53 |
+
if response.status_code == 200:
|
| 54 |
+
image_data = response.json()['images'][0]['image_base64']
|
| 55 |
+
return f"data:image/png;base64,{image_data}"
|
| 56 |
+
else:
|
| 57 |
+
return f"Image generation failed: {response.text}"
|
| 58 |
|
| 59 |
+
def process_and_generate(google_api_key, stability_api_key, prompt, style, negative_prompt):
|
| 60 |
+
genai.configure(api_key=google_api_key)
|
| 61 |
|
| 62 |
enhanced_prompt = enhance_prompt(prompt, style)
|
| 63 |
+
image_url = generate_image(stability_api_key, enhanced_prompt, style, negative_prompt)
|
| 64 |
return image_url, enhanced_prompt
|
| 65 |
|
| 66 |
with gr.Blocks() as demo:
|
| 67 |
+
gr.Markdown("# Stability AI Image Generator with Google Gemini Prompt Enhancement")
|
| 68 |
|
| 69 |
with gr.Row():
|
| 70 |
with gr.Column(scale=1):
|
| 71 |
+
google_api_key = gr.Textbox(label="Google AI API Key", type="password")
|
| 72 |
+
stability_api_key = gr.Textbox(label="Stability AI API Key", type="password")
|
| 73 |
prompt = gr.Textbox(label="Prompt")
|
| 74 |
style = gr.Dropdown(label="Style", choices=STYLES)
|
| 75 |
negative_prompt = gr.Textbox(label="Negative Prompt", value=DEFAULT_NEGATIVE_PROMPT)
|
|
|
|
| 81 |
|
| 82 |
generate_btn.click(
|
| 83 |
process_and_generate,
|
| 84 |
+
inputs=[google_api_key, stability_api_key, prompt, style, negative_prompt],
|
| 85 |
outputs=[image_output, enhanced_prompt_output]
|
| 86 |
)
|
| 87 |
|