Skip to content

Instantly share code, notes, and snippets.

@bwnyasse
Created February 18, 2025 04:03
Show Gist options
  • Save bwnyasse/b85af394c596769531e76129adedaabb to your computer and use it in GitHub Desktop.
Save bwnyasse/b85af394c596769531e76129adedaabb to your computer and use it in GitHub Desktop.
Building a Scrabble Companion with Gemini 2.0 - Move Analysis with Multiple LLMs
class GeminiService {
late GenerativeModel _model;
final ImageStorageService _imageStorage = ImageStorageService();
final FirebaseService _firebaseService = FirebaseService();
GeminiService() {
// Initialize with Gemini 2.0 Flash
_model = FirebaseVertexAI.instance
.generativeModel(model: 'gemini-2.0-flash');
}
Future<Map<String, dynamic>> analyzeBoardImage(
String sessionId,
String imagePath,
) async {
try {
// Read current image bytes
final currentImageBytes = await File(imagePath).readAsBytes();
// Get board state
final boardState = await _firebaseService.getBoardState(sessionId).first;
final isFirstMove = boardState.isEmpty;
if (isFirstMove) {
// Handle first move analysis
final response = await _model.generateContent([
Content.multi([
TextPart(_constructInitialBoardPrompt()),
DataPart('image/jpeg', currentImageBytes),
]),
]);
return {
'status': 'success',
'type': 'initial',
'data': _parseGeminiResponse(response.text!, true),
};
} else {
// Compare with previous state
final response = await _model.generateContent([
Content.multi([
TextPart(_constructImageComparisonPrompt(boardState)),
DataPart('image/jpeg', currentImageBytes),
]),
]);
return {
'status': 'success',
'type': 'move',
'data': _parseGeminiResponse(response.text!, false),
};
}
} catch (e) {
return {
'status': 'error',
'message': e.toString(),
};
}
}
// Prompt construction for initial board analysis
String _constructInitialBoardPrompt() {
return '''
You are analyzing an image of an initial Scrabble board move.
Accurately identify all visible letters and their positions.
Return ONLY a JSON with this format:
{
"board": [
{
"letter": "A",
"row": 7,
"col": 7,
"points": 1
}
]
}
''';
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment