Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
@@ -1,5 +1,6 @@
|
|
1 |
import gradio as gr
|
2 |
-
|
|
|
3 |
from PIL import Image
|
4 |
from io import BytesIO
|
5 |
import base64
|
@@ -19,40 +20,43 @@ extra limbs, disfigured, deformed, body out of frame, bad anatomy, watermark, si
|
|
19 |
cut off, low contrast, underexposed, overexposed, bad art, beginner, amateur, distorted face
|
20 |
"""
|
21 |
|
22 |
-
def enhance_prompt(
|
|
|
23 |
enhanced_prompt_request = f"Enhance the following prompt for image generation in the style of {style}. Make it more detailed and vivid, while keeping the original intent: '{prompt}'"
|
24 |
|
25 |
response = model.generate_content(enhanced_prompt_request)
|
26 |
|
27 |
return response.text
|
28 |
|
29 |
-
def generate_image(
|
30 |
full_prompt = f"{enhanced_prompt}\nStyle: {style}\nNegative prompt: {negative_prompt}"
|
31 |
|
32 |
-
response =
|
|
|
|
|
|
|
|
|
|
|
|
|
33 |
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
return f"data:image/png;base64,{img_str}"
|
44 |
-
else:
|
45 |
-
return None
|
46 |
|
47 |
def process_and_generate(api_key, prompt, style, negative_prompt):
|
48 |
-
genai.
|
49 |
-
|
50 |
-
|
51 |
-
image_url = generate_image(model, enhanced_prompt, style, negative_prompt)
|
52 |
return image_url, enhanced_prompt
|
53 |
|
54 |
with gr.Blocks() as demo:
|
55 |
-
gr.Markdown("# Google
|
56 |
|
57 |
with gr.Row():
|
58 |
with gr.Column(scale=1):
|
|
|
1 |
import gradio as gr
|
2 |
+
from google import genai
|
3 |
+
from google.genai import types
|
4 |
from PIL import Image
|
5 |
from io import BytesIO
|
6 |
import base64
|
|
|
20 |
cut off, low contrast, underexposed, overexposed, bad art, beginner, amateur, distorted face
|
21 |
"""
|
22 |
|
23 |
+
def enhance_prompt(client, prompt, style):
|
24 |
+
model = client.get_model('gemini-pro')
|
25 |
enhanced_prompt_request = f"Enhance the following prompt for image generation in the style of {style}. Make it more detailed and vivid, while keeping the original intent: '{prompt}'"
|
26 |
|
27 |
response = model.generate_content(enhanced_prompt_request)
|
28 |
|
29 |
return response.text
|
30 |
|
31 |
+
def generate_image(client, enhanced_prompt, style, negative_prompt):
|
32 |
full_prompt = f"{enhanced_prompt}\nStyle: {style}\nNegative prompt: {negative_prompt}"
|
33 |
|
34 |
+
response = client.models.generate_images(
|
35 |
+
model='imagen-3.0-generate-002',
|
36 |
+
prompt=full_prompt,
|
37 |
+
config=types.GenerateImagesConfig(
|
38 |
+
number_of_images=1,
|
39 |
+
)
|
40 |
+
)
|
41 |
|
42 |
+
image_bytes = response.generated_images[0].image.image_bytes
|
43 |
+
image = Image.open(BytesIO(image_bytes))
|
44 |
+
|
45 |
+
# Convert PIL Image to base64 string
|
46 |
+
buffered = BytesIO()
|
47 |
+
image.save(buffered, format="PNG")
|
48 |
+
img_str = base64.b64encode(buffered.getvalue()).decode()
|
49 |
+
|
50 |
+
return f"data:image/png;base64,{img_str}"
|
|
|
|
|
|
|
51 |
|
52 |
def process_and_generate(api_key, prompt, style, negative_prompt):
|
53 |
+
client = genai.Client(api_key=api_key)
|
54 |
+
enhanced_prompt = enhance_prompt(client, prompt, style)
|
55 |
+
image_url = generate_image(client, 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):
|