Mohit5899 commited on
Commit
62e0db2
·
verified ·
1 Parent(s): c0f459c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +38 -44
app.py CHANGED
@@ -3,8 +3,8 @@ import os
3
  import mimetypes
4
  import tempfile
5
  import time
6
- from google import genai
7
- from google.genai import types
8
  import gradio as gr
9
 
10
  def generate_image(api_key, prompt, file_name):
@@ -20,55 +20,49 @@ def generate_image(api_key, prompt, file_name):
20
 
21
  try:
22
  # Set up the client
23
- client = genai.Client(api_key=api_key)
24
- model = "gemini-2.0-flash-exp-image-generation"
25
 
26
- # Create content
27
- contents = [
28
- types.Content(
29
- role="user",
30
- parts=[types.Part.from_text(text=prompt)],
31
- ),
32
- ]
33
-
34
- # Configure generation settings
35
- generate_content_config = types.GenerateContentConfig(
36
- response_modalities=["image", "text"],
37
- safety_settings=[
38
- types.SafetySetting(category="HARM_CATEGORY_HARASSMENT", threshold="BLOCK_NONE"),
39
- types.SafetySetting(category="HARM_CATEGORY_HATE_SPEECH", threshold="BLOCK_NONE"),
40
- types.SafetySetting(category="HARM_CATEGORY_SEXUALLY_EXPLICIT", threshold="BLOCK_NONE"),
41
- types.SafetySetting(category="HARM_CATEGORY_DANGEROUS_CONTENT", threshold="BLOCK_NONE"),
42
- types.SafetySetting(category="HARM_CATEGORY_CIVIC_INTEGRITY", threshold="BLOCK_NONE"),
43
- ],
44
- response_mime_type="text/plain",
45
- )
46
 
47
  # Create a temporary directory to store the image
48
  temp_dir = tempfile.mkdtemp()
49
  generated_image_path = None
50
  generation_text = ""
51
 
52
- # Stream the content
53
- for chunk in client.models.generate_content_stream(
54
- model=model,
55
- contents=contents,
56
- config=generate_content_config,
57
- ):
58
- if not chunk.candidates or not chunk.candidates[0].content or not chunk.candidates[0].content.parts:
59
- continue
60
-
61
- if chunk.candidates[0].content.parts[0].inline_data:
62
- inline_data = chunk.candidates[0].content.parts[0].inline_data
63
- file_extension = mimetypes.guess_extension(inline_data.mime_type) or '.jpg'
64
- generated_image_path = os.path.join(temp_dir, f"{file_name}{file_extension}")
65
-
66
- with open(generated_image_path, "wb") as f:
67
- f.write(inline_data.data)
68
-
69
- generation_text += f"Image of type {inline_data.mime_type} generated successfully."
70
- else:
71
- generation_text += chunk.text
 
 
 
72
 
73
  if generated_image_path:
74
  return generated_image_path, generation_text
 
3
  import mimetypes
4
  import tempfile
5
  import time
6
+ import google.generativeai as genai
7
+ from google.generativeai import types
8
  import gradio as gr
9
 
10
  def generate_image(api_key, prompt, file_name):
 
20
 
21
  try:
22
  # Set up the client
23
+ genai.configure(api_key=api_key)
24
+ model = genai.GenerativeModel("gemini-2.0-flash-exp-image-generation")
25
 
26
+ # Create generation config
27
+ generation_config = {
28
+ "response_modalities": ["image", "text"],
29
+ "safety_settings": {
30
+ "HARM_CATEGORY_HARASSMENT": "BLOCK_NONE",
31
+ "HARM_CATEGORY_HATE_SPEECH": "BLOCK_NONE",
32
+ "HARM_CATEGORY_SEXUALLY_EXPLICIT": "BLOCK_NONE",
33
+ "HARM_CATEGORY_DANGEROUS_CONTENT": "BLOCK_NONE",
34
+ "HARM_CATEGORY_CIVIC_INTEGRITY": "BLOCK_NONE",
35
+ }
36
+ }
 
 
 
 
 
 
 
 
 
37
 
38
  # Create a temporary directory to store the image
39
  temp_dir = tempfile.mkdtemp()
40
  generated_image_path = None
41
  generation_text = ""
42
 
43
+ # Generate the content
44
+ response = model.generate_content(
45
+ prompt,
46
+ generation_config=generation_config,
47
+ stream=True
48
+ )
49
+
50
+ for chunk in response:
51
+ if hasattr(chunk, 'candidates') and chunk.candidates:
52
+ candidate = chunk.candidates[0]
53
+ if hasattr(candidate, 'content') and candidate.content:
54
+ for part in candidate.content.parts:
55
+ if hasattr(part, 'inline_data') and part.inline_data:
56
+ inline_data = part.inline_data
57
+ file_extension = mimetypes.guess_extension(inline_data.mime_type) or '.jpg'
58
+ generated_image_path = os.path.join(temp_dir, f"{file_name}{file_extension}")
59
+
60
+ with open(generated_image_path, "wb") as f:
61
+ f.write(inline_data.data)
62
+
63
+ generation_text += f"Image of type {inline_data.mime_type} generated successfully."
64
+ elif hasattr(part, 'text') and part.text:
65
+ generation_text += part.text
66
 
67
  if generated_image_path:
68
  return generated_image_path, generation_text