Skip to content

Commit 7204ee8

Browse files
Merge pull request krayin#2095 from amit-webkul/lead
Update the Magic AI for image support.
2 parents 54c34f8 + 68737b9 commit 7204ee8

File tree

13 files changed

+347
-180
lines changed

13 files changed

+347
-180
lines changed

packages/Webkul/Admin/src/Config/core_config.php

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,13 @@
196196
'title' => 'admin::app.configuration.index.magic-ai.settings.enable',
197197
'type' => 'boolean',
198198
'channel_based' => true,
199+
], [
200+
'name' => 'api_key',
201+
'title' => 'admin::app.configuration.index.magic-ai.settings.api-key',
202+
'type' => 'password',
203+
'depends' => 'enable:1',
204+
'validation' => 'required_if:enable,1',
205+
'info' => 'admin::app.configuration.index.magic-ai.settings.api-key-info',
199206
], [
200207
'name' => 'model',
201208
'title' => 'admin::app.configuration.index.magic-ai.settings.models.title',
@@ -223,13 +230,6 @@
223230
'value' => 'x-ai/grok-2-1212',
224231
],
225232
],
226-
], [
227-
'name' => 'api_key',
228-
'title' => 'admin::app.configuration.index.magic-ai.settings.api-key',
229-
'type' => 'password',
230-
'depends' => 'enable:1',
231-
'validation' => 'required_if:enable,1',
232-
'info' => 'admin::app.configuration.index.magic-ai.settings.api-key-info',
233233
], [
234234
'name' => 'other_model',
235235
'title' => 'admin::app.configuration.index.magic-ai.settings.other',
@@ -249,14 +249,6 @@
249249
'name' => 'enabled',
250250
'title' => 'admin::app.configuration.index.magic-ai.settings.enable',
251251
'type' => 'boolean',
252-
], [
253-
'name' => 'accepted_types',
254-
'type' => 'text',
255-
'title' => 'admin::app.configuration.index.magic-ai.settings.accepted-types',
256-
'info' => 'admin::app.configuration.index.magic-ai.settings.accepted-types-info',
257-
'validation' => 'required_if:enabled,1',
258-
'default' => 'pdf',
259-
'depends' => 'enabled:1',
260252
],
261253
],
262254
],

packages/Webkul/Admin/src/Http/Controllers/Lead/LeadController.php

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use Illuminate\Http\RedirectResponse;
88
use Illuminate\Http\Resources\Json\AnonymousResourceCollection;
99
use Illuminate\Support\Facades\Event;
10+
use Illuminate\Support\Facades\Validator;
1011
use Illuminate\View\View;
1112
use Prettus\Repository\Criteria\RequestCriteria;
1213
use Webkul\Admin\DataGrids\Lead\LeadDataGrid;
@@ -32,6 +33,11 @@
3233

3334
class LeadController extends Controller
3435
{
36+
/**
37+
* Const variable for supported types.
38+
*/
39+
const SUPPORTED_TYPES = 'pdf,bmp,jpeg,jpg,png,webp';
40+
3541
/**
3642
* Create a new controller instance.
3743
*
@@ -637,9 +643,8 @@ public function createByAI()
637643
$leadData = [];
638644

639645
$errorMessages = [];
640-
641646
foreach (request()->file('files') as $file) {
642-
$lead = $this->processFile($file, core()->getConfigData('general.magic_ai.pdf_generation.accepted_types'));
647+
$lead = $this->processFile($file);
643648

644649
if (
645650
isset($lead['status'])
@@ -652,27 +657,18 @@ public function createByAI()
652657
}
653658

654659
if (isset($errorMessages[0]['code'])) {
655-
return response()->json([
656-
'status' => 'error',
657-
'message' => $errorMessages[0]['message'],
658-
]);
660+
return response()->json(MagicAI::errorHandler($errorMessages[0]['message']));
659661
}
660662

661663
if (
662664
empty($leadData)
663665
&& ! empty($errorMessages)
664666
) {
665-
return response()->json([
666-
'status' => 'error',
667-
'message' => implode(', ', $errorMessages),
668-
], 400);
667+
return response()->json(MagicAI::errorHandler(implode(', ', $errorMessages)), 400);
669668
}
670669

671670
if (empty($leadData)) {
672-
return response()->json([
673-
'status' => 'error',
674-
'message' => trans('admin::app.leads.no-valid-files'),
675-
], 400);
671+
return response()->json(MagicAI::errorHandler(trans('admin::app.leads.no-valid-files')), 400);
676672
}
677673

678674
return self::createLeads($leadData);
@@ -684,13 +680,20 @@ public function createByAI()
684680
* @param mixed $file
685681
* @param mixed $supportedFormats
686682
*/
687-
private function processFile($file, $supportedFormats)
683+
private function processFile($file)
688684
{
689-
$this->validate(request(), [
690-
'file' => 'required_in|extensions:'.$supportedFormats.'|mimes:'.$supportedFormats,
691-
]);
685+
$validator = Validator::make(
686+
['file' => $file],
687+
['file' => 'required|extensions:'.str_replace(' ', '', self::SUPPORTED_TYPES)]
688+
);
689+
690+
if ($validator->fails()) {
691+
return MagicAI::errorHandler($validator->errors()->first());
692+
}
693+
694+
$base64Pdf = base64_encode(file_get_contents($file->getRealPath()));
692695

693-
$extractedData = MagicAiService::extractDataFromPdf($file->getPathName());
696+
$extractedData = MagicAIService::extractDataFromFile($base64Pdf);
694697

695698
$lead = MagicAI::mapAIDataToLead($extractedData);
696699

packages/Webkul/Admin/src/Resources/lang/ar/app.php

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1874,11 +1874,17 @@
18741874
'destroy-failed' => 'لا يمكن حذف العميل المحتمل.',
18751875

18761876
'file' => [
1877-
'empty-content' => 'محتوى PDF فارغ أو لم يتمكن من الاستخراج.',
1878-
'invalid-format' => 'تنسيق JSON غير صالح.',
1879-
'invalid-response' => 'تنسيق استجابة الذكاء الاصطناعي غير صالح.',
1880-
'missing-api-key' => 'مفتاح API أو تكوين النموذج مفقود.',
1881-
'not-found' => 'الملف غير موجود.',
1877+
'data-not-found' => 'البيانات غير موجودة.',
1878+
'empty-content' => 'محتوى PDF فارغ أو لم يتم استخراجه.',
1879+
'failed-extract' => 'فشل في استخراج النص من الملف.',
1880+
'insufficient-info' => 'بسبب نقص البيانات، لا يمكننا معالجة طلبك في الوقت الحالي.',
1881+
'invalid-base64' => 'تنسيق base64 غير صالح.',
1882+
'invalid-format' => 'تنسيق JSON غير صالح.',
1883+
'invalid-response' => 'تنسيق استجابة الذكاء الاصطناعي غير صالح.',
1884+
'missing-api-key' => 'مفتاح API أو تكوين النموذج مفقود.',
1885+
'not-found' => 'الملف غير موجود.',
1886+
'recursive-call' => 'تم الكشف عن استدعاء متكرر.',
1887+
'text-generation-failed' => 'فشل استخراج النص. قد يكون الملف فارغًا أو غير قابل للقراءة.',
18821888
],
18831889

18841890
'index' => [
@@ -1948,12 +1954,12 @@
19481954

19491955
'upload' => [
19501956
'create-lead' => 'إنشاء عميل محتمل باستخدام الذكاء الاصطناعي',
1951-
'file' => 'تحميل ملف',
1952-
'file-info' => 'يتم قبول ملفات بصيغة PDF فقط.',
1957+
'file' => 'رفع ملف',
1958+
'file-info' => 'يتم قبول الملفات بتنسيق pdf, bmp, jpg, jpeg, png فقط.',
19531959
'file-required' => 'يرجى اختيار ملف صالح واحد على الأقل للمتابعة.',
1954-
'sample-pdf' => 'نموذج PDF',
1960+
'sample-pdf' => 'نموذج File',
19551961
'save-btn' => 'حفظ',
1956-
'upload-pdf' => 'تحميل PDF',
1962+
'upload-file' => 'رفع الملف',
19571963
],
19581964
],
19591965

@@ -2123,8 +2129,6 @@
21232129
'info' => 'تكوين الذكاء الاصطناعي السحري للتطبيق.',
21242130

21252131
'settings' => [
2126-
'accepted-types' => 'أنواع الملفات المقبولة',
2127-
'accepted-types-info' => 'قائمة بأنواع الملفات المقبولة لإنشاء ملفات PDF، مفصولة بفواصل.',
21282132
'api-key' => 'مفتاح API',
21292133
'api-key-info' => 'تذكر استخدام مفتاح API من OpenRouter لكل نموذج. إنها خطوة بسيطة لتعزيز الأمان والأداء.',
21302134
'enable' => 'تمكين',

packages/Webkul/Admin/src/Resources/lang/en/app.php

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1876,11 +1876,17 @@
18761876
'destroy-failed' => 'Lead can not be deleted.',
18771877

18781878
'file' => [
1879-
'empty-content' => 'PDF content is empty or could not be extracted.',
1880-
'invalid-format' => 'Invalid JSON format.',
1881-
'invalid-response' => 'Invalid AI response format.',
1882-
'missing-api-key' => 'Missing API key or model configuration.',
1883-
'not-found' => 'File not found.',
1879+
'data-not-found' => 'Data not found.',
1880+
'empty-content' => 'PDF content is empty or could not be extracted.',
1881+
'failed-extract' => 'Failed to extract text from file.',
1882+
'insufficient-info' => 'Due to insufficient data, we are unable to process your request at the moment.',
1883+
'invalid-base64' => 'Invalid base64 format.',
1884+
'invalid-format' => 'Invalid JSON format.',
1885+
'invalid-response' => 'Invalid AI response format.',
1886+
'missing-api-key' => 'Missing API key or model configuration.',
1887+
'not-found' => 'File not found.',
1888+
'recursive-call' => 'Recursive call detected.',
1889+
'text-generation-failed' => 'Text extraction failed. The file might be empty or unreadable.',
18841890
],
18851891

18861892
'index' => [
@@ -1951,11 +1957,11 @@
19511957
'upload' => [
19521958
'create-lead' => 'Create Lead Using AI',
19531959
'file' => 'File Upload',
1954-
'file-info' => 'Only PDF format files are accepted.',
1960+
'file-info' => 'Only pdf,bmp,jpg,jpeg,png format files are accepted.',
19551961
'file-required' => 'Please select at least one valid file to proceed.',
19561962
'sample-pdf' => 'Sample PDF',
19571963
'save-btn' => 'Save',
1958-
'upload-pdf' => 'Upload PDF',
1964+
'upload-file' => 'Upload File',
19591965
],
19601966
],
19611967

@@ -2162,8 +2168,6 @@
21622168
'info' => 'Magic AI configuration for the application.',
21632169

21642170
'settings' => [
2165-
'accepted-types' => 'Accepted types',
2166-
'accepted-types-info' => 'Comma separated list of file types that are accepted for PDF generation.',
21672171
'api-key' => 'API Key',
21682172
'api-key-info' => 'Remember to use a OpenRouter API key for each model. It\'s a simple step to enhance security and performance.',
21692173
'enable' => 'Enable',

packages/Webkul/Admin/src/Resources/lang/es/app.php

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1877,11 +1877,17 @@
18771877
'destroy-failed' => 'No se puede eliminar el lead.',
18781878

18791879
'file' => [
1880-
'empty-content' => 'El contenido del PDF está vacío o no se pudo extraer.',
1881-
'invalid-format' => 'Formato JSON inválido.',
1882-
'invalid-response' => 'Formato de respuesta de IA inválido.',
1883-
'missing-api-key' => 'Falta la clave API o la configuración del modelo.',
1884-
'not-found' => 'Archivo no encontrado.',
1880+
'data-not-found' => 'Datos no encontrados.',
1881+
'empty-content' => 'El contenido del PDF está vacío o no se pudo extraer.',
1882+
'failed-extract' => 'No se pudo extraer el texto del archivo.',
1883+
'insufficient-info' => 'Debido a datos insuficientes, no podemos procesar su solicitud en este momento.',
1884+
'invalid-base64' => 'Formato base64 inválido.',
1885+
'invalid-format' => 'Formato JSON inválido.',
1886+
'invalid-response' => 'Formato de respuesta de IA inválido.',
1887+
'missing-api-key' => 'Falta la clave API o la configuración del modelo.',
1888+
'not-found' => 'Archivo no encontrado.',
1889+
'recursive-call' => 'Se detectó una llamada recursiva.',
1890+
'text-generation-failed' => 'La extracción de texto falló. El archivo podría estar vacío o ilegible.',
18851891
],
18861892

18871893
'index' => [
@@ -1951,12 +1957,12 @@
19511957

19521958
'upload' => [
19531959
'create-lead' => 'Crear Lead Usando IA',
1954-
'file' => 'Subir Archivo',
1955-
'file-info' => 'Solo se aceptan archivos en formato PDF.',
1960+
'file' => 'Carga de archivo',
1961+
'file-info' => 'Solo se aceptan archivos en formato pdf, bmp, jpg, jpeg, png.',
19561962
'file-required' => 'Por favor, selecciona al menos un archivo válido para continuar.',
19571963
'sample-pdf' => 'PDF de Muestra',
19581964
'save-btn' => 'Guardar',
1959-
'upload-pdf' => 'Subir PDF',
1965+
'upload-file' => 'Subir archivo',
19601966
],
19611967
],
19621968

@@ -2165,8 +2171,6 @@
21652171
'info' => 'Configuración de Magic AI para la aplicación.',
21662172

21672173
'settings' => [
2168-
'accepted-types' => 'Tipos aceptados',
2169-
'accepted-types-info' => 'Lista separada por comas de tipos de archivos que se aceptan para la generación de PDF.',
21702174
'api-key' => 'Clave API',
21712175
'api-key-info' => 'Recuerda usar una clave API de OpenRouter para cada modelo. Es un paso simple para mejorar la seguridad y el rendimiento.',
21722176
'enable' => 'Habilitar',

packages/Webkul/Admin/src/Resources/lang/fa/app.php

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1878,11 +1878,17 @@
18781878
'destroy-failed' => 'سرنخ قابل حذف نیست.',
18791879

18801880
'file' => [
1881-
'empty-content' => 'محتوای PDF خالی است یا نمی‌توان آن را استخراج کرد.',
1882-
'invalid-format' => 'فرمت JSON نامعتبر است.',
1883-
'invalid-response' => 'فرمت پاسخ AI نامعتبر است.',
1884-
'missing-api-key' => 'کلید API یا پیکربندی مدل گم شده است.',
1885-
'not-found' => 'فایل یافت نشد.',
1881+
'data-not-found' => 'داده‌ای یافت نشد.',
1882+
'empty-content' => 'محتوای PDF خالی است یا نمی‌توان آن را استخراج کرد.',
1883+
'failed-extract' => 'استخراج متن از فایل ناموفق بود.',
1884+
'insufficient-info' => 'به دلیل اطلاعات ناکافی، در حال حاضر نمی‌توانیم درخواست شما را پردازش کنیم.',
1885+
'invalid-base64' => 'فرمت base64 نامعتبر است.',
1886+
'invalid-format' => 'فرمت JSON نامعتبر است.',
1887+
'invalid-response' => 'فرمت پاسخ هوش مصنوعی نامعتبر است.',
1888+
'missing-api-key' => 'کلید API یا پیکربندی مدل مفقود است.',
1889+
'not-found' => 'فایل یافت نشد.',
1890+
'recursive-call' => 'تماس بازگشتی شناسایی شد.',
1891+
'text-generation-failed' => 'استخراج متن ناموفق بود. فایل ممکن است خالی یا غیرقابل خواندن باشد.',
18861892
],
18871893

18881894
'index' => [
@@ -1952,12 +1958,12 @@
19521958

19531959
'upload' => [
19541960
'create-lead' => 'ایجاد سرنخ با استفاده از هوش مصنوعی',
1955-
'file' => 'بارگذاری فایل',
1956-
'file-info' => 'فقط فایل‌های با فرمت PDF پذیرفته می‌شوند.',
1961+
'file' => 'آپلود فایل',
1962+
'file-info' => 'فقط فایل‌های با فرمت pdf, bmp, jpg, jpeg, png پذیرفته می‌شوند.',
19571963
'file-required' => 'لطفاً حداقل یک فایل معتبر برای ادامه انتخاب کنید.',
19581964
'sample-pdf' => 'نمونه PDF',
19591965
'save-btn' => 'ذخیره',
1960-
'upload-pdf' => 'بارگذاری PDF',
1966+
'upload-file' => 'بارگذاری فایل',
19611967
],
19621968
],
19631969

@@ -2166,8 +2172,6 @@
21662172
'info' => 'پیکربندی هوش مصنوعی جادویی برای برنامه.',
21672173

21682174
'settings' => [
2169-
'accepted-types' => 'انواع پذیرفته شده',
2170-
'accepted-types-info' => 'لیست انواع فایل‌های پذیرفته شده برای تولید PDF به صورت جدا شده با کاما.',
21712175
'api-key' => 'کلید API',
21722176
'api-key-info' => 'به یاد داشته باشید که برای هر مدل از کلید API OpenRouter استفاده کنید. این یک گام ساده برای افزایش امنیت و عملکرد است.',
21732177
'enable' => 'فعال کردن',

0 commit comments

Comments
 (0)