Skip to content

Commit

Permalink
Improved UI, Modified AI query execution, Added form of sentence
Browse files Browse the repository at this point in the history
  • Loading branch information
mateusz-kierepka-hl committed Oct 1, 2024
1 parent 7611ec1 commit 63f8c86
Show file tree
Hide file tree
Showing 9 changed files with 344 additions and 247 deletions.
16 changes: 16 additions & 0 deletions ChatAAC/App.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,22 @@
<FluentTheme />
</Application.Styles>


<TrayIcon.Icons>
<TrayIcons>
<TrayIcon Icon="/Assets/avalonia-logo.ico"
ToolTipText="ChatAAC"
CommandParameter="TrayIconClicked">
<TrayIcon.Menu>
<NativeMenu>
<NativeMenuItem Header="_About" Click="OnAboutClick"/>
<NativeMenuItem Header="_Settings" Click="OnSettingsClick"/>
</NativeMenu>
</TrayIcon.Menu>
</TrayIcon>
</TrayIcons>
</TrayIcon.Icons>

<!-- Add Native Menu -->
<NativeMenu.Menu>
<NativeMenu>
Expand Down
19 changes: 19 additions & 0 deletions ChatAAC/Converters/EqualityConverter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using Avalonia.Data.Converters;
using System;
using System.Globalization;
using Avalonia;

namespace ChatAAC.Converters;

public class EqualityConverter : IValueConverter
{
public object Convert(object? value, Type targetType, object? parameter, CultureInfo culture)
{
return Equals(value?.ToString(), parameter?.ToString());
}

public object? ConvertBack(object? value, Type targetType, object? parameter, CultureInfo culture)
{
return value is bool and true ? parameter : AvaloniaProperty.UnsetValue;
}
}
6 changes: 6 additions & 0 deletions ChatAAC/Models/ChatRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ public class ChatRequest
{
public string Prompt { get; set; } = string.Empty;

public string Form { get; set; } = string.Empty;

public string Tense { get; set; } = string.Empty;

public string Model { get; set; } = string.Empty;

public int Quantity { get; set; }
// Dodaj inne właściwości zgodnie z wymaganiami OllamaSharp
}
9 changes: 6 additions & 3 deletions ChatAAC/Services/OllamaClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@ public class OllamaClient
{
readonly Chat _chat;

public OllamaClient(string apiUrl)
public OllamaClient()
{

Console.WriteLine($"Connecting to {ConfigViewModel.Instance.OllamaAddress} ...");

var ollama = new OllamaApiClient(ConfigViewModel.Instance.OllamaAddress)
{
SelectedModel = "gemma2"
SelectedModel = ConfigViewModel.Instance.SelectedModel ?? "gemma2"
};
_chat = new Chat(ollama);
}
Expand All @@ -34,12 +34,15 @@ public Task<IAsyncEnumerable<string>> ChatAsync(ChatRequest request)
Twoje zadanie:
1. Przeanalizuj podane słowa kluczowe.
2. Stwórz z nich jedno pełne, sensowne zdanie lub krótką wypowiedź.
2. Stwórz z nich jedno pełne, sensowne zdanie lub krótką wypowiedź generowaną do jednej osoby.
3. Upewnij się, że zdanie jest gramatycznie poprawne i zachowuje intencję komunikacyjną użytkownika.
4. Jeśli to konieczne, dodaj odpowiednie słowa łączące lub kontekstowe, aby zdanie brzmiało naturalnie.
5. Nie zmieniaj znaczenia ani nie dodawaj nowych informacji, których nie ma w oryginalnych słowach kluczowych.
6. Jeśli słowa kluczowe sugerują pytanie, sformułuj je jako pytanie.
7. Zachowaj prostotę wypowiedzi, unikając zbyt skomplikowanych konstrukcji.
8. Przyjmij czasowniki dla czasu: {request.Tense}.
9. Tryb formy zdania jako: {request.Form}.
10. Przyjmij, że tam, gdzie jest ilość to jest to: {request.Quantity}.
Odpowiedź:
Podaj tylko wygenerowane zdanie lub krótką wypowiedź, bez żadnych dodatkowych komentarzy czy wyjaśnień.";
Expand Down
32 changes: 17 additions & 15 deletions ChatAAC/ViewModels/ConfigViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public class ConfigViewModel : ReactiveObject
private static readonly object Lock = new object();

private string _ollamaAddress = "http://localhost:11434";
private string? _selectedModel;
private string _selectedModel;
private bool _showSex;
private bool _showViolence;
private bool _showAac;
Expand All @@ -41,7 +41,7 @@ public string OllamaAddress
set => this.RaiseAndSetIfChanged(ref _ollamaAddress, value);
}

public string? SelectedModel
public string SelectedModel
{
get => _selectedModel;
set => this.RaiseAndSetIfChanged(ref _selectedModel, value);
Expand Down Expand Up @@ -103,10 +103,14 @@ public static ConfigViewModel Instance

public ConfigViewModel()
{

LoadConfiguration();
NormalizeOllamaAddress();
SaveCommand = ReactiveCommand.Create(SaveConfiguration);
InitializeData();

if (string.IsNullOrEmpty( _selectedModel))
_selectedModel = "gemma2";
}

private void NormalizeOllamaAddress()
Expand Down Expand Up @@ -156,17 +160,15 @@ private void LoadConfiguration()
PropertyNameCaseInsensitive = true
};
var config = JsonSerializer.Deserialize<ConfigData>(json, options);
if (config != null)
{
OllamaAddress = config.OllamaAddress;
SelectedModel = config.SelectedModel;
ShowSex = config.ShowSex;
ShowViolence = config.ShowViolence;
ShowAac = config.ShowAac;
ShowSchematic = config.ShowSchematic;
SelectedLanguage = config.SelectedLanguage;
LoadedIconsCount = config.LoadedIconsCount;
}
if (config == null) return;
OllamaAddress = config.OllamaAddress;
SelectedModel = config.SelectedModel;
ShowSex = config.ShowSex;
ShowViolence = config.ShowViolence;
ShowAac = config.ShowAac;
ShowSchematic = config.ShowSchematic;
SelectedLanguage = config.SelectedLanguage;
LoadedIconsCount = config.LoadedIconsCount;
}
}

Expand Down Expand Up @@ -201,8 +203,8 @@ public void UpdateLoadedIconsCount(int count)

public class ConfigData
{
public string OllamaAddress { get; set; } = "";
public string? SelectedModel { get; set; }
public string OllamaAddress { get; set; } = string.Empty;
public string SelectedModel { get; set; } = string.Empty;
public bool ShowSex { get; set; }
public bool ShowViolence { get; set; }
public bool ShowAac { get; set; }
Expand Down
121 changes: 104 additions & 17 deletions ChatAAC/ViewModels/MainViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,44 @@ public class MainViewModel : ViewModelBase

public ObservableCollection<Category> Categories { get; set; } = [];
public ObservableCollection<Tag> Tags { get; set; } = [];
// Nowe właściwości dla formy wypowiedzi
private string _selectedTense = "Teraźniejszy";
public string SelectedTense
{
get => _selectedTense;
set => this.RaiseAndSetIfChanged(ref _selectedTense, value);
}

private string _selectedForm = "Oznajmująca";
public string SelectedForm
{
get => _selectedForm;
set => this.RaiseAndSetIfChanged(ref _selectedForm, value);
}

private int _quantity = 1;
public int Quantity
{
get => _quantity;
set => this.RaiseAndSetIfChanged(ref _quantity, value);
}

// Właściwość dla ukrytego paska konfiguracji
private bool _isConfigBarVisible = false;
public bool IsConfigBarVisible
{
get => _isConfigBarVisible;
set => this.RaiseAndSetIfChanged(ref _isConfigBarVisible, value);
}

// Nowe komendy
public ReactiveCommand<Unit, Unit> GenerateAiTextCommand { get; }
public ReactiveCommand<Unit, Unit> ReturnToCategoriesCommand { get; }
public ReactiveCommand<Unit, Unit> OpenSettingsCommand { get; }
public ReactiveCommand<Unit, Unit> ManageCategoriesCommand { get; }
public ReactiveCommand<Unit, Unit> ImportExportCommand { get; }
private readonly PictogramService _pictogramService;
private readonly OllamaClient _ollamaClient = new(ConfigViewModel.Instance.OllamaAddress); // Klient OllamaSharp
private readonly OllamaClient _ollamaClient = new(); // Klient OllamaSharp
private readonly ITtsService _ttsService; // Interfejs TTS
private LoadingWindow? _loadingWindow;

Expand Down Expand Up @@ -119,7 +154,7 @@ public bool IsFullScreen
get => _isFullScreen;
set => this.RaiseAndSetIfChanged(ref _isFullScreen, value);
}

public ReactiveCommand<Category, Unit> SelectCategoryCommand { get; }
private List<Pictogram>? _allPictograms = [];
public IEnumerable<IGrouping<string, Pictogram>> GroupedPictograms =>
Pictograms.GroupBy(p => p.Categories.FirstOrDefault() ?? "Inne");
Expand Down Expand Up @@ -153,7 +188,11 @@ public MainViewModel()
PictogramClickedCommand = ReactiveCommand.CreateFromTask<Pictogram>(OnPictogramClickedAsync);
RemovePictogramCommand = ReactiveCommand.Create<Pictogram>(OnRemovePictogram);
SpeakCommand = ReactiveCommand.Create(OnSpeak);

GenerateAiTextCommand = ReactiveCommand.CreateFromTask(OnGenerateAiTextAsync);
ReturnToCategoriesCommand = ReactiveCommand.Create(OnReturnToCategories);
OpenSettingsCommand = ReactiveCommand.Create(OnOpenSettings);
ManageCategoriesCommand = ReactiveCommand.Create(OnManageCategories);
ImportExportCommand = ReactiveCommand.Create(OnImportExport);
// Tworzenie poleceń ReactiveCommand z ustawionym schedulerem
SendToAiCommand = ReactiveCommand.CreateFromTask(OnSendToAiAsync, outputScheduler: RxApp.MainThreadScheduler);

Expand All @@ -175,17 +214,70 @@ public MainViewModel()
// Subskrypcje na zmiany w SelectedPictograms
SelectedPictograms.CollectionChanged += (_, _) => UpdateConstructedSentence();


SelectCategoryCommand = ReactiveCommand.Create<Category>(category =>
{
SelectedCategory = category;
FilterPictograms();
});

LoadPictogramsAsync();
}

private void OnOpenSettings()
{
var configWindow = new ConfigWindow
{
DataContext = new ConfigViewModel()
};
var mainWindow = (Application.Current?.ApplicationLifetime as IClassicDesktopStyleApplicationLifetime)
?.MainWindow;
if (mainWindow != null)
configWindow.ShowDialog(mainWindow);
else
configWindow.Show();
}

private void OnManageCategories()
{
// Logika zarządzania kategoriami
// Przykład: new CategoryManagementWindow().Show();
}

private void OnImportExport()
{
// Logika importu/eksportu danych
// Przykład: new ImportExportWindow().Show();
}
private async Task OnGenerateAiTextAsync()
{
// Implementacja generowania tekstu z AI na podstawie wybranych ikon
// Możesz wykorzystać istniejącą logikę OnSendToAiAsync


// Uruchomienie AI
await OnSendToAiAsync();

// Jeśli odpowiedź AI została wygenerowana, odczytaj ją
if (!string.IsNullOrWhiteSpace(AiResponse))
{
await OnSpeakAiResponseAsync();
}
}
private void OnReturnToCategories()
{
// Logika powrotu do głównych kategorii
SelectedCategory = Categories.FirstOrDefault(c => c.Id == "core");
FilterPictograms();
}
private async void LoadPictogramsAsync()
{
try
{
IsLoading = true;
Console.WriteLine("Rozpoczynanie pobierania piktogramów...");
_allPictograms = await _pictogramService.GetAllPictogramsAsync().ConfigureAwait(false);
if (_allPictograms != null && _allPictograms.Count > 0)
if (_allPictograms is { Count: > 0 })
{
Console.WriteLine($"Pobrano {_allPictograms.Count} piktogramów.");

Expand Down Expand Up @@ -398,25 +490,17 @@ private void LoadHistory()
}
}

private async Task OnPictogramClickedAsync(Pictogram pictogram)
private Task OnPictogramClickedAsync(Pictogram pictogram)
{
Console.WriteLine(
$"OnPictogramClicked wykonywane na wątku {System.Threading.Thread.CurrentThread.ManagedThreadId}");
if (SelectedPictograms.Contains(pictogram)) return;
if (SelectedPictograms.Contains(pictogram)) return Task.CompletedTask;
SelectedPictograms.Add(pictogram);
Console.WriteLine($"Dodano piktogram: {pictogram.Id}");

// Aktualizacja zdania
UpdateConstructedSentence();

// Uruchomienie AI
await OnSendToAiAsync();

// Jeśli odpowiedź AI została wygenerowana, odczytaj ją
if (!string.IsNullOrWhiteSpace(AiResponse))
{
await OnSpeakAiResponseAsync();
}
return Task.CompletedTask;
}

private void OnRemovePictogram(Pictogram pictogram)
Expand Down Expand Up @@ -470,8 +554,11 @@ private async Task OnSendToAiAsync()
// Tworzenie zapytania do Ollama
var chatRequest = new ChatRequest
{
Model = "llava-phi3",
Prompt = ConstructedSentence
Model = ConfigViewModel.Instance.SelectedModel,
Prompt = ConstructedSentence,
Form = SelectedForm,
Tense = SelectedTense,
Quantity = Quantity
};

var response = await _ollamaClient.ChatAsync(chatRequest).ConfigureAwait(false);
Expand Down
Loading

0 comments on commit 63f8c86

Please sign in to comment.