import gradio as gr import google.generativeai as genai import os from typing import Optional, List from huggingface_hub import whoami from PIL import Image import tempfile import io # Import io for handling in-memory binary streams # --- Google Gemini API Configuration --- # Set your Google API key as an environment variable GOOGLE_API_KEY = os.getenv("GOOGLE_API_KEY", "") if not GOOGLE_API_KEY: raise ValueError("GOOGLE_API_KEY environment variable not set.") genai.configure(api_key=GOOGLE_API_KEY) # --- Define the correct model name --- GEMINI_MODEL_NAME = 'gemini-2.5-flash-image-preview' def verify_pro_status(token: Optional[gr.OAuthToken]) -> bool: """Verifies if the user is a Hugging Face PRO user or part of an enterprise org.""" if not token: return False try: user_info = whoami(token=token.token) if user_info.get("isPro", False): return True orgs = user_info.get("orgs", []) if any(org.get("isEnterprise", False) for org in orgs): return True return False except Exception as e: print(f"Could not verify user's PRO/Enterprise status: {e}") return False # --- Backend Generation Functions --- def run_single_image_logic(prompt: str, image_path: Optional[str] = None) -> str: """Handles text-to-image or single image-to-image using Google Gemini.""" try: model = genai.GenerativeModel(GEMINI_MODEL_NAME) # Use the defined model name contents = [prompt] if image_path: input_image = Image.open(image_path) contents.append(input_image) response = model.generate_content(contents) # Access the image data correctly based on the response structure # Assuming the generated content might be in response.candidates[0].content.parts[0].inline_data.data # Or direct from response.parts if it's a single part with inline_data image_data = None if hasattr(response, 'parts') and response.parts: for part in response.parts: if hasattr(part, 'inline_data') and hasattr(part.inline_data, 'data'): image_data = part.inline_data.data break elif hasattr(response, 'candidates') and response.candidates: for candidate in response.candidates: if hasattr(candidate, 'content') and hasattr(candidate.content, 'parts') and candidate.content.parts: for part in candidate.content.parts: if hasattr(part, 'inline_data') and hasattr(part.inline_data, 'data'): image_data = part.inline_data.data break if image_data: break if not image_data: raise ValueError("No image data found in the model response.") # Save the generated image to a temporary file to return its path pil_image = Image.open(io.BytesIO(image_data)) with tempfile.NamedTemporaryFile(delete=False, suffix=".png") as tmpfile: pil_image.save(tmpfile.name) return tmpfile.name except Exception as e: raise gr.Error(f"Image generation failed: {e}") def run_multi_image_logic(prompt: str, images: List[str]) -> str: """ Handles multi-image editing by sending a list of images and a prompt. """ if not images: raise gr.Error("Please upload at least one image in the 'Multiple Images' tab.") try: model = genai.GenerativeModel(GEMINI_MODEL_NAME) # Use the defined model name # The prompt should be the last part of the contents list contents = [Image.open(image_path[0]) for image_path in images] contents.append(prompt) response = model.generate_content(contents) image_data = None if hasattr(response, 'parts') and response.parts: for part in response.parts: if hasattr(part, 'inline_data') and hasattr(part.inline_data, 'data'): image_data = part.inline_data.data break elif hasattr(response, 'candidates') and response.candidates: for candidate in response.candidates: if hasattr(candidate, 'content') and hasattr(candidate.content, 'parts') and candidate.content.parts: for part in candidate.content.parts: if hasattr(part, 'inline_data') and hasattr(part.inline_data, 'data'): image_data = part.inline_data.data break if image_data: break if not image_data: raise ValueError("No image data found in the model response.") # Save the generated image to a temporary file to return its path pil_image = Image.open(io.BytesIO(image_data)) with tempfile.NamedTemporaryFile(delete=False, suffix=".png") as tmpfile: pil_image.save(tmpfile.name) return tmpfile.name except Exception as e: raise gr.Error(f"Image generation failed: {e}") # --- Gradio App UI --- css = ''' #sub_title{margin-top: -35px !important} .tab-wrapper{margin-bottom: -33px !important} .tabitem{padding: 0px !important} #output{margin-top: 25px} .fillable{max-width: 980px !important} .dark .progress-text {color: white} ''' with gr.Blocks(theme=gr.themes.Citrus(), css=css) as demo: gr.HTML("