Skip to content

Commit

Permalink
feat: Add AI control parameters and timer initializations across comp…
Browse files Browse the repository at this point in the history
…onents
  • Loading branch information
jacob-i committed Jan 16, 2024
1 parent c1b7127 commit 4f1e41c
Show file tree
Hide file tree
Showing 24 changed files with 225 additions and 220 deletions.
2 changes: 1 addition & 1 deletion app/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ import 'views/screens/talk_screen.dart';

// Entry point of the application
// runApp starts the Flutter app by inflating the given widget and attaching it to the screen
void main() => runApp(TalkApp());
void main() => runApp(const TalkApp());
8 changes: 5 additions & 3 deletions app/lib/models/ai.dart
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class Ai {

// Call the OpenAI API if no matching questions are found locally
if (aiEnabled && OpenAI_API.oat().isNotEmpty) {
networkOperation = await OpenAI_API.getResponseFromOpenAI(message,
networkOperation = OpenAI_API.getResponseFromOpenAI(message,
previousMessages: previousMessages);
String response = await networkOperation!.value;
String poweredTitle = OpenAI_API.model == 'gpt-4'
Expand All @@ -50,7 +50,7 @@ class Ai {
: OpenAI_API.model == 'huggingface'
? HuggingFace_API.model()
: OpenAI_API.model == 'pulzeai'
? '${PulzeAI_API.lastUsedModel()}'
? PulzeAI_API.lastUsedModel()
: '';
return [
Message(
Expand Down Expand Up @@ -118,7 +118,9 @@ class Ai {
// Skip non-matching characters.
if (!matches1[i]) continue;
// Find the next match in s2.
while (!matches2[k]) k++;
while (!matches2[k]) {
k++;
}
// Count transposition if the characters don't match.
if (s1[i] != s2[k]) transpositions++;
k++;
Expand Down
2 changes: 1 addition & 1 deletion app/lib/services/hugging_face_api.dart
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class HuggingFace_API {
static const String _modelKey = 'huggingface_model';
static const String _systemMessageKey = 'huggingface_system_message';
static const String _sendMessagesKey = 'huggingface_send_messages';
static final FlutterSecureStorage _secureStorage = FlutterSecureStorage();
static const FlutterSecureStorage _secureStorage = FlutterSecureStorage();

static String oat() => HuggingFace_API()._oat();
static void setOat(String value) => HuggingFace_API()._setOat(value);
Expand Down
36 changes: 18 additions & 18 deletions app/lib/services/openai_api.dart
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class OpenAI_API {
static const String _modelKey = 'openai_model';
static const String _selectedLanguageKey = 'selected_language';
static const String _systemPromptKey = 'openai_system_prompt';
static final FlutterSecureStorage _secureStorage = FlutterSecureStorage();
static const FlutterSecureStorage _secureStorage = FlutterSecureStorage();

static Future<void> loadOat() async {
try {
Expand Down Expand Up @@ -86,7 +86,7 @@ class OpenAI_API {

final apiKey = OpenAI_API.oat();

final queryUrl = 'https://api.openai.com/v1/images/generations';
const queryUrl = 'https://api.openai.com/v1/images/generations';
final headers = {
'Content-Type': 'application/json',
'Authorization': 'Bearer $apiKey',
Expand Down Expand Up @@ -127,7 +127,7 @@ class OpenAI_API {
}

// Replace this URL with your AWS Lambda function URL
final lambdaUrl = 'YOUR_LAMBDA_FUNCTION_URL';
const lambdaUrl = 'YOUR_LAMBDA_FUNCTION_URL';

final headers = {
'Content-Type': 'application/json',
Expand Down Expand Up @@ -164,7 +164,7 @@ class OpenAI_API {
if (kDebugMode) {
print('isInputSafe exception: $e');
}
throw e;
rethrow;
}
}

Expand Down Expand Up @@ -205,7 +205,7 @@ class OpenAI_API {
String message, {
List<Map<String, String?>> previousMessages = const [],
int maxTries = 1,
String? customSystemPrompt = null,
String? customSystemPrompt,
}) {
// Create a cancelable completer
final completer = CancelableCompleter<String>();
Expand Down Expand Up @@ -253,9 +253,9 @@ class OpenAI_API {
if (HuggingFace_API.oat() != '') {
//print(previousMessages);
String formattedPrevMessages = formatPrevMessages(previousMessages);
if (previousMessages.length > 0 && HuggingFace_API.sendMessages()) {
if (previousMessages.isNotEmpty && HuggingFace_API.sendMessages()) {
finalResponse = await HuggingFace_API.generate(
message + ' previousMessages: ' + formattedPrevMessages);
'$message previousMessages: $formattedPrevMessages');
} else {
finalResponse = await HuggingFace_API.generate(message);
}
Expand All @@ -277,7 +277,7 @@ class OpenAI_API {

// Explicitly return null to avoid

return null;
return;
}

static Future<void> _getResponseFromPulzeAI(
Expand All @@ -290,9 +290,9 @@ class OpenAI_API {
if (PulzeAI_API.oat() != '') {
//print(previousMessages);
String formattedPrevMessages = formatPrevMessages(previousMessages);
if (previousMessages.length > 0 && PulzeAI_API.sendMessages()) {
if (previousMessages.isNotEmpty && PulzeAI_API.sendMessages()) {
finalResponse = await PulzeAI_API.generate(
message + ' previousMessages: ' + formattedPrevMessages);
'$message previousMessages: $formattedPrevMessages');
} else {
finalResponse = await PulzeAI_API.generate(message);
}
Expand All @@ -314,14 +314,14 @@ class OpenAI_API {

// Explicitly return null to avoid

return null;
return;
}

static Future<void> _getResponseFromOpenAI(
String message, CancelableCompleter<String> completer,
{List<Map<String, String?>> previousMessages = const [],
int maxTries = 1,
String? customSystemPrompt = null}) async {
String? customSystemPrompt}) async {
String finalResponse = '';
String inputMessage = message;
int tries = 0;
Expand All @@ -341,7 +341,7 @@ class OpenAI_API {
if (kDebugMode) {
print('inputMessage = $inputMessage');
}
final apiUrl = 'https://api.openai.com/v1/chat/completions';
const apiUrl = 'https://api.openai.com/v1/chat/completions';

final headers = {
'Content-Type': 'application/json',
Expand All @@ -356,7 +356,7 @@ class OpenAI_API {
{
'role': 'system',
'content':
customSystemPrompt == null ? systemPrompt : customSystemPrompt
customSystemPrompt ?? systemPrompt
},
...previousMessages,
{'role': 'user', 'content': inputMessage}
Expand Down Expand Up @@ -422,7 +422,7 @@ class OpenAI_API {
if (tries + 1 < maxTries) {
tries++;
// You can add a delay before retrying the request.
await Future.delayed(Duration(seconds: 2));
await Future.delayed(const Duration(seconds: 2));
} else {
finalResponse =
'Sorry, there was an error processing your request. Please try again later.';
Expand All @@ -438,7 +438,7 @@ class OpenAI_API {

// Explicitly return null to avoid

return null;
return;
}

// Draw to Code Functionality
Expand All @@ -449,7 +449,7 @@ class OpenAI_API {
return 'Enter API key in settings';
}

final systemPrompt = """
const systemPrompt = """
You are a skilled web developer with expertise in Tailwind CSS. A user will provide a low-fidelity wireframe along with descriptive notes. Your task is to create a high-fidelity, responsive HTML webpage using Tailwind CSS and JavaScript, embedded within a single HTML file.
- Embed additional CSS and JavaScript directly in the HTML file.
Expand Down Expand Up @@ -485,7 +485,7 @@ The final output should be a single HTML file, starting with "<html>". Avoid mar
{"type": "text", "text": userPrompt},
{
"type": "image_url",
"image_url": {"url": "data:image/jpeg;base64,${imageBase64}"}
"image_url": {"url": "data:image/jpeg;base64,$imageBase64"}
}
]
}
Expand Down
8 changes: 4 additions & 4 deletions app/lib/services/pulze_ai_api.dart
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class PulzeAI_API {
static const String _modelKey = 'pulze_ai_model';
static const String _systemMessageKey = 'pulze_ai_system_message';
static const String _sendMessagesKey = 'pulze_ai_send_messages';
static final FlutterSecureStorage _secureStorage = FlutterSecureStorage();
static const FlutterSecureStorage _secureStorage = FlutterSecureStorage();

static Future<void> loadOat() async {
try {
Expand Down Expand Up @@ -115,7 +115,7 @@ class PulzeAI_API {

final apiKey = PulzeAI_API.oat();

final queryUrl = 'https://api.pulze.ai/v1/completions/';
const queryUrl = 'https://api.pulze.ai/v1/completions/';
final headers = {
'Content-Type': 'application/json',
'Authorization': 'Bearer $apiKey',
Expand All @@ -126,7 +126,7 @@ class PulzeAI_API {
'model': modelId, // Specify the model
'prompt': _systemMessage == ''
? text
: text + ' [System message]: ' + _systemMessage,
: '$text [System message]: $_systemMessage',
'max_tokens': 300, // Added max_tokens
'temperature': 0, // Added temperature
});
Expand All @@ -148,7 +148,7 @@ class PulzeAI_API {
if (response.statusCode == 307) {
var newUri = response.headers['location'];
if (kDebugMode) {
print('New Uri: ${newUri}');
print('New Uri: $newUri');
}
if (newUri != null) {
response = await http.get(Uri.parse(newUri));
Expand Down
14 changes: 7 additions & 7 deletions app/lib/utils/text_to_speech.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class TextToSpeech {
static const double DEFAULT_SPEECH_RATE = 1.0;
static const double REDUCED_SPEECH_RATE = 0.85;

FlutterTts _flutterTts = FlutterTts();
final FlutterTts _flutterTts = FlutterTts();

static final Map<String, String> _languageCodes = {
'American English': 'en-US',
Expand Down Expand Up @@ -62,15 +62,15 @@ class TextToSpeech {
}

static Map<String, String> get languageCodes => _languageCodes;
static String? lastLanguage = null;
static String? lastLanguage;

Future<void> speakText(String text, {String language = 'en-US'}) async {
if (text == TYPING_INDICATOR) {
return;
}

bool containsLanguageCode =
_languageCodes.keys.any((key) => text.contains(key + ':'));
_languageCodes.keys.any((key) => text.contains('$key:'));

Completer<void> completer = Completer<void>();
_flutterTts.setCompletionHandler(() {
Expand Down Expand Up @@ -100,19 +100,19 @@ class TextToSpeech {
for (String line in lines) {
String currentLanguage = language;
for (final entry in _languageCodes.entries) {
if (line.contains(entry.key + ':')) {
if (line.contains('${entry.key}:')) {
// Speak the part before the colon with the default language.
String beforeColon = line.split(entry.key + ':')[0];
String beforeColon = line.split('${entry.key}:')[0];
if (beforeColon != '') {
await _flutterTts.setLanguage(currentLanguage);
await setSpeechRate(currentLanguage);
await _flutterTts.speak(beforeColon + ' ' + entry.key);
await _flutterTts.speak('$beforeColon ${entry.key}');
await completer.future;
completer = Completer<void>();
}

// Speak the part after the colon with the appropriate language.
String afterColon = line.split(entry.key + ':')[1];
String afterColon = line.split('${entry.key}:')[1];
currentLanguage = entry.value;
await _flutterTts.setLanguage(currentLanguage);
await setSpeechRate(currentLanguage);
Expand Down
4 changes: 2 additions & 2 deletions app/lib/utils/utils_desktop.dart
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,13 @@ class UtilsPlatform {
final ui.Canvas canvas = ui.Canvas(recorder);

// Fill the canvas with a white background
final ui.Paint paintBackground = ui.Paint()..color = ui.Color(0xFFFFFFFF);
final ui.Paint paintBackground = ui.Paint()..color = const ui.Color(0xFFFFFFFF);
canvas.drawRect(ui.Rect.fromLTWH(0, 0, width.toDouble(), height.toDouble()),
paintBackground);

// Draw the lines with a black paint
final ui.Paint paintLines = ui.Paint()
..color = ui.Color(0xFF000000)
..color = const ui.Color(0xFF000000)
..strokeWidth = 2.0;

for (int i = 0; i < points.length - 1; i++) {
Expand Down
4 changes: 2 additions & 2 deletions app/lib/utils/utils_web.dart
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,9 @@ class UtilsPlatform {
}

static Future<void> downloadImage(String url, String description) async {
final windowFeatures =
const windowFeatures =
'menubar=no,toolbar=no,status=no,resizable=yes,scrollbars=yes,width=600,height=400';
html.window.open(url, 'glowby-image-${description}', windowFeatures);
html.window.open(url, 'glowby-image-$description', windowFeatures);
}

static Future<void> initializeState(dynamic f) {
Expand Down
Loading

0 comments on commit 4f1e41c

Please sign in to comment.