debug, small fixes

This commit is contained in:
Dawid Pietrykowski 2024-07-29 22:33:48 +02:00
parent 9f75adb929
commit bc088720ee
4 changed files with 71 additions and 16 deletions

View File

@ -1,4 +1,5 @@
import 'dart:async'; import 'dart:async';
import 'package:gemini_app/config.dart';
import 'package:http/http.dart' as http; import 'package:http/http.dart' as http;
import 'package:flutter_bloc/flutter_bloc.dart'; import 'package:flutter_bloc/flutter_bloc.dart';
@ -11,14 +12,20 @@ class EegState {
required this.mind_wandering, required this.mind_wandering,
required this.focus, required this.focus,
}); });
String getJsonString() {
return '{"mind_wandering": $mind_wandering, "focus": $focus}';
}
} }
class EegCubit extends Cubit<EegState> { class EegCubit extends Cubit<EegState> {
EegCubit() : super(EegState(mind_wandering: 0.0, focus: 0.0)) { EegCubit() : super(EegState(mind_wandering: 0.9, focus: 0.1)) {
// Start the timer when the cubit is created // Start the timer when the cubit is created
if (isDebug) {
startPolling(); startPolling();
} }
}
Timer? _timer; Timer? _timer;
@ -43,6 +50,10 @@ class EegCubit extends Cubit<EegState> {
Future<List<double>> fetchEegData() async { Future<List<double>> fetchEegData() async {
if (isDebug) {
return [0.9, 0.1]; // Placeholder ret
}
final url = Uri.parse('http://192.168.83.153:1234'); final url = Uri.parse('http://192.168.83.153:1234');
try { try {
@ -83,4 +94,13 @@ Future<List<double>> fetchEegData() async {
stopPolling(); stopPolling();
return super.close(); return super.close();
} }
void toggleState() {
// Toggle the state between mind_wandering and focus
if (state.mind_wandering > state.focus) {
updateEegData(state.focus, state.mind_wandering);
} else {
updateEegData(state.mind_wandering, state.focus);
}
}
} }

View File

@ -1,5 +1,7 @@
import 'package:bloc/bloc.dart'; import 'package:bloc/bloc.dart';
import 'package:flutter/services.dart'; import 'package:flutter/services.dart';
import 'package:gemini_app/config.dart';
import 'package:gemini_app/bloc/eeg_state.dart';
import 'package:google_generative_ai/google_generative_ai.dart'; import 'package:google_generative_ai/google_generative_ai.dart';
enum GeminiStatus { initial, loading, success, error } enum GeminiStatus { initial, loading, success, error }
@ -40,7 +42,7 @@ class GeminiState {
class GeminiCubit extends Cubit<GeminiState> { class GeminiCubit extends Cubit<GeminiState> {
GeminiCubit() : super(GeminiState.initialState); GeminiCubit() : super(GeminiState.initialState);
void sendMessage(String prompt) async { void sendMessage(String prompt, EegState eegState) async {
var messagesWithoutPrompt = state.messages; var messagesWithoutPrompt = state.messages;
var messagesWithPrompt = state.messages + [ var messagesWithPrompt = state.messages + [
Content.text(prompt) Content.text(prompt)
@ -59,20 +61,23 @@ void sendMessage(String prompt) async {
const String systemPrmpt = """You are an AI tutor helping students understand topics with help of biometric data. You will be supplied with a json containing data extracted from an EEG device, use that data to modify your approach and help the student learn more effectively. const String systemPrmpt = """You are an AI tutor helping students understand topics with help of biometric data. You will be supplied with a json containing data extracted from an EEG device, use that data to modify your approach and help the student learn more effectively.
Write the response in two parts: Use language: POLISH
State analysis: <describe what is the state of the student and how to best approach them> Write the response in markdown and split it into two parts:
Tutor response: <continue with the lesson, respond to answers, etc>"""; State analysis: describe what is the state of the student and how to best approach them
Tutor response: continue with the lesson, respond to answers, etc""";
final model = GenerativeModel( final model = GenerativeModel(
model: 'gemini-1.5-pro-latest', model: 'gemini-1.5-pro-latest',
apiKey: '', apiKey: geminiApiKey,
safetySettings: safetySettings, safetySettings: safetySettings,
systemInstruction: Content.system(systemPrmpt) systemInstruction: Content.system(systemPrmpt)
); );
try { try {
final chat = model.startChat(history: messagesWithoutPrompt); final chat = model.startChat(history: messagesWithoutPrompt);
final stream = chat.sendMessageStream(Content.text(prompt)); final stream = chat.sendMessageStream(
Content.text("EEG DATA:\n${eegState.getJsonString()}\nPytanie:\n$prompt")
);
String responseText = ''; String responseText = '';
@ -92,4 +97,8 @@ Tutor response: <continue with the lesson, respond to answers, etc>""";
)); ));
} }
} }
void resetConversation() {
emit(GeminiState.initialState);
}
} }

2
lib/config.dart Normal file
View File

@ -0,0 +1,2 @@
const String geminiApiKey = '';
const bool isDebug = true;

View File

@ -35,7 +35,6 @@ class GeminiChatState extends State<GeminiChat> {
void initState() { void initState() {
super.initState(); super.initState();
_startConversation(); _startConversation();
context.read<EegCubit>().startPolling();
} }
@override @override
@ -47,17 +46,26 @@ void dispose() {
void _startConversation() async { void _startConversation() async {
final String rjp = await rootBundle.loadString('assets/lessons/rjp.md'); final String rjp = await rootBundle.loadString('assets/lessons/rjp.md');
print(rjp); print(rjp);
context.read<GeminiCubit>().sendMessage("Zacznij prowadzić lekcje na podstawie poniszego skryptu:\n" + rjp); context.read<GeminiCubit>().sendMessage("Jesteś nauczycielem/chatbotem prowadzącym zajęcia z jednym uczniem. Uczeń ma możliwość zadawania pytań w trakcie, natomiast jesteś odpowiedzialny za prowadzenie lekcji i przedstawienie tematu. Zacznij prowadzić lekcje dla jednego ucznia na podstawie poniszego skryptu:\n" + rjp, context.read<EegCubit>().state);
} }
void _sendMessage() async { void _sendMessage() async {
context.read<GeminiCubit>().sendMessage(_textController.text); context.read<GeminiCubit>().sendMessage(_textController.text, context.read<EegCubit>().state);
_textController.clear(); _textController.clear();
} }
void _toggleEegState() {
context.read<EegCubit>().toggleState();
}
void _resetConversation() {
context.read<GeminiCubit>().resetConversation();
_startConversation();
}
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
return Scaffold( return Scaffold(
resizeToAvoidBottomInset: true,
appBar: AppBar( appBar: AppBar(
title: const Text('Gemini Pro Chat'), title: const Text('Gemini Pro Chat'),
), ),
@ -103,10 +111,26 @@ void dispose() {
), ),
onSubmitted: (_) => _sendMessage(), onSubmitted: (_) => _sendMessage(),
), ),
ElevatedButton( Row(
children: [
Expanded(
child: ElevatedButton(
onPressed: _sendMessage, onPressed: _sendMessage,
child: const Text('Send'), child: const Text('Send'),
), ),
),
const SizedBox(width: 8),
ElevatedButton(
onPressed: _resetConversation,
child: const Text('Reset'),
),
const SizedBox(width: 8),
ElevatedButton(
onPressed: _toggleEegState,
child: const Text('Toggle State'),
),
],
),
], ],
), ),
), ),