Spaces:
Runtime error
Runtime error
| import gradio as gr | |
| from PIL import Image | |
| import cv2 | |
| import numpy as np | |
| import os | |
| from google import genai | |
| from google.genai import types | |
| import io | |
| # ====================== GEMINI API AYARLARI ====================== | |
| # Google AI Studio'dan API key al: https://aistudio.google.com/app/apikey | |
| GEMINI_API_KEY = os.getenv("GEMINI_API_KEY") # Hugging Face Secrets'e ekle veya .env kullan | |
| client = genai.Client(api_key=GEMINI_API_KEY) if GEMINI_API_KEY else None | |
| # ====================== KLASİK FİLTRELER ====================== | |
| def apply_classic_filter(image, filter_name, intensity=1.0): | |
| img = np.array(image) | |
| if filter_name == "Orijinal": | |
| return image | |
| elif filter_name == "Siyah-Beyaz": | |
| gray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY) | |
| return Image.fromarray(cv2.cvtColor(gray, cv2.COLOR_GRAY2RGB)) | |
| elif filter_name == "Sepya": | |
| sepia = np.array([[0.393, 0.769, 0.189], [0.349, 0.686, 0.168], [0.272, 0.534, 0.131]]) | |
| img_sepia = cv2.transform(img, sepia) | |
| img_sepia = np.clip(img_sepia, 0, 255).astype(np.uint8) | |
| return Image.fromarray(img_sepia) | |
| elif filter_name == "Vintage": | |
| img = cv2.cvtColor(img, cv2.COLOR_RGB2BGR) | |
| img = cv2.addWeighted(img, 0.8, cv2.GaussianBlur(img, (15,15), 0), 0.2, 0) | |
| return Image.fromarray(cv2.cvtColor(img, cv2.COLOR_BGR2RGB)) | |
| elif filter_name == "Cartoon": | |
| gray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY) | |
| gray = cv2.medianBlur(gray, 5) | |
| edges = cv2.adaptiveThreshold(gray, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY, 9, 9) | |
| color = cv2.bilateralFilter(img, 9, 300, 300) | |
| cartoon = cv2.bitwise_and(color, color, mask=edges) | |
| return Image.fromarray(cartoon) | |
| elif filter_name == "Pencil Sketch": | |
| gray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY) | |
| invert = cv2.bitwise_not(gray) | |
| blur = cv2.GaussianBlur(invert, (21, 21), 0) | |
| sketch = cv2.divide(gray, blur, scale=256.0) | |
| return Image.fromarray(sketch) | |
| elif filter_name == "Beauty / Glow": | |
| img_pil = Image.fromarray(img) | |
| img_pil = ImageEnhance.Brightness(img_pil).enhance(1.2) | |
| img_pil = ImageEnhance.Contrast(img_pil).enhance(1.3) | |
| img_pil = ImageEnhance.Sharpness(img_pil).enhance(1.5) | |
| return img_pil | |
| # ... diğer filtreleri istersen ekleyebilirim | |
| return image | |
| # ====================== NANO BANANA 2 GERÇEK API ====================== | |
| def apply_nano_banana_2(image: Image.Image, prompt: str): | |
| if not client: | |
| return Image.new("RGB", (512, 512), color="red") # API key yok uyarısı | |
| if not prompt.strip(): | |
| prompt = "Bu fotoğrafı daha güzel ve profesyonel hale getir, doğal ışık ve detayları artır" | |
| try: | |
| # Resmi bytes olarak hazırla | |
| img_byte_arr = io.BytesIO() | |
| image.save(img_byte_arr, format='PNG') | |
| img_byte_arr = img_byte_arr.getvalue() | |
| # Gemini Nano Banana 2 (gemini-3.1-flash-image-preview) ile düzenleme | |
| response = client.models.generate_content( | |
| model="gemini-3.1-flash-image-preview", # Nano Banana 2 | |
| contents=[ | |
| types.Content( | |
| role="user", | |
| parts=[ | |
| types.Part.from_bytes(data=img_byte_arr, mime_type="image/png"), | |
| types.Part.from_text(text=prompt) | |
| ] | |
| ) | |
| ] | |
| ) | |
| # Yanıttan üretilen resmi al | |
| for part in response.candidates[0].content.parts: | |
| if part.inline_data: | |
| generated_image = Image.open(io.BytesIO(part.inline_data.data)) | |
| return generated_image | |
| return image # fallback | |
| except Exception as e: | |
| print("Nano Banana 2 Error:", str(e)) | |
| # Hata durumunda basit bir efekt ver | |
| return ImageEnhance.Contrast(image).enhance(1.4) | |
| # ====================== ANA FONKSİYON ====================== | |
| def apply_filter(image, filter_name, intensity=1.0, prompt=""): | |
| if filter_name == "Prompt ile Düzenle (Nano Banana 2)": | |
| return apply_nano_banana_2(image, prompt) | |
| else: | |
| return apply_classic_filter(image, filter_name, intensity) | |
| # ====================== GRADIO ARAYÜZ ====================== | |
| with gr.Blocks(title="📸 Foto Filter + Nano Banana 2", theme=gr.themes.Soft()) as demo: | |
| gr.Markdown("# 📸 Foto Filter + **Gerçek Nano Banana 2** Entegrasyonu\n" | |
| "Artık 'Prompt ile Düzenle' seçeneğinde **Google Gemini 3.1 Flash Image (Nano Banana 2)** kullanıyor!") | |
| with gr.Row(): | |
| with gr.Column(): | |
| input_image = gr.Image(type="pil", label="📤 Fotoğraf Yükle", height=550) | |
| filter_choice = gr.Dropdown( | |
| choices=[ | |
| "Orijinal", "Siyah-Beyaz", "Sepya", "Vintage", "Cartoon", | |
| "Pencil Sketch", "Beauty / Glow", | |
| "Prompt ile Düzenle (Nano Banana 2)" | |
| ], | |
| value="Orijinal", | |
| label="🎨 Filtre / Efekt Seç" | |
| ) | |
| intensity = gr.Slider(0.5, 2.0, value=1.0, step=0.1, label="Klasik Filtre Yoğunluğu", visible=True) | |
| prompt = gr.Textbox( | |
| label="✍️ Düzenleme Talimatı (Nano Banana 2 için)", | |
| placeholder="Örnek: 'Bu kişiyi plajda gün batımında göster', 'karikatür tarzı yap', 'profesyonel portre efekti ver'", | |
| lines=2 | |
| ) | |
| btn = gr.Button("🚀 Uygula", variant="primary", size="large") | |
| with gr.Column(): | |
| output_image = gr.Image(type="pil", label="📥 Sonuç", height=550) | |
| gr.Markdown(""" | |
| ### 💡 Nasıl Kullanılır? | |
| 1. Google AI Studio’dan ücretsiz API Key al → [aistudio.google.com/app/apikey](https://aistudio.google.com/app/apikey) | |
| 2. Hugging Face Space’te **Secrets** kısmına `GEMINI_API_KEY` adıyla ekle | |
| 3. "Prompt ile Düzenle (Nano Banana 2)" seçeneğini kullan | |
| """) | |
| btn.click( | |
| fn=apply_filter, | |
| inputs=[input_image, filter_choice, intensity, prompt], | |
| outputs=output_image | |
| ) | |
| if __name__ == "__main__": | |
| demo.launch() |