Skip to content

Commit

Permalink
updated config window
Browse files Browse the repository at this point in the history
Added better model handling
  • Loading branch information
mateusz-kierepka-hl committed Sep 27, 2024
1 parent 8fade4d commit 4e1e63d
Show file tree
Hide file tree
Showing 13 changed files with 382 additions and 75 deletions.
1 change: 1 addition & 0 deletions .idea/.idea.ChatAAC/.idea/avalonia.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 12 additions & 3 deletions ChatAAC/App.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,19 @@
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Class="ChatAAC.App"
xmlns:local="using:ChatAAC"
RequestedThemeVariant="Default">
<!-- "Default" ThemeVariant follows system theme variant. "Dark" or "Light" are other available options. -->

RequestedThemeVariant="Default"
Name="Chat AAC"
x:DataType="local:App">

<Application.Styles>
<FluentTheme />
</Application.Styles>

<!-- Add Native Menu -->
<NativeMenu.Menu>
<NativeMenu>
<NativeMenuItem Header="_About" Click="OnAboutClick"/>
<NativeMenuItem Header="_Settings" Click="OnSettingsClick"/>
</NativeMenu>
</NativeMenu.Menu>
</Application>
52 changes: 46 additions & 6 deletions ChatAAC/App.axaml.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
using System;
using Avalonia;
using Avalonia.Controls.ApplicationLifetimes;
using Avalonia.Markup.Xaml;
using ChatAAC.Views;
using ReactiveUI;
using Avalonia.ReactiveUI;

using ChatAAC.ViewModels;

namespace ChatAAC;

public partial class App : Application
public class App : Application
{
public override void Initialize()
{
Expand All @@ -21,12 +22,51 @@ public override void OnFrameworkInitializationCompleted()
{
if (ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop)
{
// Line below is needed to remove Avalonia data validation.
// Without this line you will get duplicate validations from both Avalonia and CT
//BindingPlugins.DataValidators.RemoveAt(0);
desktop.MainWindow = new MainWindow();
desktop.MainWindow = new MainWindow
{
DataContext = new MainViewModel()
};
}

base.OnFrameworkInitializationCompleted();
}

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

}

private void OpenAboutWindow()
{
var aboutWindow = new AboutWindow
{
DataContext = new AboutViewModel()
};
var mainWindow = (Application.Current?.ApplicationLifetime as IClassicDesktopStyleApplicationLifetime)
?.MainWindow;
if (mainWindow != null)
aboutWindow.ShowDialog(mainWindow);
else
aboutWindow.Show();
}

private void OnAboutClick(object? sender, EventArgs e)
{
OpenAboutWindow();
}

private void OnSettingsClick(object? sender, EventArgs e)
{
OpenConfigWindow();
}
}
4 changes: 2 additions & 2 deletions ChatAAC/Services/MacTtsService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public async Task SpeakAsync(string text)
if (string.IsNullOrWhiteSpace(text))
throw new ArgumentException("Tekst do odczytania nie może być pusty.", nameof(text));

if (!IsMacOS())
if (!IsMacOs())
throw new PlatformNotSupportedException("TTS za pomocą 'say' jest wspierane tylko na macOS.");

var processStartInfo = new ProcessStartInfo
Expand Down Expand Up @@ -51,7 +51,7 @@ public async Task SpeakAsync(string text)
}
}

private bool IsMacOS()
private bool IsMacOs()
{
return RuntimeInformation.IsOSPlatform(OSPlatform.OSX);
}
Expand Down
16 changes: 4 additions & 12 deletions ChatAAC/Services/OllamaClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using ChatAAC.ViewModels;
using OllamaSharp;

namespace ChatAAC.Services;
Expand All @@ -12,19 +13,10 @@ public class OllamaClient

public OllamaClient(string apiUrl)
{
if (string.IsNullOrWhiteSpace(apiUrl))
apiUrl = "http://localhost:11434";

if (!apiUrl.StartsWith("http"))
apiUrl = "http://" + apiUrl;

if (apiUrl.IndexOf(':', 5) < 0)
apiUrl += ":11434";


Console.WriteLine($"Connecting to {apiUrl} ...");

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

var ollama = new OllamaApiClient(apiUrl)
var ollama = new OllamaApiClient(ConfigViewModel.Instance.OllamaAddress)
{
SelectedModel = "gemma2"
};
Expand Down
34 changes: 13 additions & 21 deletions ChatAAC/Services/PictogramService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public PictogramService()
// Pobierz dane z API ARASAAC
try
{
var url = "https://api.arasaac.org/v1/pictograms/all/pl";
const string url = "https://api.arasaac.org/v1/pictograms/all/pl";
var response = await HttpClient.GetAsync(url).ConfigureAwait(false);

if (response.IsSuccessStatusCode)
Expand All @@ -83,21 +83,14 @@ public PictogramService()
var pictograms = JsonSerializer.Deserialize<List<Pictogram>>(responseData);

// Sprawdź, czy deserializacja zwróciła dane
if (pictograms is { Count: > 0 })
{
await CheckAndDownloadMissingImages(pictograms);
return pictograms;
}
else
{
throw new Exception("Pobrano puste dane z API ARASAAC.");
}
}
else
{
throw new Exception(
$"Niepowodzenie pobierania danych z ARASAAC API. Status Code: {response.StatusCode}");
if (pictograms is not { Count: > 0 }) throw new Exception("Pobrano puste dane z API ARASAAC.");
await CheckAndDownloadMissingImages(pictograms);

return pictograms;
}

throw new Exception(
$"Niepowodzenie pobierania danych z ARASAAC API. Status Code: {response.StatusCode}");
}
catch (Exception ex)
{
Expand All @@ -109,13 +102,12 @@ public PictogramService()

private async Task CheckAndDownloadMissingImages(List<Pictogram> pictograms)
{
foreach (var pictogram in pictograms)
foreach (var pictogram in from pictogram in pictograms
let imagePath = Path.Combine(_cacheDirectory, $"{pictogram.Id}.png")
where !File.Exists(imagePath)
select pictogram)
{
var imagePath = Path.Combine(_cacheDirectory, $"{pictogram.Id}.png");
if (!File.Exists(imagePath))
{
await DownloadPictogramImageAsync(pictogram.Id.ToString());
}
await DownloadPictogramImageAsync(pictogram.Id.ToString());
}
}

Expand Down
4 changes: 2 additions & 2 deletions ChatAAC/Services/TtsService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public async Task SpeakAsync(string text)
if (string.IsNullOrWhiteSpace(text))
throw new ArgumentException("Tekst do odczytania nie może być pusty.", nameof(text));

if (!IsMacOS())
if (!IsMacOs())
throw new PlatformNotSupportedException("TTS za pomocą 'say' jest wspierane tylko na macOS.");

// Przygotowanie procesu
Expand Down Expand Up @@ -62,7 +62,7 @@ public async Task SpeakAsync(string text)
/// Sprawdza, czy aplikacja działa na macOS.
/// </summary>
/// <returns>Prawda, jeśli system operacyjny to macOS; w przeciwnym razie fałsz.</returns>
private bool IsMacOS()
private bool IsMacOs()
{
return RuntimeInformation.IsOSPlatform(OSPlatform.OSX);
}
Expand Down
Loading

0 comments on commit 4e1e63d

Please sign in to comment.