From 461f00765292229aced016ac572e12217af33801 Mon Sep 17 00:00:00 2001 From: Miguel Date: Wed, 24 Apr 2024 08:33:57 +0200 Subject: [PATCH] Add project files. --- App.xaml | 8 +++ App.xaml.cs | 59 +++++++++++++++++ AssemblyInfo.cs | 10 +++ GTPCorrgir.csproj | 19 ++++++ GTPCorrgir.sln | 25 ++++++++ MainWindow.xaml | 12 ++++ MainWindow.xaml.cs | 24 +++++++ gtpask.cs | 157 +++++++++++++++++++++++++++++++++++++++++++++ 8 files changed, 314 insertions(+) create mode 100644 App.xaml create mode 100644 App.xaml.cs create mode 100644 AssemblyInfo.cs create mode 100644 GTPCorrgir.csproj create mode 100644 GTPCorrgir.sln create mode 100644 MainWindow.xaml create mode 100644 MainWindow.xaml.cs create mode 100644 gtpask.cs diff --git a/App.xaml b/App.xaml new file mode 100644 index 0000000..dd3d3a5 --- /dev/null +++ b/App.xaml @@ -0,0 +1,8 @@ + + + + + diff --git a/App.xaml.cs b/App.xaml.cs new file mode 100644 index 0000000..b5edf53 --- /dev/null +++ b/App.xaml.cs @@ -0,0 +1,59 @@ +using System.Configuration; +using System.Data; +using System.Windows; +using System; +using System.Windows; +using System.Windows.Forms; // Necesitas agregar una referencia a System.Windows.Forms +using Application = System.Windows.Application; + +namespace GTPCorrgir +{ + /// + /// Interaction logic for App.xaml + /// + public partial class App : Application + { + gtpask GTP = new gtpask(); + protected override void OnStartup(StartupEventArgs e) + { + base.OnStartup(e); + + if (System.Windows.Clipboard.ContainsText()) + { + GTP.TextoACorregir = System.Windows.Clipboard.GetText(); + + + } + + // Inicia y espera la tarea asíncrona + Task.Run(async () => + { + await GTP.CorregirTexto(); + + // Asegúrate de cerrar la aplicación en el hilo de la UI + Dispatcher.Invoke(() => + { + System.Windows.Clipboard.SetText(GTP.TextoCorregido); + MostrarNotificacion("Correccion Lista", GTP.TextoCorregido); + Application.Current.Shutdown(); + }); + }); + + + } + + + private void MostrarNotificacion(string titulo, string mensaje) + { + NotifyIcon notificacion = new NotifyIcon + { + Icon = SystemIcons.Information, + BalloonTipTitle = titulo, + BalloonTipText = mensaje, + Visible = true + }; + notificacion.ShowBalloonTip(1000); // Muestra la notificación por 3000 milisegundos + } + } + +} diff --git a/AssemblyInfo.cs b/AssemblyInfo.cs new file mode 100644 index 0000000..b0ec827 --- /dev/null +++ b/AssemblyInfo.cs @@ -0,0 +1,10 @@ +using System.Windows; + +[assembly: ThemeInfo( + ResourceDictionaryLocation.None, //where theme specific resource dictionaries are located + //(used if a resource is not found in the page, + // or application resource dictionaries) + ResourceDictionaryLocation.SourceAssembly //where the generic resource dictionary is located + //(used if a resource is not found in the page, + // app, or any theme specific resource dictionaries) +)] diff --git a/GTPCorrgir.csproj b/GTPCorrgir.csproj new file mode 100644 index 0000000..e668e1b --- /dev/null +++ b/GTPCorrgir.csproj @@ -0,0 +1,19 @@ + + + + WinExe + net8.0-windows + enable + enable + true + true + + + + + + + + + + diff --git a/GTPCorrgir.sln b/GTPCorrgir.sln new file mode 100644 index 0000000..4ea5de4 --- /dev/null +++ b/GTPCorrgir.sln @@ -0,0 +1,25 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 17 +VisualStudioVersion = 17.9.34723.18 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "GTPCorrgir", "GTPCorrgir.csproj", "{64B560BC-C93A-4407-923E-19C8C292E2BC}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {64B560BC-C93A-4407-923E-19C8C292E2BC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {64B560BC-C93A-4407-923E-19C8C292E2BC}.Debug|Any CPU.Build.0 = Debug|Any CPU + {64B560BC-C93A-4407-923E-19C8C292E2BC}.Release|Any CPU.ActiveCfg = Release|Any CPU + {64B560BC-C93A-4407-923E-19C8C292E2BC}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {CC80356B-9D92-48AD-A4AF-E285B667368A} + EndGlobalSection +EndGlobal diff --git a/MainWindow.xaml b/MainWindow.xaml new file mode 100644 index 0000000..3cca046 --- /dev/null +++ b/MainWindow.xaml @@ -0,0 +1,12 @@ + + + + + diff --git a/MainWindow.xaml.cs b/MainWindow.xaml.cs new file mode 100644 index 0000000..c590376 --- /dev/null +++ b/MainWindow.xaml.cs @@ -0,0 +1,24 @@ +using System.Text; +using System.Windows; +using System.Windows.Controls; +using System.Windows.Data; +using System.Windows.Documents; +using System.Windows.Input; +using System.Windows.Media; +using System.Windows.Media.Imaging; +using System.Windows.Navigation; +using System.Windows.Shapes; + +namespace GTPCorrgir +{ + /// + /// Interaction logic for MainWindow.xaml + /// + public partial class MainWindow : Window + { + public MainWindow() + { + InitializeComponent(); + } + } +} \ No newline at end of file diff --git a/gtpask.cs b/gtpask.cs new file mode 100644 index 0000000..198da23 --- /dev/null +++ b/gtpask.cs @@ -0,0 +1,157 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Net.Http; +using System.Text; +using System.Threading.Tasks; +using System.Windows.Threading; +using LanguageDetection; +using Newtonsoft.Json; + +namespace GTPCorrgir +{ + internal class gtpask + { + private readonly string openAiApiKey = "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"; + private readonly string accionGTP = "Me puedes corregir el siguiente texto y mejorarlo para que se comprenda mejor: "; + private Dictionary languageMap = new Dictionary + { + { "en", "Inglés" }, + { "es", "Español" }, + { "it", "Italiano" }, + { "pt", "Portugués" } + // Agrega más idiomas según sea necesario + }; + public string IdiomaDetectado; + public string TextoACorregir; + public string TextoCorregido; + + private string DetectarIdioma(string TextoACorregir) + { + LanguageDetector detector = new LanguageDetector(); + detector.AddLanguages("en", "es", "it", "pt"); + string detectedLanguageCode = detector.Detect(TextoACorregir); + + string detectedLanguageName = languageMap.ContainsKey(detectedLanguageCode) + ? languageMap[detectedLanguageCode] + : "Desconocido"; + + return detectedLanguageName; + } + private bool DetectarIdioma() + { + if (TextoACorregir.Length>0) + { + IdiomaDetectado = DetectarIdioma(TextoACorregir); + if(IdiomaDetectado != "Desconocido") + return true; + else return false; + } + return false; + } + + public async Task CorregirTexto() + { + var Instrucciones = accionGTP; + + if (DetectarIdioma()) { + if (IdiomaDetectado == "Desconocido" || this.TextoACorregir.Length == 0) + { + // Nada que hacer + TextoCorregido = TextoACorregir; + return; + } + + TextoACorregir = "El resultado debe estar en " + IdiomaDetectado + ". " + Instrucciones + "\"" + TextoACorregir + "\""; + + // TextoCorregido = await CallOpenAiApi(TextoACorregir); + TextoCorregido = await CallOllamaApi(TextoACorregir); + + // Elimina comillas al principio y al final si existen + TextoCorregido = TextoCorregido.Trim('\"'); + } + } + private async Task CallOllamaApi(string input) + { + var httpClient = new HttpClient(); + httpClient.DefaultRequestHeaders.Add("Authorization", $"Bearer {openAiApiKey}"); + + var requestData = new + { + model = "llama3", + messages = new[] + { + new { role = "user", content = input }, + }, + stream = false + }; + + var content = new StringContent(JsonConvert.SerializeObject(requestData), Encoding.UTF8, "application/json"); + try + { + var response = await httpClient.PostAsync("http://127.0.0.1:11434/api/chat", content); + response.EnsureSuccessStatusCode(); + var jsonResponse = await response.Content.ReadAsStringAsync(); + dynamic data = JsonConvert.DeserializeObject(jsonResponse); + + if (data.done == true && data.message != null) + { + return data.message.content; + } + return "No hubo respuesta del asistente o la sesión aún no ha concluido."; + } + catch (HttpRequestException e) + { + // Captura errores en la solicitud HTTP, como problemas de red o respuestas de error HTTP. + Console.WriteLine($"Error making HTTP request: {e.Message}"); + return null; + } + catch (Exception e) + { + // Captura cualquier otro error + Console.WriteLine($"An error occurred: {e.Message}"); + return null; + } + + } + private async Task CallOpenAiApi(string input) + { + var httpClient = new HttpClient(); + httpClient.DefaultRequestHeaders.Add("Authorization", $"Bearer {openAiApiKey}"); + + var requestData = new + { + model = "gpt-4", + messages = new[] + { + new { role = "system", content = "Este es un mensaje del sistema inicializando la conversación" }, + new { role = "user", content = input } + } + }; + + var content = new StringContent(JsonConvert.SerializeObject(requestData), Encoding.UTF8, "application/json"); + try + { + var response = await httpClient.PostAsync("https://api.openai.com/v1/chat/completions", content); + response.EnsureSuccessStatusCode(); + var jsonResponse = await response.Content.ReadAsStringAsync(); + dynamic data = JsonConvert.DeserializeObject(jsonResponse); + return data.choices[0].message.content; + } + catch (HttpRequestException e) + { + // Captura errores en la solicitud HTTP, como problemas de red o respuestas de error HTTP. + Console.WriteLine($"Error making HTTP request: {e.Message}"); + return null; + } + catch (Exception e) + { + // Captura cualquier otro error + Console.WriteLine($"An error occurred: {e.Message}"); + return null; + } + + } + + } +}