added language detection controller, edited README, and removed some old code
parent
7e7c8810c8
commit
aea0c9ccec
@ -1,24 +0,0 @@
|
|||||||
import 'package:fluffychat/pangea/choreographer/controllers/it_controller.dart';
|
|
||||||
|
|
||||||
class MlController {
|
|
||||||
final ITController controller;
|
|
||||||
MlController(this.controller);
|
|
||||||
|
|
||||||
// sendPayloads(String message, String messageId) async {
|
|
||||||
// final MessageServiceModel serviceModel = MessageServiceModel(
|
|
||||||
// classId: controller.state!.classId,
|
|
||||||
// roomId: controller.state!.roomId,
|
|
||||||
// message: message.toString(),
|
|
||||||
// messageId: messageId.toString(),
|
|
||||||
// payloadIds: controller.state!.payLoadIds,
|
|
||||||
// userId: controller.state!.userId!,
|
|
||||||
// l1Lang: controller.state!.sourceLangCode,
|
|
||||||
// l2Lang: controller.state!.targetLangCode!,
|
|
||||||
// );
|
|
||||||
// try {
|
|
||||||
// await MessageServiceRepo.sendPayloads(serviceModel);
|
|
||||||
// } catch (err) {
|
|
||||||
// debugPrint('$err in sendPayloads');
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
}
|
|
||||||
@ -0,0 +1,122 @@
|
|||||||
|
import 'dart:async';
|
||||||
|
import 'dart:convert';
|
||||||
|
|
||||||
|
import 'package:fluffychat/pangea/config/environment.dart';
|
||||||
|
import 'package:fluffychat/pangea/controllers/pangea_controller.dart';
|
||||||
|
import 'package:fluffychat/pangea/network/urls.dart';
|
||||||
|
import 'package:http/http.dart' as http;
|
||||||
|
|
||||||
|
import '../network/requests.dart';
|
||||||
|
|
||||||
|
class LanguageDetectionRequest {
|
||||||
|
String fullText;
|
||||||
|
String userL1;
|
||||||
|
String userL2;
|
||||||
|
|
||||||
|
LanguageDetectionRequest({
|
||||||
|
required this.fullText,
|
||||||
|
this.userL1 = "",
|
||||||
|
required this.userL2,
|
||||||
|
});
|
||||||
|
|
||||||
|
Map<String, dynamic> toJson() => {
|
||||||
|
'full_text': fullText,
|
||||||
|
'user_l1': userL1,
|
||||||
|
'user_l2': userL2,
|
||||||
|
};
|
||||||
|
|
||||||
|
@override
|
||||||
|
bool operator ==(Object other) {
|
||||||
|
if (identical(this, other)) return true;
|
||||||
|
return other is LanguageDetectionRequest &&
|
||||||
|
other.fullText == fullText &&
|
||||||
|
other.userL1 == userL1 &&
|
||||||
|
other.userL2 == userL2;
|
||||||
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
int get hashCode => fullText.hashCode ^ userL1.hashCode ^ userL2.hashCode;
|
||||||
|
}
|
||||||
|
|
||||||
|
class LanguageDetectionResponse {
|
||||||
|
List<Map<String, dynamic>> detections;
|
||||||
|
String fullText;
|
||||||
|
|
||||||
|
LanguageDetectionResponse({
|
||||||
|
required this.detections,
|
||||||
|
required this.fullText,
|
||||||
|
});
|
||||||
|
|
||||||
|
factory LanguageDetectionResponse.fromJson(Map<String, dynamic> json) {
|
||||||
|
return LanguageDetectionResponse(
|
||||||
|
detections: List<Map<String, dynamic>>.from(json['detections']),
|
||||||
|
fullText: json['full_text'],
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class _LanguageDetectionCacheItem {
|
||||||
|
Future<LanguageDetectionResponse> data;
|
||||||
|
|
||||||
|
_LanguageDetectionCacheItem({
|
||||||
|
required this.data,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
class LanguageDetectionController {
|
||||||
|
static final Map<LanguageDetectionRequest, _LanguageDetectionCacheItem>
|
||||||
|
_cache = {};
|
||||||
|
late final PangeaController _pangeaController;
|
||||||
|
Timer? _cacheClearTimer;
|
||||||
|
|
||||||
|
LanguageDetectionController(PangeaController pangeaController) {
|
||||||
|
_pangeaController = pangeaController;
|
||||||
|
_initializeCacheClearing();
|
||||||
|
}
|
||||||
|
|
||||||
|
void _initializeCacheClearing() {
|
||||||
|
const duration = Duration(minutes: 15); // Adjust the duration as needed
|
||||||
|
_cacheClearTimer = Timer.periodic(duration, (Timer t) => _clearCache());
|
||||||
|
}
|
||||||
|
|
||||||
|
void _clearCache() {
|
||||||
|
_cache.clear();
|
||||||
|
}
|
||||||
|
|
||||||
|
void dispose() {
|
||||||
|
_cacheClearTimer?.cancel();
|
||||||
|
}
|
||||||
|
|
||||||
|
Future<LanguageDetectionResponse> get(
|
||||||
|
LanguageDetectionRequest params,
|
||||||
|
) async {
|
||||||
|
if (_cache.containsKey(params)) {
|
||||||
|
return _cache[params]!.data;
|
||||||
|
} else {
|
||||||
|
final Future<LanguageDetectionResponse> response = _fetchResponse(
|
||||||
|
await _pangeaController.userController.accessToken,
|
||||||
|
params,
|
||||||
|
);
|
||||||
|
_cache[params] = _LanguageDetectionCacheItem(data: response);
|
||||||
|
return response;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static Future<LanguageDetectionResponse> _fetchResponse(
|
||||||
|
String accessToken,
|
||||||
|
LanguageDetectionRequest params,
|
||||||
|
) async {
|
||||||
|
final Requests request = Requests(
|
||||||
|
choreoApiKey: Environment.choreoApi,
|
||||||
|
accessToken: accessToken,
|
||||||
|
);
|
||||||
|
|
||||||
|
final http.Response res = await request.post(
|
||||||
|
url: PApiUrls.languageDetection,
|
||||||
|
body: params.toJson(),
|
||||||
|
);
|
||||||
|
|
||||||
|
final Map<String, dynamic> json = jsonDecode(res.body);
|
||||||
|
return LanguageDetectionResponse.fromJson(json);
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -1,55 +0,0 @@
|
|||||||
import '../config/environment.dart';
|
|
||||||
import '../network/requests.dart';
|
|
||||||
import '../network/urls.dart';
|
|
||||||
|
|
||||||
class MessageServiceRepo {
|
|
||||||
static Future<void> sendPayloads(
|
|
||||||
MessageServiceModel serviceModel,
|
|
||||||
String messageId,
|
|
||||||
) async {
|
|
||||||
final Requests req = Requests(
|
|
||||||
baseUrl: Environment.choreoApi,
|
|
||||||
choreoApiKey: Environment.choreoApiKey,
|
|
||||||
);
|
|
||||||
|
|
||||||
final json = serviceModel.toJson();
|
|
||||||
json["msg_id"] = messageId;
|
|
||||||
|
|
||||||
await req.post(url: PApiUrls.messageService, body: json);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
class MessageServiceModel {
|
|
||||||
List<int> payloadIds;
|
|
||||||
String? messageId;
|
|
||||||
String message;
|
|
||||||
String userId;
|
|
||||||
String roomId;
|
|
||||||
String? classId;
|
|
||||||
String? l1Lang;
|
|
||||||
String l2Lang;
|
|
||||||
|
|
||||||
MessageServiceModel({
|
|
||||||
required this.payloadIds,
|
|
||||||
required this.messageId,
|
|
||||||
required this.message,
|
|
||||||
required this.userId,
|
|
||||||
required this.roomId,
|
|
||||||
required this.classId,
|
|
||||||
required this.l1Lang,
|
|
||||||
required this.l2Lang,
|
|
||||||
});
|
|
||||||
|
|
||||||
toJson() {
|
|
||||||
return {
|
|
||||||
'payload_ids': payloadIds,
|
|
||||||
'msg_id': messageId,
|
|
||||||
'message': message,
|
|
||||||
'user_id': userId,
|
|
||||||
'room_id': roomId,
|
|
||||||
'class_id': classId,
|
|
||||||
'l1_lang': l1Lang,
|
|
||||||
'l2_lang': l2Lang,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Loading…
Reference in New Issue