Update: 2026-05-07 18:16:10
This commit is contained in:
43
musadaq-app/lib/core/services/voice_assistant_service.dart
Normal file
43
musadaq-app/lib/core/services/voice_assistant_service.dart
Normal file
@@ -0,0 +1,43 @@
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:dio/dio.dart';
|
||||
|
||||
import '../network/dio_client.dart';
|
||||
import '../utils/logger.dart';
|
||||
|
||||
class VoiceAssistantService {
|
||||
final Dio _dio = DioClient().client;
|
||||
|
||||
Future<Map<String, dynamic>> processVoiceCommand(File audioFile) async {
|
||||
final fileName = audioFile.uri.pathSegments.isNotEmpty
|
||||
? audioFile.uri.pathSegments.last
|
||||
: 'voice_command.m4a';
|
||||
|
||||
final formData = FormData.fromMap({
|
||||
'audio': await MultipartFile.fromFile(audioFile.path, filename: fileName),
|
||||
});
|
||||
|
||||
final response = await _dio.post(
|
||||
'voice/transcribe',
|
||||
data: formData,
|
||||
options: Options(contentType: 'multipart/form-data'),
|
||||
);
|
||||
|
||||
if (response.data is! Map) {
|
||||
throw Exception('استجابة غير متوقعة من الخادم');
|
||||
}
|
||||
|
||||
final body = Map<String, dynamic>.from(response.data as Map);
|
||||
if (body['success'] != true) {
|
||||
throw Exception((body['message'] ?? 'فشل تنفيذ الأمر الصوتي').toString());
|
||||
}
|
||||
|
||||
final payload = body['data'];
|
||||
if (payload is! Map) {
|
||||
throw Exception('بيانات المساعد الصوتي ناقصة');
|
||||
}
|
||||
|
||||
AppLogger.print('Voice API success: ${body['message']}');
|
||||
return Map<String, dynamic>.from(payload);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user