Gemini API का इस्तेमाल करके, ऑडियो फ़ाइलों का विश्लेषण करना

Gemini मॉडल से, ऑडियो फ़ाइलों का विश्लेषण करने के लिए कहा जा सकता है. इन फ़ाइलों को इनलाइन (base64-encoded) या यूआरएल के ज़रिए उपलब्ध कराया जा सकता है. जब आप Firebase AI Logic, का इस्तेमाल करते हैं, तो अपने ऐप्लिकेशन से सीधे यह अनुरोध किया जा सकता है.

इस सुविधा की मदद से, ये काम किए जा सकते हैं:

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

कोड के सैंपल पर जाएं स्ट्रीम किए गए जवाबों के लिए कोड पर जाएं


ऑडियो के साथ काम करने के अन्य विकल्पों के बारे में जानने के लिए, अन्य गाइड देखें
स्ट्रक्चर्ड आउटपुट जनरेट करना एक से ज़्यादा बार बातचीत करना दोनों दिशाओं में स्ट्रीमिंग

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

इस पेज पर, अपने चुने गए Gemini API प्रोवाइडर के हिसाब से कॉन्टेंट और कोड देखने के लिए, उसके नाम पर क्लिक करें.

अगर आपने अब तक, शुरू करने के लिए गाइड में दिए गए निर्देशों का पालन नहीं किया है, तो उन्हें पूरा करें. इसमें बताया गया है कि Firebase प्रोजेक्ट कैसे सेट अप करें, अपने ऐप्लिकेशन को Firebase से कैसे कनेक्ट करें, एसडीके कैसे जोड़ें, अपने चुने गए Gemini API प्रोवाइडर के लिए बैकएंड सेवा को कैसे शुरू करें, और GenerativeModel इंस्टेंस कैसे बनाएं.

हम आपको Google AI Studio का इस्तेमाल करने का सुझाव देते हैं. इसकी मदद से, प्रॉम्प्ट की जांच की जा सकती है और उनमें बदलाव किया जा सकता है.Google AI Studio.

base64-encoded ऑडियो फ़ाइलों से टेक्स्ट जनरेट करना

इस सैंपल को आज़माने से पहले, अपने प्रोजेक्ट और ऐप्लिकेशन को सेट अप करने के लिए, इस गाइड का शुरू करने से पहले सेक्शन पूरा करें.
उस सेक्शन में, आपको Gemini API के अपने चुने हुए Gemini API प्रोवाइडर के लिए एक बटन पर भी क्लिक करना होगा, ताकि आपको इस पेज पर, प्रोवाइडर के हिसाब से कॉन्टेंट दिखे.

Gemini मॉडल से, टेक्स्ट और ऑडियो के साथ प्रॉम्प्ट करके टेक्स्ट जनरेट करने के लिए कहा जा सकता है. इसके लिए, इनपुट फ़ाइल का mimeType और फ़ाइल उपलब्ध कराएं. इनपुट फ़ाइलों के लिए ज़रूरी शर्तें और सुझाव, इस पेज पर आगे दिए गए हैं.

Swift

टेक्स्ट और एक ऑडियो फ़ाइल के मल्टीमॉडल इनपुट से टेक्स्ट जनरेट करने के लिए, generateContent() को कॉल किया जा सकता है.


import FirebaseAILogic

// Initialize the Gemini Developer API backend service.
let ai = FirebaseAI.firebaseAI(backend: .googleAI())

// Create a `GenerativeModel` instance with a model that supports your use case.
let model = ai.generativeModel(modelName: "gemini-3.5-flash")


// Provide the audio as `Data`
guard let audioData = try? Data(contentsOf: audioURL) else {
    print("Error loading audio data.")
    return // Or handle the error appropriately
}

// Specify the appropriate audio MIME type
let audio = InlineDataPart(data: audioData, mimeType: "audio/mpeg")


// Provide a text prompt to include with the audio
let prompt = "Transcribe what's said in this audio recording."

// To generate text output, call `generateContent` with the audio and text prompt
let response = try await model.generateContent(audio, prompt)

// Print the generated text, handling the case where it might be nil
print(response.text ?? "No text in response.")

Kotlin

टेक्स्ट और एक ऑडियो फ़ाइल के मल्टीमॉडल इनपुट से टेक्स्ट जनरेट करने के लिए, generateContent() को कॉल किया जा सकता है.

Kotlin के लिए, इस एसडीके में मौजूद तरीके, सस्पेंड फ़ंक्शन होते हैं. इन्हें कोरोटीन स्कोप से कॉल करना ज़रूरी है.

// Initialize the Gemini Developer API backend service.
// Create a `GenerativeModel` instance with a model that supports your use case.
val model = Firebase.ai(backend = GenerativeBackend.googleAI())
                        .generativeModel("gemini-3.5-flash")


val contentResolver = applicationContext.contentResolver

val inputStream = contentResolver.openInputStream(audioUri)

if (inputStream != null) {  // Check if the audio loaded successfully
    inputStream.use { stream ->
        val bytes = stream.readBytes()

        // Provide a prompt that includes the audio specified above and text
        val prompt = content {
            inlineData(bytes, "audio/mpeg")  // Specify the appropriate audio MIME type
            text("Transcribe what's said in this audio recording.")
        }

        // To generate text output, call `generateContent` with the prompt
        val response = model.generateContent(prompt)

        // Log the generated text, handling the case where it might be null
        Log.d(TAG, response.text?: "")
    }
} else {
    Log.e(TAG, "Error getting input stream for audio.")
    // Handle the error appropriately
}

Java

टेक्स्ट और एक ऑडियो फ़ाइल के मल्टीमॉडल इनपुट से टेक्स्ट जनरेट करने के लिए, generateContent() को कॉल किया जा सकता है.

Java के लिए, इस एसडीके में मौजूद तरीके, ListenableFuture दिखाते हैं.

// Initialize the Gemini Developer API backend service.
// Create a `GenerativeModel` instance with a model that supports your use case.
GenerativeModel ai = FirebaseAI.getInstance(GenerativeBackend.googleAI())
        .generativeModel("gemini-3.5-flash");

// Use the GenerativeModelFutures Java compatibility layer which offers
// support for ListenableFuture and Publisher APIs
GenerativeModelFutures model = GenerativeModelFutures.from(ai);


ContentResolver resolver = getApplicationContext().getContentResolver();

try (InputStream stream = resolver.openInputStream(audioUri)) {
    File audioFile = new File(new URI(audioUri.toString()));
    int audioSize = (int) audioFile.length();
    byte audioBytes = new byte[audioSize];
    if (stream != null) {
        stream.read(audioBytes, 0, audioBytes.length);
        stream.close();

        // Provide a prompt that includes the audio specified above and text
        Content prompt = new Content.Builder()
              .addInlineData(audioBytes, "audio/mpeg")  // Specify the appropriate audio MIME type
              .addText("Transcribe what's said in this audio recording.")
              .build();

        // To generate text output, call `generateContent` with the prompt
        ListenableFuture<GenerateContentResponse> response = model.generateContent(prompt);
        Futures.addCallback(response, new FutureCallback<GenerateContentResponse>() {
            @Override
            public void onSuccess(GenerateContentResponse result) {
                String text = result.getText();
                Log.d(TAG, (text == null) ? "" : text);
            }
            @Override
            public void onFailure(Throwable t) {
                Log.e(TAG, "Failed to generate a response", t);
            }
        }, executor);
    } else {
        Log.e(TAG, "Error getting input stream for file.");
        // Handle the error appropriately
    }
} catch (IOException e) {
    Log.e(TAG, "Failed to read the audio file", e);
} catch (URISyntaxException e) {
    Log.e(TAG, "Invalid audio file", e);
}

Web

टेक्स्ट और एक ऑडियो फ़ाइल के मल्टीमॉडल इनपुट से टेक्स्ट जनरेट करने के लिए, generateContent() को कॉल किया जा सकता है.


import { initializeApp } from "firebase/app";
import { getAI, getGenerativeModel, GoogleAIBackend } from "firebase/ai";

// TODO(developer) Replace the following with your app's Firebase configuration
// See: https://firebase.google.com/docs/web/learn-more#config-object
const firebaseConfig = {
  // ...
};

// Initialize FirebaseApp
const firebaseApp = initializeApp(firebaseConfig);

// Initialize the Gemini Developer API backend service.
const ai = getAI(firebaseApp, { backend: new GoogleAIBackend() });

// Create a `GenerativeModel` instance with a model that supports your use case.
const model = getGenerativeModel(ai, { model: "gemini-3.5-flash" });


// Converts a File object to a Part object.
async function fileToGenerativePart(file) {
  const base64EncodedDataPromise = new Promise((resolve) => {
    const reader = new FileReader();
    reader.onloadend = () => resolve(reader.result.split(','));
    reader.readAsDataURL(file);
  });
  return {
    inlineData: { data: await base64EncodedDataPromise, mimeType: file.type },
  };
}

async function run() {
  // Provide a text prompt to include with the audio
  const prompt = "Transcribe what's said in this audio recording.";

  // Prepare audio for input
  const fileInputEl = document.querySelector("input[type=file]");
  const audioPart = await fileToGenerativePart(fileInputEl.files);

  // To generate text output, call `generateContent` with the text and audio
  const result = await model.generateContent([prompt, audioPart]);

  // Log the generated text, handling the case where it might be undefined
  console.log(result.response.text() ?? "No text in response.");
}

run();

Dart

टेक्स्ट और एक ऑडियो फ़ाइल के मल्टीमॉडल इनपुट से टेक्स्ट जनरेट करने के लिए, generateContent() को कॉल किया जा सकता है.


import 'package:firebase_ai/firebase_ai.dart';
import 'package:firebase_core/firebase_core.dart';
import 'firebase_options.dart';

// Initialize FirebaseApp
await Firebase.initializeApp(
  options: DefaultFirebaseOptions.currentPlatform,
);

// Initialize the Gemini Developer API backend service.
// Create a `GenerativeModel` instance with a model that supports your use case.
final model =
      FirebaseAI.googleAI().generativeModel(model: 'gemini-3.5-flash');


// Provide a text prompt to include with the audio
final prompt = TextPart("Transcribe what's said in this audio recording.");

// Prepare audio for input
final audio = await File('audio0.mp3').readAsBytes();

// Provide the audio as `Data` with the appropriate audio MIME type
final audioPart = InlineDataPart('audio/mpeg', audio);

// To generate text output, call `generateContent` with the text and audio
final response = await model.generateContent([
  Content.multi([prompt,audioPart])
]);

// Print the generated text
print(response.text);

Unity

टेक्स्ट और एक ऑडियो फ़ाइल के मल्टीमॉडल इनपुट से टेक्स्ट जनरेट करने के लिए, GenerateContentAsync() को कॉल किया जा सकता है.


using Firebase;
using Firebase.AI;

// Initialize the Gemini Developer API backend service.
var ai = FirebaseAI.GetInstance(FirebaseAI.Backend.GoogleAI());

// Create a `GenerativeModel` instance with a model that supports your use case.
var model = ai.GetGenerativeModel(modelName: "gemini-3.5-flash");


// Provide a text prompt to include with the audio
var prompt = ModelContent.Text("Transcribe what's said in this audio recording.");

// Provide the audio as `data` with the appropriate audio MIME type
var audio = ModelContent.InlineData("audio/mpeg",
      System.IO.File.ReadAllBytes(System.IO.Path.Combine(
        UnityEngine.Application.streamingAssetsPath, "audio0.mp3")));

// To generate text output, call `GenerateContentAsync` with the text and audio
var response = await model.GenerateContentAsync(new [] { prompt, audio });

// Print the generated text
UnityEngine.Debug.Log(response.Text ?? "No text in response.");

अपने इस्तेमाल के उदाहरण और ऐप्लिकेशन के हिसाब से मॉडल चुनने का तरीका जानें. मॉडल की जगह चुनना ज़रूरी नहीं है.

जवाब को स्ट्रीम करना

इस सैंपल को आज़माने से पहले, अपने प्रोजेक्ट और ऐप्लिकेशन को सेट अप करने के लिए, इस गाइड का शुरू करने से पहले सेक्शन पूरा करें.
उस सेक्शन में, आपको Gemini API के अपने चुने हुए Gemini API प्रोवाइडर के लिए एक बटन पर भी क्लिक करना होगा, ताकि आपको इस पेज पर, प्रोवाइडर के हिसाब से कॉन्टेंट दिखे.

मॉडल जनरेशन से पूरा नतीजा मिलने का इंतज़ार न करके, स्ट्रीमिंग का इस्तेमाल करके आंशिक नतीजों को मैनेज किया जा सकता है. इससे, इंटरैक्शन को तेज़ किया जा सकता है. जवाब को स्ट्रीम करने के लिए, generateContentStream को कॉल करें.



इनपुट ऑडियो फ़ाइलों के लिए ज़रूरी शर्तें और सुझाव

ध्यान दें कि इनलाइन डेटा के तौर पर उपलब्ध कराई गई फ़ाइल को ट्रांज़िट में base64 में एनकोड किया जाता है. इससे, अनुरोध का साइज़ बढ़ जाता है. अगर कोई अनुरोध बहुत बड़ा है, तो आपको एचटीटीपी 413 गड़बड़ी का मैसेज मिलता है.

इनके बारे में ज़्यादा जानकारी पाने के लिए, "इनपुट के तौर पर इस्तेमाल की जा सकने वाली फ़ाइलें और ज़रूरी शर्तें" पेज देखें:

ऑडियो के लिए इस्तेमाल किए जा सकने वाले MIME टाइप

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

  • AAC - audio/aac
  • FLAC - audio/flac
  • MP3 - audio/mp3
  • MPA - audio/m4a
  • MPEG - audio/mpeg
  • MPGA - audio/mpga
  • MP4 - audio/mp4
  • OPUS - audio/opus
  • PCM - audio/pcm
  • WAV - audio/wav
  • WEBM - audio/webm

हर अनुरोध के लिए सीमाएं

हर अनुरोध में ज़्यादा से ज़्यादा फ़ाइलें: एक ऑडियो फ़ाइल



तुम और क्या कर सकती हो?

अन्य सुविधाओं को आज़माएं

कॉन्टेंट जनरेशन को कंट्रोल करने का तरीका जानें

Google AI Studio का इस्तेमाल करके, प्रॉम्प्ट और मॉडल कॉन्फ़िगरेशन के साथ एक्सपेरिमेंट भी किया जा सकता है. साथ ही, जनरेट किया गया कोड स्निपेट भी पाया जा सकता है. Google AI Studio.

इस्तेमाल किए जा सकने वाले मॉडल के बारे में ज़्यादा जानें

अलग-अलग इस्तेमाल के उदाहरणों के लिए उपलब्ध मॉडल , उनके कोटों और कीमत के बारे में जानें.


सुझाव/राय देना या शिकायत करना के बारे में अपने अनुभव के साथ Firebase AI Logic