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
|
| 5 |
+
import os
|
| 6 |
+
from google import genai
|
| 7 |
+
from google.genai import types
|
| 8 |
+
import io
|
| 9 |
+
|
| 10 |
+
# ====================== GEMINI API AYARLARI ======================
|
| 11 |
+
# Google AI Studio'dan API key al: https://aistudio.google.com/app/apikey
|
| 12 |
+
GEMINI_API_KEY = os.getenv("GEMINI_API_KEY") # Hugging Face Secrets'e ekle veya .env kullan
|
| 13 |
+
|
| 14 |
+
client = genai.Client(api_key=GEMINI_API_KEY) if GEMINI_API_KEY else None
|
| 15 |
+
|
| 16 |
+
# ====================== KLASİK FİLTRELER ======================
|
| 17 |
+
def apply_classic_filter(image, filter_name, intensity=1.0):
|
| 18 |
+
img = np.array(image)
|
| 19 |
+
|
| 20 |
+
if filter_name == "Orijinal":
|
| 21 |
+
return image
|
| 22 |
+
elif filter_name == "Siyah-Beyaz":
|
| 23 |
+
gray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
|
| 24 |
+
return Image.fromarray(cv2.cvtColor(gray, cv2.COLOR_GRAY2RGB))
|
| 25 |
+
elif filter_name == "Sepya":
|
| 26 |
+
sepia = np.array([[0.393, 0.769, 0.189], [0.349, 0.686, 0.168], [0.272, 0.534, 0.131]])
|
| 27 |
+
img_sepia = cv2.transform(img, sepia)
|
| 28 |
+
img_sepia = np.clip(img_sepia, 0, 255).astype(np.uint8)
|
| 29 |
+
return Image.fromarray(img_sepia)
|
| 30 |
+
elif filter_name == "Vintage":
|
| 31 |
+
img = cv2.cvtColor(img, cv2.COLOR_RGB2BGR)
|
| 32 |
+
img = cv2.addWeighted(img, 0.8, cv2.GaussianBlur(img, (15,15), 0), 0.2, 0)
|
| 33 |
+
return Image.fromarray(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))
|
| 34 |
+
elif filter_name == "Cartoon":
|
| 35 |
+
gray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
|
| 36 |
+
gray = cv2.medianBlur(gray, 5)
|
| 37 |
+
edges = cv2.adaptiveThreshold(gray, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY, 9, 9)
|
| 38 |
+
color = cv2.bilateralFilter(img, 9, 300, 300)
|
| 39 |
+
cartoon = cv2.bitwise_and(color, color, mask=edges)
|
| 40 |
+
return Image.fromarray(cartoon)
|
| 41 |
+
elif filter_name == "Pencil Sketch":
|
| 42 |
+
gray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
|
| 43 |
+
invert = cv2.bitwise_not(gray)
|
| 44 |
+
blur = cv2.GaussianBlur(invert, (21, 21), 0)
|
| 45 |
+
sketch = cv2.divide(gray, blur, scale=256.0)
|
| 46 |
+
return Image.fromarray(sketch)
|
| 47 |
+
elif filter_name == "Beauty / Glow":
|
| 48 |
+
img_pil = Image.fromarray(img)
|
| 49 |
+
img_pil = ImageEnhance.Brightness(img_pil).enhance(1.2)
|
| 50 |
+
img_pil = ImageEnhance.Contrast(img_pil).enhance(1.3)
|
| 51 |
+
img_pil = ImageEnhance.Sharpness(img_pil).enhance(1.5)
|
| 52 |
+
return img_pil
|
| 53 |
+
# ... diğer filtreleri istersen ekleyebilirim
|
| 54 |
+
return image
|
| 55 |
+
|
| 56 |
+
# ====================== NANO BANANA 2 GERÇEK API ======================
|
| 57 |
+
def apply_nano_banana_2(image: Image.Image, prompt: str):
|
| 58 |
+
if not client:
|
| 59 |
+
return Image.new("RGB", (512, 512), color="red") # API key yok uyarısı
|
| 60 |
+
|
| 61 |
+
if not prompt.strip():
|
| 62 |
+
prompt = "Bu fotoğrafı daha güzel ve profesyonel hale getir, doğal ışık ve detayları artır"
|
| 63 |
+
|
| 64 |
+
try:
|
| 65 |
+
# Resmi bytes olarak hazırla
|
| 66 |
+
img_byte_arr = io.BytesIO()
|
| 67 |
+
image.save(img_byte_arr, format='PNG')
|
| 68 |
+
img_byte_arr = img_byte_arr.getvalue()
|
| 69 |
+
|
| 70 |
+
# Gemini Nano Banana 2 (gemini-3.1-flash-image-preview) ile düzenleme
|
| 71 |
+
response = client.models.generate_content(
|
| 72 |
+
model="gemini-3.1-flash-image-preview", # Nano Banana 2
|
| 73 |
+
contents=[
|
| 74 |
+
types.Content(
|
| 75 |
+
role="user",
|
| 76 |
+
parts=[
|
| 77 |
+
types.Part.from_bytes(data=img_byte_arr, mime_type="image/png"),
|
| 78 |
+
types.Part.from_text(text=prompt)
|
| 79 |
+
]
|
| 80 |
+
)
|
| 81 |
+
]
|
| 82 |
+
)
|
| 83 |
+
|
| 84 |
+
# Yanıttan üretilen resmi al
|
| 85 |
+
for part in response.candidates[0].content.parts:
|
| 86 |
+
if part.inline_data:
|
| 87 |
+
generated_image = Image.open(io.BytesIO(part.inline_data.data))
|
| 88 |
+
return generated_image
|
| 89 |
+
|
| 90 |
+
return image # fallback
|
| 91 |
+
|
| 92 |
+
except Exception as e:
|
| 93 |
+
print("Nano Banana 2 Error:", str(e))
|
| 94 |
+
# Hata durumunda basit bir efekt ver
|
| 95 |
+
return ImageEnhance.Contrast(image).enhance(1.4)
|
| 96 |
+
|
| 97 |
+
# ====================== ANA FONKSİYON ======================
|
| 98 |
+
def apply_filter(image, filter_name, intensity=1.0, prompt=""):
|
| 99 |
+
if filter_name == "Prompt ile Düzenle (Nano Banana 2)":
|
| 100 |
+
return apply_nano_banana_2(image, prompt)
|
| 101 |
+
else:
|
| 102 |
+
return apply_classic_filter(image, filter_name, intensity)
|
| 103 |
+
|
| 104 |
+
# ====================== GRADIO ARAYÜZ ======================
|
| 105 |
+
with gr.Blocks(title="📸 Foto Filter + Nano Banana 2", theme=gr.themes.Soft()) as demo:
|
| 106 |
+
gr.Markdown("# 📸 Foto Filter + **Gerçek Nano Banana 2** Entegrasyonu\n"
|
| 107 |
+
"Artık 'Prompt ile Düzenle' seçeneğinde **Google Gemini 3.1 Flash Image (Nano Banana 2)** kullanıyor!")
|
| 108 |
+
|
| 109 |
+
with gr.Row():
|
| 110 |
+
with gr.Column():
|
| 111 |
+
input_image = gr.Image(type="pil", label="📤 Fotoğraf Yükle", height=550)
|
| 112 |
+
|
| 113 |
+
filter_choice = gr.Dropdown(
|
| 114 |
+
choices=[
|
| 115 |
+
"Orijinal", "Siyah-Beyaz", "Sepya", "Vintage", "Cartoon",
|
| 116 |
+
"Pencil Sketch", "Beauty / Glow",
|
| 117 |
+
"Prompt ile Düzenle (Nano Banana 2)"
|
| 118 |
+
],
|
| 119 |
+
value="Orijinal",
|
| 120 |
+
label="🎨 Filtre / Efekt Seç"
|
| 121 |
+
)
|
| 122 |
+
|
| 123 |
+
intensity = gr.Slider(0.5, 2.0, value=1.0, step=0.1, label="Klasik Filtre Yoğunluğu", visible=True)
|
| 124 |
+
prompt = gr.Textbox(
|
| 125 |
+
label="✍️ Düzenleme Talimatı (Nano Banana 2 için)",
|
| 126 |
+
placeholder="Örnek: 'Bu kişiyi plajda gün batımında göster', 'karikatür tarzı yap', 'profesyonel portre efekti ver'",
|
| 127 |
+
lines=2
|
| 128 |
+
)
|
| 129 |
+
|
| 130 |
+
btn = gr.Button("🚀 Uygula", variant="primary", size="large")
|
| 131 |
+
|
| 132 |
+
with gr.Column():
|
| 133 |
+
output_image = gr.Image(type="pil", label="📥 Sonuç", height=550)
|
| 134 |
+
|
| 135 |
+
gr.Markdown("""
|
| 136 |
+
### 💡 Nasıl Kullanılır?
|
| 137 |
+
1. Google AI Studio’dan ücretsiz API Key al → [aistudio.google.com/app/apikey](https://aistudio.google.com/app/apikey)
|
| 138 |
+
2. Hugging Face Space’te **Secrets** kısmına `GEMINI_API_KEY` adıyla ekle
|
| 139 |
+
3. "Prompt ile Düzenle (Nano Banana 2)" seçeneğini kullan
|
| 140 |
+
""")
|
| 141 |
+
|
| 142 |
+
btn.click(
|
| 143 |
+
fn=apply_filter,
|
| 144 |
+
inputs=[input_image, filter_choice, intensity, prompt],
|
| 145 |
+
outputs=output_image
|
| 146 |
+
)
|
| 147 |
+
|
| 148 |
+
if __name__ == "__main__":
|
| 149 |
+
demo.launch()
|