[Flutter] Firebase : Build with the Gemini API 로 이미지 전달하기

이건선·2025년 1월 21일
0

Flutter

목록 보기
29/30

1. Firebase Build with Gemini

  1. 파이어 베이스에서 Build with Gemini 선택
  2. 위의 스크린샷에서 'Firebase Blaze' 요금제 계정 등록
    (위의 스크린샷은 이미 등록을 마쳐서 '보기'임)

2. Google Could Console API

파이어 베이스 등록을 마치고 https://console.cloud.google.com/ 에서 'Vertex AI'를 검색, API 허가 한다.

  1. 스크린샷은 이미 허용해서 'API 사용 버튼'이 안보임

3. 프로젝트 라이브러리

  1. 프로젝트에 파이어 베이스 등록 (설명 생략)
  2. 필수 라이브러리 구성
import 'package:firebase_core/firebase_core.dart';
import 'package:firebase_vertexai/firebase_vertexai.dart';
import 'firebase_options.dart';

자세히 알아보기

4. 코드 작성


class _MyHomePageState extends State<MyHomePage> {

  final model = FirebaseVertexAI.instance.generativeModel(model: 'gemini-1.5-flash');

  
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        backgroundColor: Theme.of(context).colorScheme.inversePrimary,
        title: Text(widget.title),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
              ElevatedButton(onPressed: () async {
                // Content 생성 (이미지 + 텍스트 프롬프트)
                final imageUrl =
                    "https://helpx.adobe.com/content/dam/help/en/photoshop/using/quick-actions/remove-background-before-qa1.png";

                final content = Content.multi([
                  FileData('image/png', imageUrl), // 이미지 URL
                  TextPart('Analyze this image and generate relevant tags.') // 텍스트 프롬프트
                ]);

                // generateContent 메서드로 태그화 요청
                final GenerateContentResponse response = await model.generateContent([content]);

                try {
                  // 첫 번째 후보의 텍스트 출력
                  print("1. Text: ---> ${response.text}");


                  // 차단 피드백 출력
                  if (response.promptFeedback != null) {
                    print("2. Block Reason: ---> ${response.promptFeedback?.blockReason}");
                    print("3. Block Message: ---> ${response.promptFeedback?.blockReasonMessage}");
                  }

                  // 함수 호출 정보 출력
                  for (final functionCall in response.functionCalls) {
                    print("4. Function Call: ---> $functionCall");
                  }

                } catch (e) {
                  print("Error: ---> $e");
                }

  }, child: Text("생성"))
          ],
        ),
      ), // This trailing comma makes auto-formatting nicer for build methods.
    );
  }
}

5. 결과


I/flutter ( 7828): 1. Text: ---> This is what I see in the image: A white fluffy dog with its tongue sticking out runs through tall green grass. the dog is in focus and the grass is blurred in the background.  tags: dog, white dog, small dog, dog running, dog in grass, happy dog, dog park, running dog, pet, furry, canine, mammal, pet, playful, adorable, cute, animal, nature, outdoors, grass, green, blur, background, zoom, fast, speed, motion, happy, tongue, spring, summer, field, park,  field, run, sprinting,  active,  energetic, joyful, tail wag,  fluffy, playful, cute,  adorable, happy,  sweet,  loyal, friend, companion, pet, animal, nature.
profile
멋지게 기록하자

0개의 댓글