Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -9,12 +9,16 @@ def save_binary_file(file_name, data):
|
|
| 9 |
with open(file_name, "wb") as f:
|
| 10 |
f.write(data)
|
| 11 |
|
| 12 |
-
def generate_edit(prompt,
|
| 13 |
# Initialize Gemini client with provided key or env fallback
|
| 14 |
client = genai.Client(api_key=(api_key.strip() if api_key and api_key.strip() != "" else os.environ.get("GEMINI_API_KEY")))
|
| 15 |
|
| 16 |
-
#
|
| 17 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
contents = [
|
| 19 |
types.Content(
|
| 20 |
role="user",
|
|
@@ -25,23 +29,20 @@ def generate_edit(prompt, image, api_key, model="gemini-2.0-flash-exp"):
|
|
| 25 |
),
|
| 26 |
]
|
| 27 |
|
| 28 |
-
# Configure generation with text and image response modalities
|
| 29 |
generate_content_config = types.GenerateContentConfig(
|
| 30 |
temperature=1,
|
| 31 |
top_p=0.95,
|
| 32 |
top_k=40,
|
| 33 |
max_output_tokens=8192,
|
| 34 |
response_modalities=["image", "text"],
|
| 35 |
-
response_mime_type="text/plain", # Important
|
| 36 |
)
|
| 37 |
|
| 38 |
text_response = ""
|
| 39 |
-
|
| 40 |
|
| 41 |
-
# Temporary file to write streamed image data
|
| 42 |
with tempfile.NamedTemporaryFile(suffix=".png", delete=False) as tmp:
|
| 43 |
temp_path = tmp.name
|
| 44 |
-
# Stream generation content, check each chunk for inline image data or text
|
| 45 |
for chunk in client.models.generate_content_stream(
|
| 46 |
model=model,
|
| 47 |
contents=contents,
|
|
@@ -52,12 +53,13 @@ def generate_edit(prompt, image, api_key, model="gemini-2.0-flash-exp"):
|
|
| 52 |
candidate = chunk.candidates[0].content.parts[0]
|
| 53 |
if candidate.inline_data:
|
| 54 |
save_binary_file(temp_path, candidate.inline_data.data)
|
| 55 |
-
|
| 56 |
-
break # Stop
|
| 57 |
else:
|
| 58 |
text_response += chunk.text + "\n"
|
| 59 |
del files
|
| 60 |
-
|
|
|
|
| 61 |
|
| 62 |
def process_image_and_prompt(pil_image, prompt, api_key):
|
| 63 |
try:
|
|
|
|
| 9 |
with open(file_name, "wb") as f:
|
| 10 |
f.write(data)
|
| 11 |
|
| 12 |
+
def generate_edit(prompt, pil_image, api_key, model="gemini-2.0-flash-exp"):
|
| 13 |
# Initialize Gemini client with provided key or env fallback
|
| 14 |
client = genai.Client(api_key=(api_key.strip() if api_key and api_key.strip() != "" else os.environ.get("GEMINI_API_KEY")))
|
| 15 |
|
| 16 |
+
# Save PIL Image to a temporary file path for upload
|
| 17 |
+
with tempfile.NamedTemporaryFile(suffix=".png", delete=False) as tmp_file:
|
| 18 |
+
image_path = tmp_file.name
|
| 19 |
+
pil_image.save(image_path)
|
| 20 |
+
|
| 21 |
+
files = [client.files.upload(file=image_path)]
|
| 22 |
contents = [
|
| 23 |
types.Content(
|
| 24 |
role="user",
|
|
|
|
| 29 |
),
|
| 30 |
]
|
| 31 |
|
|
|
|
| 32 |
generate_content_config = types.GenerateContentConfig(
|
| 33 |
temperature=1,
|
| 34 |
top_p=0.95,
|
| 35 |
top_k=40,
|
| 36 |
max_output_tokens=8192,
|
| 37 |
response_modalities=["image", "text"],
|
| 38 |
+
response_mime_type="text/plain", # Important for streaming image inline data
|
| 39 |
)
|
| 40 |
|
| 41 |
text_response = ""
|
| 42 |
+
image_path_result = None
|
| 43 |
|
|
|
|
| 44 |
with tempfile.NamedTemporaryFile(suffix=".png", delete=False) as tmp:
|
| 45 |
temp_path = tmp.name
|
|
|
|
| 46 |
for chunk in client.models.generate_content_stream(
|
| 47 |
model=model,
|
| 48 |
contents=contents,
|
|
|
|
| 53 |
candidate = chunk.candidates[0].content.parts[0]
|
| 54 |
if candidate.inline_data:
|
| 55 |
save_binary_file(temp_path, candidate.inline_data.data)
|
| 56 |
+
image_path_result = temp_path
|
| 57 |
+
break # Stop on first image data chunk
|
| 58 |
else:
|
| 59 |
text_response += chunk.text + "\n"
|
| 60 |
del files
|
| 61 |
+
|
| 62 |
+
return image_path_result, text_response
|
| 63 |
|
| 64 |
def process_image_and_prompt(pil_image, prompt, api_key):
|
| 65 |
try:
|