bluenevus commited on
Commit
cc9c490
·
verified ·
1 Parent(s): 97a72e4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +32 -23
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
- model = genai.GenerativeModel("gemini-2.0-flash")
32
- full_prompt = f"{enhanced_prompt}\nStyle: {style}\nNegative prompt: {negative_prompt}"
33
 
34
- response = model.generate_content(full_prompt)
 
 
 
 
35
 
36
- if response.parts:
37
- for part in response.parts:
38
- if part.image:
39
- image_bytes = part.image.to_bytes()
40
- image = Image.open(io.BytesIO(image_bytes))
41
-
42
- # Convert PIL Image to base64 string
43
- buffered = io.BytesIO()
44
- image.save(buffered, format="PNG")
45
- img_str = base64.b64encode(buffered.getvalue()).decode()
46
-
47
- return f"data:image/png;base64,{img_str}"
48
 
49
- return "Image generation failed"
 
 
 
 
 
 
50
 
51
- def process_and_generate(api_key, prompt, style, negative_prompt):
52
- genai.configure(api_key=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("# Google Imagen 3 Image Generator")
60
 
61
  with gr.Row():
62
  with gr.Column(scale=1):
63
- api_key = gr.Textbox(label="Google AI API Key", type="password")
 
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=[api_key, prompt, style, negative_prompt],
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