bluenevus commited on
Commit
56f7197
·
verified ·
1 Parent(s): 3f649de

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +26 -20
app.py CHANGED
@@ -1,6 +1,9 @@
1
  import gradio as gr
2
- from openai import OpenAI
3
- import httpx
 
 
 
4
 
5
  # List of popular styles
6
  STYLES = [
@@ -18,43 +21,46 @@ cut off, low contrast, underexposed, overexposed, bad art, beginner, amateur, di
18
  """
19
 
20
  def enhance_prompt(client, prompt, style):
21
- enhanced_prompt_request = f"Enhance the following prompt for DALL-E 3 image generation in the style of {style}. Make it more detailed and vivid, while keeping the original intent: '{prompt}'"
 
22
 
23
- response = client.chat.completions.create(
24
- model="gpt-4",
25
- messages=[
26
- {"role": "system", "content": "You are an expert at creating detailed, vivid prompts for image generation."},
27
- {"role": "user", "content": enhanced_prompt_request}
28
- ]
29
- )
30
 
31
- return response.choices[0].message.content.strip()
32
 
33
  def generate_image(client, enhanced_prompt, style, negative_prompt):
34
  full_prompt = f"{enhanced_prompt}\nStyle: {style}\nNegative prompt: {negative_prompt}"
35
 
36
- response = client.images.generate(
37
- model="dall-e-3",
38
  prompt=full_prompt,
39
- size="1024x1024",
40
- quality="standard",
41
- n=1
42
  )
43
 
44
- return response.data[0].url
 
 
 
 
 
 
 
 
45
 
46
  def process_and_generate(api_key, prompt, style, negative_prompt):
47
- client = OpenAI(api_key=api_key, http_client=httpx.Client())
48
  enhanced_prompt = enhance_prompt(client, prompt, style)
49
  image_url = generate_image(client, enhanced_prompt, style, negative_prompt)
50
  return image_url, enhanced_prompt
51
 
52
  with gr.Blocks() as demo:
53
- gr.Markdown("# DALL-E 3 Image Generator")
54
 
55
  with gr.Row():
56
  with gr.Column(scale=1):
57
- api_key = gr.Textbox(label="OpenAI API Key", type="password")
58
  prompt = gr.Textbox(label="Prompt")
59
  style = gr.Dropdown(label="Style", choices=STYLES)
60
  negative_prompt = gr.Textbox(label="Negative Prompt", value=DEFAULT_NEGATIVE_PROMPT)
 
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
7
 
8
  # List of popular styles
9
  STYLES = [
 
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 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)