Skip to content

Commit

Permalink
updated initla messsage
Browse files Browse the repository at this point in the history
  • Loading branch information
Karol committed Jan 15, 2025
1 parent 7dc6417 commit 5feb1f4
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 80 deletions.
13 changes: 12 additions & 1 deletion app/lib/pages/chat_page.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import '../providers/api_provider.dart';
import '../widgets/chat_bubble.dart';
import '../widgets/button_row.dart'; // Import the ButtonRow widget
import 'package:file_picker/file_picker.dart';
import 'dart:typed_data';
Expand Down Expand Up @@ -72,6 +71,18 @@ class _ChatBodyState extends State<ChatBody> {

return Column(
children: [
if (apiProvider.initialMessage.isNotEmpty)
Padding(
padding: const EdgeInsets.all(16.0),
child: Container(
color: Colors.white,
padding: const EdgeInsets.all(12.0),
child: Text(
apiProvider.initialMessage,
style: const TextStyle(fontSize: 16, color: Colors.black),
),
),
),
ButtonRow(onButtonPressed: _handleButtonPress), // Add the button row
Expanded(
child: Scrollbar(
Expand Down
83 changes: 6 additions & 77 deletions app/lib/providers/api_provider.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@ import '../models/models.dart';
class ApiProvider with ChangeNotifier {
final List<Message> _messages = [];
bool _isLoading = false;
String _initialMessage = '';

List<Message> get messages => _messages;
bool get isLoading => _isLoading;
String get initialMessage => _initialMessage;

ApiProvider() {
_fetchInitialMessage(); // Fetch the initial message when ApiProvider is created
Expand All @@ -23,18 +25,14 @@ class ApiProvider with ChangeNotifier {
var response = await http.get(url);
if (response.statusCode == 200) {
var responseBody = utf8.decode(response.bodyBytes);
_addMessage(Message(
message: responseBody,
isUserMessage: false,
));
_initialMessage = responseBody;
notifyListeners();
} else {
throw Exception('Failed to fetch initial message');
}
} catch (e) {
_addMessage(Message(
message: 'Error fetching initial message: $e',
isUserMessage: false,
));
_initialMessage = 'Error fetching initial message: $e';
notifyListeners();
}
}

Expand Down Expand Up @@ -75,64 +73,6 @@ class ApiProvider with ChangeNotifier {
}
}

Future<void> uploadDocument(Uint8List fileBytes, String fileName) async {
var url = Uri.parse('https://government-assistant-api-183025368636.us-central1.run.app/validate-document');
try {
_setLoading(true);
_addMessage(Message(
message: 'Uploading document...',
isUserMessage: true,
));

// Logowanie wykrytego MIME Type
String mimeType = _detectMimeType(fileName);
print("Detected MIME Type: $mimeType");

var request = http.MultipartRequest('POST', url);
request.files.add(http.MultipartFile.fromBytes(
'file',
fileBytes,
filename: fileName,
contentType: MediaType.parse(mimeType),
));

var streamedResponse = await request.send();
var response = await http.Response.fromStream(streamedResponse);

if (response.statusCode == 200) {
var responseBody = utf8.decode(response.bodyBytes);
var responseData = json.decode(responseBody);

String resultMessage = responseData['content'] ?? 'No content available';
_addMessage(Message(
message: resultMessage,
isUserMessage: false,
isMarkdown: true,
));
} else if (response.statusCode == 400) {
var responseBody = utf8.decode(response.bodyBytes);
var responseData = json.decode(responseBody);

_addMessage(Message(
message: responseData['detail'] ?? 'Unsupported file type.',
isUserMessage: false,
));
} else {
_addMessage(Message(
message: 'Document upload failed: ${response.reasonPhrase}',
isUserMessage: false,
));
}
} catch (e) {
_addMessage(Message(
message: 'Error occurred: $e',
isUserMessage: false,
));
} finally {
_setLoading(false);
}
}

void _addMessage(Message message) {
_messages.add(message);
notifyListeners();
Expand All @@ -142,15 +82,4 @@ class ApiProvider with ChangeNotifier {
_isLoading = isLoading;
notifyListeners();
}

String _detectMimeType(String fileName) {
if (fileName.endsWith('.pdf')) {
return 'application/pdf';
} else if (fileName.endsWith('.docx')) {
return 'application/vnd.openxmlformats-officedocument.wordprocessingml.document';
} else {
print('Unsupported file type: $fileName');
throw Exception('Unsupported file type. Only PDF and DOCX are allowed.');
}
}
}
4 changes: 2 additions & 2 deletions app/lib/widgets/button_row.dart
Original file line number Diff line number Diff line change
Expand Up @@ -49,15 +49,15 @@ class ButtonRow extends StatelessWidget {
),
const SizedBox(width: 10),
ElevatedButton(
onPressed: () => onButtonPressed("Something Else"),
onPressed: () => onButtonPressed("Social Benefits"),
style: ElevatedButton.styleFrom(
backgroundColor: Colors.grey[800],
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(8),
),
minimumSize: const Size(100, 40),
),
child: const Text("Something Else", style: TextStyle(color: Colors.white)),
child: const Text("Social Benefits", style: TextStyle(color: Colors.white)),
),
],
),
Expand Down

0 comments on commit 5feb1f4

Please sign in to comment.