ऑडियो को समझना

Gemini, ऑडियो इनपुट का विश्लेषण कर सकता है और उसे समझ सकता है. इससे, इस्तेमाल के इन उदाहरणों को पूरा किया जा सकता है:

  • ऑडियो कॉन्टेंट के बारे में बताएं, उसकी खास जानकारी दें या उससे जुड़े सवालों के जवाब दें.
  • ऑडियो की ट्रांसक्रिप्ट दें.
  • ऑडियो के खास सेगमेंट का विश्लेषण करना.

इस गाइड में, ऑडियो इनपुट के लिए टेक्स्ट रिस्पॉन्स जनरेट करने के लिए, Gemini API का इस्तेमाल करने का तरीका बताया गया है.

शुरू करने से पहले

Gemini API को कॉल करने से पहले, पक्का करें कि आपने अपने पसंदीदा एसडीके टूल को इंस्टॉल कर लिया हो. साथ ही, Gemini API पासकोड को कॉन्फ़िगर कर लिया हो और वह इस्तेमाल के लिए तैयार हो.

ऑडियो इनपुट

Gemini को ऑडियो डेटा देने के लिए, ये तरीके अपनाए जा सकते हैं:

ऑडियो फ़ाइल अपलोड करना

ऑडियो फ़ाइल अपलोड करने के लिए, Files API का इस्तेमाल किया जा सकता है. अगर अनुरोध का कुल साइज़ (इसमें फ़ाइलें, टेक्स्ट प्रॉम्प्ट, सिस्टम के निर्देश वगैरह शामिल हैं) 20 एमबी से ज़्यादा है, तो हमेशा Files API का इस्तेमाल करें.

यह कोड, एक ऑडियो फ़ाइल अपलोड करता है. इसके बाद, generateContent को कॉल करने के लिए फ़ाइल का इस्तेमाल करता है.

from google import genai

client = genai.Client(api_key="GOOGLE_API_KEY")

myfile = client.files.upload(file="path/to/sample.mp3")

response = client.models.generate_content(
    model="gemini-2.0-flash", contents=["Describe this audio clip", myfile]
)

print(response.text)
import {
  GoogleGenAI,
  createUserContent,
  createPartFromUri,
} from "@google/genai";

const ai = new GoogleGenAI({ apiKey: "GOOGLE_API_KEY" });

async function main() {
  const myfile = await ai.files.upload({
    file: "path/to/sample.mp3",
    config: { mimeType: "audio/mp3" },
  });

  const response = await ai.models.generateContent({
    model: "gemini-2.0-flash",
    contents: createUserContent([
      createPartFromUri(myfile.uri, myfile.mimeType),
      "Describe this audio clip",
    ]),
  });
  console.log(response.text);
}

await main();
file, err := client.UploadFileFromPath(ctx, "path/to/sample.mp3", nil)
if err != nil {
    log.Fatal(err)
}
defer client.DeleteFile(ctx, file.Name)

model := client.GenerativeModel("gemini-2.0-flash")
resp, err := model.GenerateContent(ctx,
    genai.FileData{URI: file.URI},
    genai.Text("Describe this audio clip"))
if err != nil {
    log.Fatal(err)
}

printResponse(resp)
AUDIO_PATH="path/to/sample.mp3"
MIME_TYPE=$(file -b --mime-type "${AUDIO_PATH}")
NUM_BYTES=$(wc -c < "${AUDIO_PATH}")
DISPLAY_NAME=AUDIO

tmp_header_file=upload-header.tmp

# Initial resumable request defining metadata.
# The upload url is in the response headers dump them to a file.
curl "https://generativelanguage.googleapis.com/upload/v1beta/files?key=${GOOGLE_API_KEY}" \
  -D upload-header.tmp \
  -H "X-Goog-Upload-Protocol: resumable" \
  -H "X-Goog-Upload-Command: start" \
  -H "X-Goog-Upload-Header-Content-Length: ${NUM_BYTES}" \
  -H "X-Goog-Upload-Header-Content-Type: ${MIME_TYPE}" \
  -H "Content-Type: application/json" \
  -d "{'file': {'display_name': '${DISPLAY_NAME}'}}" 2> /dev/null

upload_url=$(grep -i "x-goog-upload-url: " "${tmp_header_file}" | cut -d" " -f2 | tr -d "\r")
rm "${tmp_header_file}"

# Upload the actual bytes.
curl "${upload_url}" \
  -H "Content-Length: ${NUM_BYTES}" \
  -H "X-Goog-Upload-Offset: 0" \
  -H "X-Goog-Upload-Command: upload, finalize" \
  --data-binary "@${AUDIO_PATH}" 2> /dev/null > file_info.json

file_uri=$(jq ".file.uri" file_info.json)
echo file_uri=$file_uri

# Now generate content using that file
curl "https://generativelanguage.googleapis.com/v1beta/models/gemini-2.0-flash:generateContent?key=$GOOGLE_API_KEY" \
    -H 'Content-Type: application/json' \
    -X POST \
    -d '{
      "contents": [{
        "parts":[
          {"text": "Describe this audio clip"},
          {"file_data":{"mime_type": "${MIME_TYPE}", "file_uri": '$file_uri'}}]
        }]
      }' 2> /dev/null > response.json

cat response.json
echo

jq ".candidates[].content.parts[].text" response.json

मीडिया फ़ाइलों के साथ काम करने के बारे में ज़्यादा जानने के लिए, Files API देखें.

ऑडियो डेटा को इनलाइन पास करना

ऑडियो फ़ाइल अपलोड करने के बजाय, generateContent को अनुरोध में इनलाइन ऑडियो डेटा भेजा जा सकता है:

from google.genai import types

with open('path/to/small-sample.mp3', 'rb') as f:
    audio_bytes = f.read()

response = client.models.generate_content(
  model='gemini-2.0-flash',
  contents=[
    'Describe this audio clip',
    types.Part.from_bytes(
      data=audio_bytes,
      mime_type='audio/mp3',
    )
  ]
)

print(response.text)
import { GoogleGenAI } from "@google/genai";
import * as fs from "node:fs";

const ai = new GoogleGenAI({ apiKey: "GOOGLE_API_KEY" });
const base64AudioFile = fs.readFileSync("path/to/small-sample.mp3", {
  encoding: "base64",
});

const contents = [
  { text: "Please summarize the audio." },
  {
    inlineData: {
      mimeType: "audio/mp3",
      data: base64AudioFile,
    },
  },
];

const response = await ai.models.generateContent({
  model: "gemini-2.0-flash",
  contents: contents,
});
console.log(response.text);
// Initialize a Gemini model appropriate for your use case.
model := client.GenerativeModel("gemini-2.0-flash")

bytes, err := os.ReadFile("path/to/small-sample.mp3")
if err != nil {
  log.Fatal(err)
}

prompt := []genai.Part{
  genai.Blob{MIMEType: "audio/mp3", Data: bytes},
  genai.Text("Please summarize the audio."),
}

// Generate content using the prompt.
resp, err := model.GenerateContent(ctx, prompt...)
if err != nil {
  log.Fatal(err)
}

// Handle the response of generated text
for _, c := range resp.Candidates {
  if c.Content != nil {
    fmt.Println(*c.Content)
  }
}

इनलाइन ऑडियो डेटा के बारे में ध्यान रखने वाली कुछ बातें:

  • अनुरोध का साइज़ ज़्यादा से ज़्यादा 20 एमबी हो सकता है. इसमें टेक्स्ट प्रॉम्प्ट, सिस्टम के निर्देश, और इनलाइन में दी गई फ़ाइलें शामिल हैं. अगर आपकी फ़ाइल के साइज़ की वजह से, अनुरोध का कुल साइज़ 20 एमबी से ज़्यादा हो जाता है, तो अनुरोध में इस्तेमाल करने के लिए, Files API का इस्तेमाल करके ऑडियो फ़ाइल अपलोड करें.
  • अगर ऑडियो सैंपल का कई बार इस्तेमाल किया जा रहा है, तो ऑडियो फ़ाइल अपलोड करना ज़्यादा बेहतर होगा.

ट्रांसक्रिप्ट पाना

ऑडियो डेटा की ट्रांसक्रिप्ट पाने के लिए, प्रॉम्प्ट में इसके लिए कहें:

myfile = client.files.upload(file='path/to/sample.mp3')
prompt = 'Generate a transcript of the speech.'

response = client.models.generate_content(
  model='gemini-2.0-flash',
  contents=[prompt, myfile]
)

print(response.text)
import {
  GoogleGenAI,
  createUserContent,
  createPartFromUri,
} from "@google/genai";

const ai = new GoogleGenAI({ apiKey: "GOOGLE_API_KEY" });
const myfile = await ai.files.upload({
  file: "path/to/sample.mp3",
  config: { mimeType: "audio/mpeg" },
});

const result = await ai.models.generateContent({
  model: "gemini-2.0-flash",
  contents: createUserContent([
    createPartFromUri(myfile.uri, myfile.mimeType),
    "Generate a transcript of the speech.",
  ]),
});
console.log("result.text=", result.text);
// Initialize a Gemini model appropriate for your use case.
model := client.GenerativeModel("gemini-2.0-flash")

// Create a prompt using text and the URI reference for the uploaded file.
prompt := []genai.Part{
  genai.FileData{URI: sampleAudio.URI},
  genai.Text("Generate a transcript of the speech."),
}

// Generate content using the prompt.
resp, err := model.GenerateContent(ctx, prompt...)
if err != nil {
  log.Fatal(err)
}

// Handle the response of generated text
for _, c := range resp.Candidates {
  if c.Content != nil {
    fmt.Println(*c.Content)
  }
}

टाइमस्टैंप देखना

MM:SS फ़ॉर्मैट के टाइमस्टैंप का इस्तेमाल करके, ऑडियो फ़ाइल के किसी खास सेक्शन का रेफ़रंस दिया जा सकता है. उदाहरण के लिए, नीचे दिए गए प्रॉम्प्ट में ऐसी ट्रांसक्रिप्ट का अनुरोध किया गया है जिसमें

  • फ़ाइल की शुरुआत से 2 मिनट 30 सेकंड पर शुरू होता है.
  • फ़ाइल शुरू होने के तीन मिनट 29 सेकंड बाद खत्म होती है.

# Create a prompt containing timestamps.
prompt = "Provide a transcript of the speech from 02:30 to 03:29."
// Create a prompt containing timestamps.
const prompt = "Provide a transcript of the speech from 02:30 to 03:29."
// Create a prompt containing timestamps.
prompt := []genai.Part{
    genai.FileData{URI: sampleAudio.URI},
    genai.Text("Provide a transcript of the speech from 02:30 to 03:29."),
}

टोकन की गिनती करना

किसी ऑडियो फ़ाइल में मौजूद टोकन की संख्या जानने के लिए, countTokens तरीके को कॉल करें. उदाहरण के लिए:

response = client.models.count_tokens(
  model='gemini-2.0-flash',
  contents=[myfile]
)

print(response)
import {
  GoogleGenAI,
  createUserContent,
  createPartFromUri,
} from "@google/genai";

const ai = new GoogleGenAI({ apiKey: "GOOGLE_API_KEY" });
const myfile = await ai.files.upload({
  file: "path/to/sample.mp3",
  config: { mimeType: "audio/mpeg" },
});

const countTokensResponse = await ai.models.countTokens({
  model: "gemini-2.0-flash",
  contents: createUserContent([
    createPartFromUri(myfile.uri, myfile.mimeType),
  ]),
});
console.log(countTokensResponse.totalTokens);
tokens, err := model.CountTokens(ctx, genai.FileData{URI: sampleAudio.URI})
if err != nil {
    log.Fatal(err)
}
fmt.Printf("File %s is %d tokens", sampleAudio.DisplayName, tokens.TotalTokens)

इस्तेमाल किए जा सकने वाले ऑडियो फ़ॉर्मैट

Gemini, ऑडियो फ़ॉर्मैट के इन MIME टाइप के साथ काम करता है:

  • WAV - audio/wav
  • MP3 - audio/mp3
  • AIFF - audio/aiff
  • AAC - audio/aac
  • OGG Vorbis - audio/ogg
  • FLAC - audio/flac

ऑडियो के बारे में तकनीकी जानकारी

  • Gemini, ऑडियो के हर सेकंड को 32 टोकन के तौर पर दिखाता है. उदाहरण के लिए, एक मिनट के ऑडियो को 1,920 टोकन के तौर पर दिखाया जाता है.
  • Gemini सिर्फ़ अंग्रेज़ी भाषा में बोली गई बातों के जवाब दे सकता है.
  • Gemini, बोली जाने वाली भाषा के अलावा, पक्षियों के गायन या साइरन जैसी आवाज़ों को "समझ" सकता है.
  • एक प्रॉम्प्ट में ऑडियो डेटा की ज़्यादा से ज़्यादा अवधि 9.5 घंटे हो सकती है. Gemini, एक प्रॉम्प्ट में ऑडियो फ़ाइलों की संख्या पर कोई पाबंदी नहीं लगाता. हालांकि, एक प्रॉम्प्ट में सभी ऑडियो फ़ाइलों की कुल अवधि 9.5 घंटे से ज़्यादा नहीं होनी चाहिए.
  • Gemini, ऑडियो फ़ाइलों को 16 केबीपीएस डेटा रिज़ॉल्यूशन में डाउनसैंपल करता है.
  • अगर ऑडियो सोर्स में एक से ज़्यादा चैनल हैं, तो Gemini उन चैनलों को एक ही चैनल में जोड़ देता है.

आगे क्या करना है

इस गाइड में, ऑडियो डेटा के आधार पर टेक्स्ट जनरेट करने का तरीका बताया गया है. ज़्यादा जानने के लिए, ये संसाधन देखें:

  • फ़ाइल के लिए प्रॉम्प्ट करने की रणनीतियां: Gemini API, टेक्स्ट, इमेज, ऑडियो, और वीडियो डेटा के साथ प्रॉम्प्ट करने की सुविधा देता है. इसे मल्टीमॉडल प्रॉम्प्ट भी कहा जाता है.
  • सिस्टम के निर्देश: सिस्टम के निर्देशों की मदद से, अपनी ज़रूरतों और इस्तेमाल के उदाहरणों के आधार पर, मॉडल के व्यवहार को कंट्रोल किया जा सकता है.
  • सुरक्षा से जुड़े दिशा-निर्देश: कभी-कभी जनरेटिव एआई मॉडल, अनचाहे आउटपुट जनरेट करते हैं. जैसे, गलत, पक्षपातपूर्ण या आपत्तिजनक आउटपुट. ऐसे आउटपुट से होने वाले नुकसान को कम करने के लिए, पोस्ट-प्रोसेसिंग और मानवीय आकलन ज़रूरी है.