파이어 베이스 등록을 마치고 https://console.cloud.google.com/ 에서 'Vertex AI'를 검색, API 허가 한다.
import 'package:firebase_core/firebase_core.dart';
import 'package:firebase_vertexai/firebase_vertexai.dart';
import 'firebase_options.dart';
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.
);
}
}
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.