This commit is contained in:
Hamza-Ayed
2024-05-15 11:02:55 +03:00
parent 2463370b0b
commit 81080ce292
112 changed files with 846 additions and 139 deletions

View File

@@ -1,6 +1,6 @@
import 'dart:convert';
import 'dart:developer';
import 'dart:io';
import 'package:SEFER/controller/functions/crud.dart';
import 'package:get/get.dart';
import 'package:image_cropper/image_cropper.dart';
import 'package:image_picker/image_picker.dart';
@@ -10,13 +10,20 @@ import 'package:path_provider/path_provider.dart';
import '../../constant/api_key.dart';
import '../../constant/colors.dart';
import 'upload_image.dart';
class AI extends GetxController {
final picker = ImagePicker();
Map<String, dynamic> responseMap = {};
Map<String, dynamic> responseCarLicenseMap = {};
Map<String, dynamic> responseBackCarLicenseMap = {};
Map<String, dynamic> responseIdCardeMap = {};
Map<String, dynamic> responseIdCardMap = {};
Map<String, dynamic> responseIdCardDriverEgyptBack = {};
Map<String, dynamic> responseIdCardDriverEgyptFront = {};
Map<String, dynamic> responseIdEgyptFront = {};
Map<String, dynamic> responseIdEgyptBack = {};
Map<String, dynamic> responseIdEgyptDriverLicense = {};
String? responseIdCardDriverEgypt1;
bool isloading = false;
var image;
CroppedFile? croppedFile;
@@ -68,6 +75,24 @@ class AI extends GetxController {
}
}
bool isLoading = false;
Future allMethodForAI(String prompt, linkPHP, imagePath) async {
isLoading = true;
update();
await ImageController().choosImage(linkPHP, imagePath);
Future.delayed(const Duration(seconds: 2));
var extractedString =
await CRUD().arabicTextExtractByVisionAndAI(imagePath: imagePath);
// print(extractedString);
var json = jsonDecode(extractedString);
var textValues = CRUD().extractTextFromLines(json);
print(textValues);
// await Get.put(AI()).geminiAiExtraction(prompt, textValues);
await Get.put(AI()).anthropicAI(textValues, prompt, imagePath);
isLoading = false;
update();
}
Future<void> generateContent() async {
await pickImage();
if (image != null) {
@@ -158,42 +183,102 @@ class AI extends GetxController {
}
}
Future<dynamic> anthropicAI(
String payload, String prompt, String idType) async {
var messagesData = [
{
"role": "user",
"content": [
{"type": "text", "text": "$payload $prompt"}
]
}
];
var requestBody = jsonEncode({
"model": "claude-3-haiku-20240307",
"max_tokens": 1024,
"temperature": 0,
"system": "Json output only without any additional ",
"messages": messagesData,
});
print('requestBody');
print(requestBody);
final response = await http.post(
Uri.parse('https://api.anthropic.com/v1/messages'),
headers: {
'x-api-key':
'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',
'anthropic-version': '2023-06-01',
'content-type': 'application/json'
},
body: requestBody,
);
if (response.statusCode == 200) {
var responseData = jsonDecode(utf8.decode(response.bodyBytes));
// Process the responseData as needed
print(utf8.decode(response.bodyBytes));
if (idType == 'car_back') {
responseIdCardDriverEgyptBack =
jsonDecode(responseData['content'][0]['text']);
print(responseIdCardDriverEgyptBack);
} else if (idType == 'car_front') {
responseIdCardDriverEgyptFront =
jsonDecode(responseData['content'][0]['text']);
print(responseIdCardDriverEgyptFront);
} else if (idType == 'id_front') {
responseIdEgyptFront = jsonDecode(responseData['content'][0]['text']);
print(responseIdEgyptFront);
} else if (idType == 'id_back') {
responseIdEgyptBack = jsonDecode(responseData['content'][0]['text']);
print(responseIdEgyptBack);
} else if (idType == 'driver_license') {
responseIdEgyptDriverLicense =
jsonDecode(responseData['content'][0]['text']);
print(responseIdEgyptDriverLicense);
}
update();
return responseData.toString();
}
return responseIdCardDriverEgyptBack.toString();
}
Future<void> geminiAiExtraction(String prompt, payload) async {
var requestBody = jsonEncode({
'contents': [
"contents": [
{
'parts': [
// {
// 'inlineData': {
// 'mimeType': 'image/jpeg',
// 'data': imageData,
// },
// },
{
'text':
"Extract the desired information from the following passage as json decoded like $prompt .and look for this instruction first name in line 3or 2 ,fullname in line 4 from it written left to right but you modify it rtl,address in line 5 it written left to right but you modify it rtl and 6,dob,nationalid in the last line as just in this:\n\n$payload"
},
],
},
"parts": [
{"text": "$payload $prompt"}
]
}
],
'generationConfig': {
'temperature': 0.4,
'topK': 32,
'topP': 1,
'maxOutputTokens': 4096,
'stopSequences': [],
"generationConfig": {
"temperature": 1,
"topK": 64,
"topP": 0.95,
"maxOutputTokens": 8192,
"stopSequences": []
},
'safety_settings': [
{"category": "HARM_CATEGORY_HARASSMENT", "threshold": "BLOCK_NONE"},
{"category": "HARM_CATEGORY_HATE_SPEECH", "threshold": "BLOCK_NONE"},
"safetySettings": [
{
"category": "HARM_CATEGORY_HARASSMENT",
"threshold": "BLOCK_MEDIUM_AND_ABOVE"
},
{
"category": "HARM_CATEGORY_HATE_SPEECH",
"threshold": "BLOCK_MEDIUM_AND_ABOVE"
},
{
"category": "HARM_CATEGORY_SEXUALLY_EXPLICIT",
"threshold": "BLOCK_NONE"
"threshold": "BLOCK_MEDIUM_AND_ABOVE"
},
{
"category": "HARM_CATEGORY_DANGEROUS_CONTENT",
"threshold": "BLOCK_NONE"
},
"threshold": "BLOCK_MEDIUM_AND_ABOVE"
}
]
});
// print(requestBody);
@@ -201,7 +286,8 @@ class AI extends GetxController {
final response = await http.post(
Uri.parse(
// 'https://generativelanguage.googleapis.com/v1beta/models/gemini-pro-vision:generateContent?key=${AK.geminiApi}'),
'https://generativelanguage.googleapis.com/v1beta/models/gemini-1.5-pro-latest:generateContent?key=AIzaSyCyoLcSkDzK5_SMe00nhut56SSXWPR074w'),
// 'https://generativelanguage.googleapis.com/v1beta/models/gemini-1.5-pro-latest:generateContent?key=${AK.geminiApi}'),
'https://generativelanguage.googleapis.com/v1beta/models/gemini-1.0-pro:generateContent?key=${AK.geminiApi}'),
headers: {'Content-Type': 'application/json'},
body: requestBody,
);
@@ -222,7 +308,10 @@ class AI extends GetxController {
if (jsonString != null) {
// Convert the JSON object to a String
jsonString = jsonEncode(json.decode(jsonString));
print(jsonString);
responseIdCardDriverEgyptBack = jsonDecode(jsonString);
print(responseIdCardDriverEgyptBack);
update();
} else {
print("JSON string not found");
}