|
- import 'dart:convert';
-
- import 'package:http/http.dart' as http;
-
- /// Définit un contrat pour tout service capable d'analyser une image
- /// et de la décrire sous forme de texte.
- abstract class ImageAnalysisService {
- /// Prend une image en base64 et retourne une description textuelle (prompt).
- Future<String> analyzeImage(String base64Image);
- }
-
- /// L'implémentation Ollama de ce service.
- class OllamaImageAnalysisService implements ImageAnalysisService {
- final String _apiUrl = 'http://192.168.20.200:11434/api/generate';
- final String _visionModel = 'llava:7b';
-
- @override
- Future<String> analyzeImage(String base64Image) async {
- print(
- "[OllamaImageAnalysisService] 🚀 Lancement de l'analyse de l'image...");
- final requestPrompt = '''
- As a professional photographe and instagram, describe this imange and give ideas to improve quality to publish on social network.
- ''';
-
- final requestBody = {
- 'model': _visionModel,
- 'prompt': requestPrompt,
- 'images': [base64Image],
- 'stream': false,
- };
-
- try {
- final response = await http.post(
- Uri.parse(_apiUrl),
- headers: {'Content-Type': 'application/json'},
- body: jsonEncode(requestBody),
- ).timeout(const Duration(minutes: 2));
-
- if (response.statusCode == 200) {
- final body = jsonDecode(response.body);
- final generatedPrompt = (body['response'] as String? ?? '')
- .trim()
- .replaceAll('\n', ' ');
- print(
- "[OllamaImageAnalysisService] ✅ Analyse terminée : $generatedPrompt");
- return generatedPrompt;
- } else {
- throw Exception(
- 'Erreur Ollama (analyzeImage) ${response.statusCode}: ${response
- .body}');
- }
- } catch (e) {
- print("[OllamaImageAnalysisService] ❌ Exception : ${e.toString()}");
- rethrow;
- }
- }
- }
|