öğreticiler

PoYo'da Nano Banana 2 API Nasıl Kullanılır: Tam Geliştirici Kılavuzu

Poyo.ai Team
Updated
9 min read
Share:

Nano Banana 2 API'nin PoYo'da Kullanımı

PoYo'daki Nano Banana 2 API, Nano Banana Pro'dan ayrı ve daha düşük maliyetli bir Nano Banana rotasıdır. Bu kılavuz, 1K çıkışı için üretim başına 5 krediden başlayan yaklaşık $0.025 fiyatla nano-banana-2 ve nano-banana-2-edit kullanımı konusunda size yol gösterir.

Nano Banana 2 API'ye Başlarken

Adım 1: PoYo Hesabınızı Kurun

Öncelikle PoYo hesabınızı oluşturun ve API kimlik bilgilerinizi alın:

  1. PoYo.ai adresinden kaydolun
  2. Kontrol panelinde API anahtarınızı oluşturun
  3. Hesabınıza kredi ekleyin (Nano Banana 2'nin maliyeti üretim başına 5 kredidir)

Adım 2: Gerekli Bağımlılıkları Yükleyin

# For Node.js/JavaScript
npm install axios

# For Python
pip install requests

# For cURL (no installation needed)

Temel Metinden Görüntüye Oluşturma

JavaScript/Node.js Uygulaması

const axios = require('axios');

const generateImage = async (prompt, size = '1:1') => {
  try {
    const response = await axios.post(
      'https://api.poyo.ai/api/generate/submit',
      {
        model: 'nano-banana-2',
        callback_url: 'https://your-domain.com/callback', // Optional webhook
        input: {
          prompt: prompt,
          size: size // Options: '1:1', '16:9', '9:16', '2:3', '3:2', '3:4', '4:3'
        }
      },
      {
        headers: {
          'Authorization': `Bearer YOUR_API_KEY`,
          'Content-Type': 'application/json'
        }
      }
    );

    console.log('Task ID:', response.data.task_id);
    console.log('Status:', response.data.status);
    console.log('Created:', response.data.created_time);
    return response.data;
  } catch (error) {
    console.error('Error generating image:', error.response?.data || error.message);
  }
};

// Example usage
generateImage('A futuristic city skyline at sunset with flying cars', '16:9');

Python Uygulaması

import requests
import json

def generate_image(prompt, api_key, size='1:1'):
    url = "https://api.poyo.ai/api/generate/submit"

    payload = {
        "model": "nano-banana-2",
        "callback_url": "https://your-domain.com/callback",  # Optional webhook
        "input": {
            "prompt": prompt,
            "size": size  # Options: '1:1', '16:9', '9:16', '2:3', '3:2', '3:4', '4:3'
        }
    }

    headers = {
        "Authorization": f"Bearer {api_key}",
        "Content-Type": "application/json"
    }

    try:
        response = requests.post(url, json=payload, headers=headers)
        response.raise_for_status()

        result = response.json()
        print(f"Task ID: {result['task_id']}")
        print(f"Status: {result['status']}")
        print(f"Created: {result['created_time']}")
        return result

    except requests.exceptions.RequestException as e:
        print(f"Error generating image: {e}")
        return None

# Example usage
api_key = "YOUR_API_KEY"
result = generate_image("A serene mountain landscape with crystal clear lake", api_key, '16:9')

cURL Uygulaması

curl -X POST "https://api.poyo.ai/api/generate/submit" \
  -H "Authorization: Bearer YOUR_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "nano-banana-2",
    "callback_url": "https://your-domain.com/callback",
    "input": {
      "prompt": "A professional headshot of a confident business woman in modern office setting",
      "size": "1:1"
    }
  }'

Nano Banana 2'nin Gelişmiş Özellikleri

1. Kültürel Bağlam Farkındalığı

Nano Banana 2'nin kültürel anlayışı özgün bölgesel içeriğe olanak tanır:

const culturalPrompts = [
  "Tokyo springtime cherry blossom picnic with families in traditional dress",
  "Berlin winter street fashion with authentic German architecture",
  "Mumbai monsoon street food vendor with vibrant local atmosphere"
];

// Generate culturally accurate images
culturalPrompts.forEach(async (prompt) => {
  const result = await generateImage(prompt);
  console.log(`Generated culturally aware image for: ${prompt}`);
});

2. Mükemmel Metin Oluşturma

Matematiksel denklemler ve karmaşık metinlerle görseller oluşturun:

def generate_educational_content(equation, api_key):
    prompt = f"A clean whiteboard with the mathematical equation '{equation}' written in clear handwriting, classroom setting"

    payload = {
        "model": "nano-banana-2",
        "prompt": prompt,
        "width": 1024,
        "height": 768,
        "guidance_scale": 8.0,
        "num_inference_steps": 60
    }

    return generate_image_with_payload(payload, api_key)

# Generate educational content with perfect text
result = generate_educational_content("E = mc虏", "YOUR_API_KEY")

3. Çoklu En Boy Oranları

Nano Banana 2, farklı kullanım durumları için çeşitli en boy oranlarını destekler:

const aspectRatios = {
  square: { width: 1024, height: 1024 },      // 1:1 - Social media
  portrait: { width: 768, height: 1024 },     // 3:4 - Mobile screens
  landscape: { width: 1024, height: 768 },    // 4:3 - Desktop
  widescreen: { width: 1792, height: 1024 },  // 16:9 - Video thumbnails
  ultrawide: { width: 2048, height: 896 }     // 21:9 - Banners
};

const generateWithAspectRatio = async (prompt, ratio) => {
  const dimensions = aspectRatios[ratio];

  const response = await axios.post(
    'https://api.poyo.ai/v1/image/generations',
    {
      model: 'nano-banana-2',
      prompt: prompt,
      ...dimensions,
      guidance_scale: 7.5
    },
    {
      headers: {
        'Authorization': `Bearer YOUR_API_KEY`,
        'Content-Type': 'application/json'
      }
    }
  );

  return response.data;
};

Nano Banana 2 ile Görüntü Düzenleme

Görüntüden Görüntüye Dönüşüm

const editImage = async (imageUrls, editPrompt, size = '1:1') => {
  try {
    const response = await axios.post(
      'https://api.poyo.ai/api/generate/submit',
      {
        model: 'nano-banana-2-edit',
        callback_url: 'https://your-domain.com/callback', // Optional
        input: {
          prompt: editPrompt,
          image_urls: imageUrls, // Array of reference image URLs
          size: size
        }
      },
      {
        headers: {
          'Authorization': `Bearer YOUR_API_KEY`,
          'Content-Type': 'application/json'
        }
      }
    );

    return response.data;
  } catch (error) {
    console.error('Error editing image:', error.response?.data || error.message);
  }
};

// Example: Transform day scene to night
const result = await editImage(
  ['https://example.com/day-scene.jpg'],
  'Transform this scene to a beautiful night setting with stars and moon',
  '16:9'
);

Sonuçları Webhook'larla İşleme

Nano Banana 2 API, sonuç teslimi için webhook'ları kullanır. Sonuçları almak için bir webhook uç noktası ayarlayın:

Webhook Uç Noktasını Ayarlama

// Express.js webhook handler
const express = require('express');
const app = express();

app.use(express.json());

// Webhook endpoint to receive results
app.post('/webhook/callback', (req, res) => {
  const { task_id, status, files } = req.body;

  console.log(`Task ${task_id} status: ${status}`);

  if (status === 'finished' && files && files.length > 0) {
    console.log('Generated image URL:', files[0].file_url);
    // Process the completed image
    processGeneratedImage(task_id, files[0].file_url);
  } else if (status === 'failed') {
    console.error('Generation failed for task:', task_id);
    handleGenerationError(task_id);
  }

  res.status(200).send('OK');
});

const processGeneratedImage = (taskId, imageUrl) => {
  // Your logic to handle the completed image
  console.log(`Processing image from task ${taskId}: ${imageUrl}`);
};

app.listen(3000, () => {
  console.log('Webhook server running on port 3000');
});

Alternatif: Manuel Durum Kontrolü

Webhook'ları kullanmamayı tercih ederseniz görev durumunu manuel olarak kontrol edebilirsiniz:

const checkTaskStatus = async (taskId) => {
  try {
    const response = await axios.get(
      `https://api.poyo.ai/api/generate/status/${taskId}`,
      {
        headers: {
          'Authorization': `Bearer YOUR_API_KEY`
        }
      }
    );

    return response.data.data; // Returns the task data
  } catch (error) {
    console.error('Error checking status:', error.message);
    return null;
  }
};

const pollForResult = async (taskId, maxAttempts = 30) => {
  for (let attempt = 0; attempt < maxAttempts; attempt++) {
    const taskData = await checkTaskStatus(taskId);

    if (!taskData) break;

    console.log(`Status: ${taskData.status}, Progress: ${taskData.progress}%`);

    if (taskData.status === 'finished') {
      console.log('Image generated successfully!');
      if (taskData.files && taskData.files.length > 0) {
        console.log('Image URL:', taskData.files[0].file_url);
      }
      return taskData;
    } else if (taskData.status === 'failed') {
      console.error('Generation failed:', taskData.error_message);
      return null;
    }

    // Status is 'not_started' or 'running'
    await new Promise(resolve => setTimeout(resolve, 3000)); // Wait 3 seconds
  }

  console.error('Maximum polling attempts reached');
  return null;
};

// Complete workflow example
const completeGeneration = async (prompt) => {
  // Step 1: Submit generation request
  const generation = await generateImage(prompt);

  if (!generation) return null;

  // Step 2: Poll for results
  const result = await pollForResult(generation.task_id);

  return result;
};

Python Durum Kontrolü

import time

def check_task_status(task_id, api_key):
    url = f"https://api.poyo.ai/api/generate/status/{task_id}"

    headers = {
        "Authorization": f"Bearer {api_key}",
        "Content-Type": "application/json"
    }

    try:
        response = requests.get(url, headers=headers)
        response.raise_for_status()

        return response.json()['data']
    except requests.exceptions.RequestException as e:
        print(f"Error checking status: {e}")
        return None

def wait_for_completion(task_id, api_key, max_attempts=30):
    for attempt in range(max_attempts):
        task_data = check_task_status(task_id, api_key)

        if not task_data:
            break

        print(f"Status: {task_data['status']}, Progress: {task_data.get('progress', 0)}%")

        if task_data['status'] == 'finished':
            print("Image generated successfully!")
            if task_data.get('files') and len(task_data['files']) > 0:
                print(f"Image URL: {task_data['files'][0]['file_url']}")
            return task_data
        elif task_data['status'] == 'failed':
            print(f"Generation failed: {task_data.get('error_message', 'Unknown error')}")
            return None

        time.sleep(3)  # Wait 3 seconds

    print("Maximum attempts reached")
    return None

# Complete workflow
def complete_image_generation(prompt, api_key, size='1:1'):
    # Step 1: Submit request
    task_result = generate_image(prompt, api_key, size)

    if not task_result:
        return None

    # Step 2: Wait for completion
    final_result = wait_for_completion(task_result['task_id'], api_key)

    return final_result

Hata İşleme ve En İyi Uygulamalar

Kapsamlı Hata İşleme

const robustImageGeneration = async (prompt, size = '1:1', retries = 3) => {
  for (let attempt = 0; attempt < retries; attempt++) {
    try {
      const response = await axios.post(
        'https://api.poyo.ai/api/generate/submit',
        {
          model: 'nano-banana-2',
          input: {
            prompt: prompt,
            size: size
          }
        },
        {
          headers: {
            'Authorization': `Bearer YOUR_API_KEY`,
            'Content-Type': 'application/json'
          },
          timeout: 30000 // 30 second timeout
        }
      );

      return response.data;

    } catch (error) {
      if (error.response) {
        // Server responded with error status
        const status = error.response.status;
        const message = error.response.data?.error || 'Unknown error';

        console.error(`API Error (${status}): ${message}`);

        // Don't retry for certain errors
        if (status === 401 || status === 403) {
          throw new Error('Authentication failed. Check your API key.');
        }

        if (status === 400) {
          throw new Error(`Bad request: ${message}`);
        }

      } else if (error.request) {
        console.error('Network error:', error.message);
      } else {
        console.error('Request setup error:', error.message);
      }

      // If this was the last attempt, throw the error
      if (attempt === retries - 1) {
        throw error;
      }

      // Wait before retrying (exponential backoff)
      const delay = Math.pow(2, attempt) * 1000;
      console.log(`Retrying in ${delay}ms... (Attempt ${attempt + 1}/${retries})`);
      await new Promise(resolve => setTimeout(resolve, delay));
    }
  }
};

Üretim İçin En İyi Uygulamalar

  1. Hız Sınırlama: API sınırlarına ulaşmamak için uygun hız sınırlaması uygulayın
  2. Önbelleğe alma: API çağrılarını azaltmak için başarılı üretimleri önbelleğe alın
  3. Kullanıcı Deneyimi: Uzun süren üretimler için ilerleme göstergeleri gösterin
  4. Hata Kurtarma: Başarısız üretimler için uygun fallback stratejileri uygulayın
class NanoBanana2Client {
  constructor(apiKey, options = {}) {
    this.apiKey = apiKey;
    this.baseURL = 'https://api.poyo.ai/v1';
    this.requestsPerMinute = options.requestsPerMinute || 60;
    this.queue = [];
  }

  async generateImage(prompt, options = {}) {
    const payload = {
      model: 'nano-banana-2',
      prompt,
      width: options.width || 1024,
      height: options.height || 1024,
      guidance_scale: options.guidanceScale || 7.5,
      num_inference_steps: options.steps || 50
    };

    return this.makeRequest('/image/generations', payload);
  }

  async editImage(imageUrl, editPrompt, options = {}) {
    const payload = {
      model: 'nano-banana-2-edit',
      image: imageUrl,
      prompt: editPrompt,
      strength: options.strength || 0.8,
      guidance_scale: options.guidanceScale || 7.5
    };

    return this.makeRequest('/image/edits', payload);
  }

  async makeRequest(endpoint, payload) {
    // Implement rate limiting, error handling, and retries
    // This is a simplified version

    try {
      const response = await axios.post(
        `${this.baseURL}${endpoint}`,
        payload,
        {
          headers: {
            'Authorization': `Bearer ${this.apiKey}`,
            'Content-Type': 'application/json'
          }
        }
      );

      return response.data;
    } catch (error) {
      throw this.handleError(error);
    }
  }

  handleError(error) {
    // Implement comprehensive error handling
    return new Error(`Nano Banana 2 API Error: ${error.message}`);
  }
}

Performans Optimizasyonu İpuçları

1. İstem Mühendisliği

  • Açık ve net olun: Ayrıntılı yönlendirmeler daha iyi sonuçlar verir
  • Stil yönergelerini ekleyin: "fotogerçekçi", "4K çözünürlük", "profesyonel"
  • Kültürel bağlamı kullanın: Nano Banana 2'nin kültürel farkındalığından yararlanın

2. Parametre Ayarlama

  • guidance_scale: Çoğu kullanım durumu için 7.0-8.5
  • num_inference_steps: 30-60 (daha iyi kalite için daha yüksek)
  • strength: 0.6-0.9 görüntü düzenleme için (daha düşük değer özgün görüntüyü daha fazla korur)

3. Toplu İşleme

const generateBatch = async (prompts) => {
  const generations = await Promise.all(
    prompts.map(prompt => generateImage(prompt))
  );

  const results = await Promise.all(
    generations.map(gen => pollForResult(gen.id))
  );

  return results.filter(result => result !== null);
};

Sonuç

PoYo'daki Nano Banana 2 API, devrim niteliğindeki 5 adımlı iş akışı, kültürel bağlam farkındalığı ve mükemmel metin oluşturma ile yapay zeka görüntü oluşturma için benzeri görülmemiş yetenekler sağlar. Bu kılavuzu takip ederek bu güçlü özellikleri uygulamalarınıza entegre edebilir ve profesyonel düzeyde görsel içerik oluşturabilirsiniz.

Daha gelişmiş özellikler ve eksiksiz API belgeleri için resmi Nano Banana 2 API belgelerini ziyaret edin.

Geliştirmeye başlamaya hazır mısınız? API anahtarınızı alın ve bugün Nano Banana 2 ile oluşturmaya başlayın.


Uygulama konusunda yardıma mı ihtiyacınız var? Gelişmiş teknikler için geliştirici topluluğumuza katılın veya daha fazla API eğitimine göz atın.

Share: