Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -3,8 +3,8 @@ import os
|
|
3 |
import mimetypes
|
4 |
import tempfile
|
5 |
import time
|
6 |
-
|
7 |
-
from google.
|
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 |
-
|
24 |
-
model = "gemini-2.0-flash-exp-image-generation"
|
25 |
|
26 |
-
# Create
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
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 |
-
#
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
)
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
|
|
|
|
|
|
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
|