Quiz fixes

This commit is contained in:
Dawid Pietrykowski 2024-08-03 22:51:03 +02:00
parent bc088720ee
commit 7158824ffa
8 changed files with 557 additions and 201 deletions

33
assets/lessons/rjp.json Normal file
View File

@ -0,0 +1,33 @@
[
{
"question": "Które z poniższych stwierdzeń opisuje ruch jednostajnie przyspieszony?",
"options": [
"Prędkość obiektu pozostaje stała.",
"Przyspieszenie obiektu jest stałe.",
"Pozycja obiektu zmienia się w regularnych odstępach czasu.",
"Prędkość obiektu zmienia się w sposób losowy."
],
"correctAnswer": 1
},
{
"question": "Które z poniższych równań opisuje ruch jednostajnie przyspieszony?",
"options": [
"v = u",
"a = 0",
"s = ut + ½at²",
"v² = u² - 2as"
],
"correctAnswer": 2
},
{
"question": "Samochód przyspiesza z 0 km/h do 100 km/h w ciągu 10 sekund. Jaka jest wartość przyspieszenia samochodu?",
"options": [
"5 m/s²",
"10 m/s²",
"15 m/s²",
"20 m/s²"
],
"correctAnswer": 1
}
]

View File

@ -2,9 +2,6 @@
**Wstęp:**
* Witam wszystkich. Dzisiaj będziemy rozmawiać o ruchu jednostajnie przyspieszonym.
* Jest to rodzaj ruchu, w którym obiekt porusza się z coraz większą prędkością w regularnych odstępach czasu.
**Definicja ruchu jednostajnie przyspieszanego:**
* Ruch jednostajnie przyspieszony to ruch, w którym przyspieszenie obiektu jest stałe.

3
devtools_options.yaml Normal file
View File

@ -0,0 +1,3 @@
description: This file stores settings for Dart & Flutter DevTools.
documentation: https://docs.flutter.dev/tools/devtools/extensions#configure-extension-enablement-states
extensions:

View File

@ -18,11 +18,10 @@ class EegState {
}
}
class EegCubit extends Cubit<EegState> {
EegCubit() : super(EegState(mind_wandering: 0.9, focus: 0.1)) {
// Start the timer when the cubit is created
if (isDebug) {
if (!isDebug) {
startPolling();
}
}
@ -48,7 +47,6 @@ class EegCubit extends Cubit<EegState> {
});
}
Future<List<double>> fetchEegData() async {
if (isDebug) {
return [0.9, 0.1]; // Placeholder ret
@ -78,7 +76,6 @@ Future<List<double>> fetchEegData() async {
}
}
void stopPolling() {
_timer?.cancel();
_timer = null;

View File

@ -1,40 +1,158 @@
import 'dart:convert';
import 'package:bloc/bloc.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';
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.
At the start you will be provided a script with a lesson to cover.
Keep the analysis and responses short.
Use language: POLISH
After completing the theoretical part there's a quiz, you can start it yourself at the appropriate time or react to users' request by including <QUIZ_START_TOKEN> at the start of your response
Write the response in markdown and split it into two parts (include the tokens):
optional: <QUIZ_START_TOKEN>
<ANALYSIS_START_TOKEN>
here describe what is the state of the student and how to best approach them
<LESSON_START_TOKEN>
here continue with the lesson, respond to answers, etc
""";
enum GeminiStatus { initial, loading, success, error }
enum MessageType { text, image, audio, video }
// enum MessageType { text, image, audio, video }
enum MessageSource { user, agent }
class QuizMessage {
final String content;
final List<String> options;
final int correctAnswer;
int? userAnswer;
QuizMessage({
required this.content,
required this.options,
required this.correctAnswer,
this.userAnswer,
});
}
// class Message {
// final String text;
// final MessageType type;
// final MessageSource source;
// Message({
// required this.text,
// required this.type,
// required this.source,
// });
// }
enum MessageType { text, lessonScript, quizQuestion, quizAnswer }
class Message {
final String text;
final MessageType type;
final MessageSource source;
final List<String>? quizOptions; // Add this for ABCD options
final int? correctAnswer; // Add this for the correct answer index
Message({
required this.text,
required this.type,
required this.source,
this.quizOptions,
this.correctAnswer,
});
static Message fromGeminiContent(Content content) {
if (content.parts.isNotEmpty) {
final part = content.parts.first;
if (part is TextPart) {
return Message(
text: part.text,
type: MessageType.text,
source: content.role == 'model'
? MessageSource.agent
: MessageSource.user,
);
}
}
throw UnsupportedError('Unsupported content type');
}
Content toGeminiContent() {
if (source == MessageSource.user || type == MessageType.lessonScript) {
return Content.text(text);
} else {
return Content.model([TextPart(text)]);
}
}
}
class QuizQuestion {
String question;
List<String> options;
int correctAnswer;
QuizQuestion({
required this.question,
required this.options,
required this.correctAnswer,
});
}
class GeminiState {
GeminiStatus status;
String error;
List<Content> messages;
List<Message> messages;
List<QuizQuestion>? quizQuestions;
bool isQuizMode;
int currentQuizIndex;
GenerativeModel? model;
GeminiState({
required this.status,
required this.error,
GeminiState(
{required this.status,
this.error = '',
this.messages = const [],
});
this.quizQuestions,
this.isQuizMode = false,
this.currentQuizIndex = -1,
this.model});
GeminiState copyWith({
GeminiStatus? status,
String? error,
List<Message>? messages,
List<QuizQuestion>? quizQuestions,
bool? isQuizMode,
int? currentQuizIndex,
GenerativeModel? model,
}) {
return GeminiState(
status: status ?? this.status,
error: error ?? this.error,
messages: messages ?? this.messages,
quizQuestions: quizQuestions ?? this.quizQuestions,
isQuizMode: isQuizMode ?? this.isQuizMode,
currentQuizIndex: currentQuizIndex ?? this.currentQuizIndex,
model: model ?? this.model,
);
}
static GeminiState get initialState => GeminiState(
status: GeminiStatus.initial,
// messages: [Message(text: "Hello, I'm Gemini Pro. How can I help you?", type: MessageType.text, source: MessageSource.agent)],
messages: [Content.model([TextPart("Hello, I'm Gemini Pro. How can I help you?")])],
messages: [
// Message.fromGeminiContent(Content.model(
// [TextPart("Hello, I'm Gemini Pro. How can I help you?")]))
],
error: '',
);
}
@ -42,53 +160,112 @@ class GeminiState {
class GeminiCubit extends Cubit<GeminiState> {
GeminiCubit() : super(GeminiState.initialState);
void sendMessage(String prompt, EegState eegState) async {
var messagesWithoutPrompt = state.messages;
var messagesWithPrompt = state.messages + [
Content.text(prompt)
];
void startLesson(EegState eegState) async {
final quizQuestions = await loadQuizQuestions();
final String rjp = await rootBundle.loadString('assets/lessons/rjp.md');
final String prompt =
"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";
emit(GeminiState(status: GeminiStatus.loading, messages: messagesWithPrompt, error: ''));
final safetySettings = [
SafetySetting(HarmCategory.harassment, HarmBlockThreshold.none),
SafetySetting(HarmCategory.hateSpeech, HarmBlockThreshold.none),
SafetySetting(HarmCategory.sexuallyExplicit, HarmBlockThreshold.none),
SafetySetting(HarmCategory.dangerousContent, HarmBlockThreshold.none),
// SafetySetting(HarmCategory.unspecified, HarmBlockThreshold.none),
];
final String rjp = await rootBundle.loadString('assets/lessons/rjp.md');
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.
Use language: POLISH
Write the response in markdown and split it into two parts:
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(
model: 'gemini-1.5-pro-latest',
apiKey: geminiApiKey,
safetySettings: safetySettings,
systemInstruction: Content.system(systemPrmpt)
systemInstruction: Content.system(systemPrmpt));
Message lessonScriptMessage = Message(
text: prompt,
type: MessageType.lessonScript,
source: MessageSource.agent,
);
GeminiState initialState = GeminiState(
status: GeminiStatus.loading,
error: '',
messages: [lessonScriptMessage],
quizQuestions: quizQuestions,
isQuizMode: false,
model: model);
emit(initialState);
try {
final chat = model.startChat(history: messagesWithoutPrompt);
final stream = chat.sendMessageStream(
Content.text("EEG DATA:\n${eegState.getJsonString()}\nPytanie:\n$prompt")
);
final chat = state.model!.startChat(history: [Content.text(prompt)]);
final stream = chat.sendMessageStream(Content.text(
"EEG DATA:\n${eegState.getJsonString()}\nPytanie:\n$prompt"));
String responseText = '';
await for (final chunk in stream) {
responseText += chunk.text ?? '';
emit(GeminiState(
emit(initialState.copyWith(
status: GeminiStatus.success,
messages: messagesWithPrompt + [Content.model([TextPart(responseText)])],
error: '',
messages: [
lessonScriptMessage,
Message(
source: MessageSource.agent,
text: responseText,
type: MessageType.text)
],
model: model));
}
} catch (e) {
emit(GeminiState(
status: GeminiStatus.error,
messages: state.messages,
error: e.toString(),
));
}
// enterQuizMode();
// sendMessage(prompt, eegState);
}
void sendMessage(String prompt, EegState eegState) async {
List<Message> messagesWithoutPrompt = state.messages;
var messagesWithPrompt = state.messages +
[
Message(
text: prompt, type: MessageType.text, source: MessageSource.user)
];
emit(state.copyWith(
status: GeminiStatus.loading,
messages: messagesWithPrompt,
));
try {
final chat = state.model!.startChat(
history: messagesWithoutPrompt
.map((mess) => mess.toGeminiContent())
.toList());
final stream = chat.sendMessageStream(Content.text(
"EEG DATA:\n${eegState.getJsonString()}\nWiadomość od ucznia:\n$prompt"));
String responseText = '';
await for (final chunk in stream) {
responseText += chunk.text ?? '';
emit(state.copyWith(
status: GeminiStatus.success,
messages: messagesWithPrompt +
[
Message(
source: MessageSource.agent,
text: responseText,
type: MessageType.text)
]));
}
if (responseText.contains("<QUIZ_START_TOKEN>")) {
enterQuizMode();
}
} catch (e) {
emit(GeminiState(
status: GeminiStatus.error,
@ -98,6 +275,73 @@ Tutor response: continue with the lesson, respond to answers, etc""";
}
}
void passAnswerToGemini(int answer) async {
final quizQuestion = state.quizQuestions![state.currentQuizIndex];
final answerMessage = Message(
text: quizQuestion.options[answer],
type: MessageType.quizAnswer,
source: MessageSource.user,
quizOptions: quizQuestion.options,
correctAnswer: quizQuestion.correctAnswer,
);
final List<Message> updatedMessages = [
...state.messages,
answerMessage,
];
emit(state.copyWith(messages: updatedMessages));
askNextQuizQuestion();
}
Future<List<QuizQuestion>> loadQuizQuestions() async {
final String quizJson =
await rootBundle.loadString('assets/lessons/rjp.json');
final List<dynamic> quizData = json.decode(quizJson);
return quizData
.map((question) => QuizQuestion(
question: question['question'],
options: List<String>.from(question['options']),
correctAnswer: question['correctAnswer'],
))
.toList();
}
void enterQuizMode() async {
if (state.isQuizMode) return; // Prevent re-entering quiz mode
askNextQuizQuestion();
}
void askNextQuizQuestion() {
var currentQuizIndex = state.currentQuizIndex + 1;
final quizQuestion = state.quizQuestions![currentQuizIndex];
final quizQuestionMessage = Message(
text: quizQuestion.question,
type: MessageType.quizQuestion,
source: MessageSource.agent,
quizOptions: quizQuestion.options,
correctAnswer: quizQuestion.correctAnswer,
);
final List<Message> updatedMessages = [
...state.messages,
quizQuestionMessage,
];
emit(state.copyWith(
messages: updatedMessages,
isQuizMode: true,
currentQuizIndex: currentQuizIndex));
}
void checkAnswer(int answerIndex) {
passAnswerToGemini(answerIndex);
}
void resetConversation() {
emit(GeminiState.initialState);
}

View File

@ -30,6 +30,7 @@ class GeminiChat extends StatefulWidget {
class GeminiChatState extends State<GeminiChat> {
final _textController = TextEditingController();
bool _quizMode = false; // Add this line
@override
void initState() {
@ -37,6 +38,16 @@ void initState() {
_startConversation();
}
void _toggleQuizMode() {
setState(() {
_quizMode = !_quizMode;
});
}
void _checkAnswer(int answer) {
context.read<GeminiCubit>().checkAnswer(answer);
}
@override
void dispose() {
context.read<EegCubit>().stopPolling();
@ -44,15 +55,16 @@ void dispose() {
}
void _startConversation() async {
final String rjp = await rootBundle.loadString('assets/lessons/rjp.md');
print(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);
context.read<GeminiCubit>().startLesson(context.read<EegCubit>().state);
}
void _sendMessage() async {
context.read<GeminiCubit>().sendMessage(_textController.text, context.read<EegCubit>().state);
context
.read<GeminiCubit>()
.sendMessage(_textController.text, context.read<EegCubit>().state);
_textController.clear();
}
void _toggleEegState() {
context.read<EegCubit>().toggleState();
}
@ -62,18 +74,25 @@ void dispose() {
_startConversation();
}
void _enterQuizMode() {
context.read<GeminiCubit>().enterQuizMode();
}
@override
Widget build(BuildContext context) {
return Scaffold(
resizeToAvoidBottomInset: true,
appBar: AppBar(
title: const Text('Gemini Pro Chat'),
title: BlocBuilder<GeminiCubit, GeminiState>(
builder: (context, state) {
return Text(state.isQuizMode ? 'Quiz Mode' : 'Gemini Pro Chat');
},
),
),
body: Padding(
padding: const EdgeInsets.all(16.0),
child: Column(
children: [
BlocBuilder<EegCubit, EegState>(
builder: (context, eegState) {
return Card(
@ -82,7 +101,8 @@ void dispose() {
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text('Mind Wandering: ${eegState.mind_wandering.toStringAsFixed(2)}'),
Text(
'Mind Wandering: ${eegState.mind_wandering.toStringAsFixed(2)}'),
Text('Focus: ${eegState.focus.toStringAsFixed(2)}'),
],
),
@ -90,7 +110,6 @@ void dispose() {
);
},
),
Expanded(
child: BlocBuilder<GeminiCubit, GeminiState>(
builder: (context, state) {
@ -104,20 +123,32 @@ void dispose() {
},
),
),
TextField(
BlocBuilder<GeminiCubit, GeminiState>(
builder: (context, state) {
return state.isQuizMode
? Container() // Hide text input in quiz mode
: TextField(
controller: _textController,
decoration: const InputDecoration(
hintText: 'Enter your message',
),
onSubmitted: (_) => _sendMessage(),
);
},
),
Row(
children: [
Expanded(
BlocBuilder<GeminiCubit, GeminiState>(
builder: (context, state) {
return state.isQuizMode
? Container()
: Expanded(
child: ElevatedButton(
onPressed: _sendMessage,
child: const Text('Send'),
),
);
},
),
const SizedBox(width: 8),
ElevatedButton(
@ -129,6 +160,17 @@ void dispose() {
onPressed: _toggleEegState,
child: const Text('Toggle State'),
),
const SizedBox(width: 8),
BlocBuilder<GeminiCubit, GeminiState>(
builder: (context, state) {
return state.isQuizMode
? Container()
: ElevatedButton(
onPressed: _enterQuizMode,
child: const Text('Start Quiz'),
);
},
),
],
),
],
@ -142,9 +184,9 @@ ListView buildChatList(GeminiState state, {bool loading = false}) {
itemCount: state.messages.length + (loading ? 1 : 0),
itemBuilder: (context, index) {
if (index == state.messages.length && loading) {
return Card(
return const Card(
child: Padding(
padding: const EdgeInsets.all(16.0),
padding: EdgeInsets.all(16.0),
child: Align(
alignment: Alignment.centerLeft,
child: BouncingDots(),
@ -153,34 +195,72 @@ ListView buildChatList(GeminiState state, {bool loading = false}) {
);
}
final message = state.messages[index];
if (state.messages[index].type == MessageType.lessonScript) {
// skip
return Container();
}
String text = "";
for (var part in message.parts) {
if (part is TextPart) {
text += part.text;
}
}
final message = state.messages[index];
// String text = message.parts.whereType<TextPart>().map((part) => part.text).join();
String text = message.text;
if (message.type == MessageType.quizQuestion) {
return Card(
child: Padding(
padding: const EdgeInsets.all(8.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
MarkdownBody(data: text),
...message.quizOptions!.asMap().entries.map((entry) {
return ElevatedButton(
onPressed: () => {_checkAnswer(entry.key)},
child: Text(entry.value),
);
}),
],
),
),
);
} else if (message.type == MessageType.quizAnswer) {
bool correct = message.text == message.correctAnswer;
var text = Text(
correct
? "Correct!"
: "Incorrect. The correct answer was: ${message.quizOptions![message.correctAnswer!]}",
style: TextStyle(
color: correct ? Colors.green : Colors.red,
fontWeight: FontWeight.bold,
),
);
return Card(
child: Padding(
padding: const EdgeInsets.all(8.0),
child: Column(
crossAxisAlignment: message.role != 'user'
? CrossAxisAlignment.start
: CrossAxisAlignment.end,
children: [
MarkdownBody(data: text),
],
crossAxisAlignment: CrossAxisAlignment.start,
children: [text],
),
),
);
} else {
return Card(
child: Padding(
padding: const EdgeInsets.all(8.0),
child: Column(
crossAxisAlignment: message.source == MessageSource.agent
? CrossAxisAlignment.start
: CrossAxisAlignment.end,
children: [MarkdownBody(data: text)],
),
),
);
}
},
);
}
}
class BouncingDots extends StatefulWidget {
const BouncingDots({super.key});
@ -188,7 +268,8 @@ class BouncingDots extends StatefulWidget {
BouncingDotsState createState() => BouncingDotsState();
}
class BouncingDotsState extends State<BouncingDots> with TickerProviderStateMixin {
class BouncingDotsState extends State<BouncingDots>
with TickerProviderStateMixin {
late List<AnimationController> _controllers;
late List<Animation<double>> _animations;
@ -202,11 +283,11 @@ class BouncingDotsState extends State<BouncingDots> with TickerProviderStateMixi
vsync: this,
),
);
_animations = _controllers.map((controller) =>
Tween<double>(begin: 0, end: -10).animate(
_animations = _controllers
.map((controller) => Tween<double>(begin: 0, end: -10).animate(
CurvedAnimation(parent: controller, curve: Curves.easeInOut),
)
).toList();
))
.toList();
for (var i = 0; i < 3; i++) {
Future.delayed(Duration(milliseconds: i * 180), () {

View File

@ -70,6 +70,7 @@ flutter:
assets:
- assets/lessons/rjp.md
- assets/lessons/rjp.json
# An image asset can refer to one or more resolution-specific "variants", see
# https://flutter.dev/assets-and-images/#resolution-aware