# pip install google-genai gradio pillow import base64 import mimetypes import os import io import gradio as gr from google import genai from google.genai import types from PIL import Image import tempfile class GeminiImageGenerator: def __init__(self): self.client = None self.model = "gemini-2.5-flash-image-preview" def set_api_key(self, api_key): """API anahtarını ayarla""" try: os.environ["GEMINI_API_KEY"] = api_key self.client = genai.Client(api_key=api_key) return "✅ API anahtarı başarıyla ayarlandı!" except Exception as e: return f"❌ API anahtarı ayarlanırken hata: {str(e)}" def generate_image(self, prompt, api_key): """Görsel oluştur""" if not api_key: return None, "❌ Lütfen önce API anahtarınızı girin!" if not prompt.strip(): return None, "❌ Lütfen bir görsel açıklaması girin!" try: # API anahtarını ayarla if not self.client or os.environ.get("GEMINI_API_KEY") != api_key: self.client = genai.Client(api_key=api_key) os.environ["GEMINI_API_KEY"] = api_key # İçerik oluştur contents = [ types.Content( role="user", parts=[ types.Part.from_text(text=f"Generate an image of: {prompt}"), ], ), ] generate_content_config = types.GenerateContentConfig( response_modalities=[ "IMAGE", "TEXT", ], ) # Stream'den veri al image_data = None text_response = "" for chunk in self.client.models.generate_content_stream( model=self.model, contents=contents, config=generate_content_config, ): if ( chunk.candidates is None or chunk.candidates[0].content is None or chunk.candidates[0].content.parts is None ): continue # Görsel verisi varsa al if (chunk.candidates[0].content.parts[0].inline_data and chunk.candidates[0].content.parts[0].inline_data.data): inline_data = chunk.candidates[0].content.parts[0].inline_data image_data = inline_data.data # Metin yanıtı varsa al if hasattr(chunk, 'text') and chunk.text: text_response += chunk.text if image_data: # Görsel verisini PIL Image'e çevir image = Image.open(io.BytesIO(image_data)) success_msg = f"✅ Görsel başarıyla oluşturuldu!\n\nAçıklama: {prompt}" if text_response: success_msg += f"\n\nGemini'nin yorumu: {text_response}" return image, success_msg else: return None, f"❌ Görsel oluşturulamadı. Yanıt: {text_response}" except Exception as e: return None, f"❌ Hata oluştu: {str(e)}" def save_image(self, image, filename): """Görseli kaydet""" if image is None: return "❌ Kaydedilecek görsel yok!" try: if not filename.strip(): filename = "generated_image.png" if not filename.endswith(('.png', '.jpg', '.jpeg')): filename += '.png' image.save(filename) return f"✅ Görsel '{filename}' olarak kaydedildi!" except Exception as e: return f"❌ Kaydetme hatası: {str(e)}" # Generator instance generator = GeminiImageGenerator() def generate_and_display(prompt, api_key): """Görsel oluştur ve göster""" return generator.generate_image(prompt, api_key) def save_current_image(image, filename): """Mevcut görseli kaydet""" return generator.save_image(image, filename) # Gradio arayüzü with gr.Blocks( theme=gr.themes.Soft(), title="Gemini Görsel Oluşturucu", css=""" .main-container { max-width: 1200px; margin: 0 auto; } .header { text-align: center; padding: 20px 0; background: linear-gradient(45deg, #667eea 0%, #764ba2 100%); color: white; border-radius: 10px; margin-bottom: 20px; } .generate-btn { background: linear-gradient(45deg, #667eea 0%, #764ba2 100%) !important; border: none !important; color: white !important; } """ ) as demo: gr.HTML("""
Google Gemini AI ile yaratıcı görseller oluşturun