From 67a93df89048aadde5c96db1d923fdb893ec74b2 Mon Sep 17 00:00:00 2001 From: Sandeep Sharma Date: Tue, 10 Dec 2024 19:29:43 +0530 Subject: [PATCH] Add project files. --- Controls/ChatControl.Designer.cs | 85 ++++++++++++++++ Controls/ChatControl.cs | 73 ++++++++++++++ Controls/ChatControl.resx | 120 +++++++++++++++++++++++ Controls/CodeControl.Designer.cs | 57 +++++++++++ Controls/CodeControl.cs | 9 ++ Controls/CodeControl.resx | 120 +++++++++++++++++++++++ Controls/ImageControl.Designer.cs | 57 +++++++++++ Controls/ImageControl.cs | 9 ++ Controls/ImageControl.resx | 120 +++++++++++++++++++++++ CraftAiTools.csproj | 19 ++++ CraftAiTools.sln | 25 +++++ Globals.cs | 15 +++ MainForm.Designer.cs | 155 ++++++++++++++++++++++++++++++ MainForm.cs | 92 ++++++++++++++++++ MainForm.resx | 126 ++++++++++++++++++++++++ Models/LLamaModel.cs | 8 ++ Program.cs | 19 ++++ 17 files changed, 1109 insertions(+) create mode 100644 Controls/ChatControl.Designer.cs create mode 100644 Controls/ChatControl.cs create mode 100644 Controls/ChatControl.resx create mode 100644 Controls/CodeControl.Designer.cs create mode 100644 Controls/CodeControl.cs create mode 100644 Controls/CodeControl.resx create mode 100644 Controls/ImageControl.Designer.cs create mode 100644 Controls/ImageControl.cs create mode 100644 Controls/ImageControl.resx create mode 100644 CraftAiTools.csproj create mode 100644 CraftAiTools.sln create mode 100644 Globals.cs create mode 100644 MainForm.Designer.cs create mode 100644 MainForm.cs create mode 100644 MainForm.resx create mode 100644 Models/LLamaModel.cs create mode 100644 Program.cs diff --git a/Controls/ChatControl.Designer.cs b/Controls/ChatControl.Designer.cs new file mode 100644 index 0000000..4ec7385 --- /dev/null +++ b/Controls/ChatControl.Designer.cs @@ -0,0 +1,85 @@ +namespace CraftAiTools.Controls; + +partial class ChatControl +{ + /// + /// Required designer variable. + /// + private System.ComponentModel.IContainer components = null; + + /// + /// Clean up any resources being used. + /// + /// true if managed resources should be disposed; otherwise, false. + protected override void Dispose(bool disposing) + { + if (disposing && (components != null)) + { + components.Dispose(); + } + base.Dispose(disposing); + } + + #region Component Designer generated code + + /// + /// Required method for Designer support - do not modify + /// the contents of this method with the code editor. + /// + private void InitializeComponent() + { + txtLog = new TextBox(); + txtUserInput = new TextBox(); + btnSend = new Button(); + SuspendLayout(); + // + // txtLog + // + txtLog.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right; + txtLog.Font = new Font("Roboto", 9.75F, FontStyle.Regular, GraphicsUnit.Point, 0); + txtLog.Location = new Point(18, 18); + txtLog.Multiline = true; + txtLog.Name = "txtLog"; + txtLog.ReadOnly = true; + txtLog.Size = new Size(1017, 487); + txtLog.TabIndex = 0; + // + // txtUserInput + // + txtUserInput.Font = new Font("Roboto", 9.75F, FontStyle.Regular, GraphicsUnit.Point, 0); + txtUserInput.Location = new Point(18, 521); + txtUserInput.Multiline = true; + txtUserInput.Name = "txtUserInput"; + txtUserInput.Size = new Size(893, 78); + txtUserInput.TabIndex = 1; + // + // btnSend + // + btnSend.Font = new Font("Roboto", 12F, FontStyle.Regular, GraphicsUnit.Point, 0); + btnSend.Location = new Point(917, 521); + btnSend.Name = "btnSend"; + btnSend.Size = new Size(118, 78); + btnSend.TabIndex = 2; + btnSend.Text = "Send"; + btnSend.UseVisualStyleBackColor = true; + btnSend.Click += btnSend_Click; + // + // ChatControl + // + AutoScaleDimensions = new SizeF(7F, 15F); + AutoScaleMode = AutoScaleMode.Font; + Controls.Add(btnSend); + Controls.Add(txtUserInput); + Controls.Add(txtLog); + Name = "ChatControl"; + Size = new Size(1050, 617); + ResumeLayout(false); + PerformLayout(); + } + + #endregion + + private TextBox txtLog; + private TextBox txtUserInput; + private Button btnSend; +} diff --git a/Controls/ChatControl.cs b/Controls/ChatControl.cs new file mode 100644 index 0000000..c4140fb --- /dev/null +++ b/Controls/ChatControl.cs @@ -0,0 +1,73 @@ +using LLama; +using LLama.Common; + +namespace CraftAiTools.Controls; + +public partial class ChatControl : UserControl +{ + private InteractiveExecutor? executor; + private ChatHistory? chatHistory; + private ChatSession? session; + private InferenceParams? inferenceParams; + private LLamaWeights? model; + private LLamaContext? context; + + public ChatControl() + { + InitializeComponent(); + + btnSend.Enabled = false; + + if (Globals.SelectModelPath is null) + { + MessageBox.Show("Please select a model first."); + return; + } + + var parameters = new ModelParams(Globals.SelectModelPath) + { + ContextSize = 1024, // The longest length of chat as memory. + GpuLayerCount = 5 // How many layers to offload to GPU. Please adjust it according to your GPU memory. + }; + + model = LLamaWeights.LoadFromFile(parameters); + context = model.CreateContext(parameters); + + btnSend.Enabled = false; + + executor = new InteractiveExecutor(context); + + chatHistory = new ChatHistory(); + chatHistory.AddMessage(AuthorRole.System, "Transcript of a dialog, where the User interacts with an Assistant named Bob. Bob is helpful, kind, honest, good at writing, and never fails to answer the User's requests immediately and with precision."); + chatHistory.AddMessage(AuthorRole.User, "Hello, Bob."); + chatHistory.AddMessage(AuthorRole.Assistant, "Hello. How may I help you today?"); + + if (executor is not null) + session = new(executor, chatHistory); + + inferenceParams = new InferenceParams() + { + MaxTokens = 1024, + AntiPrompts = ["User:"] // Stop generation once antiprompts appear. + }; + + txtLog.Text += "The chat session has started...\nUser: "; + + btnSend.Enabled = session is not null; + } + + private async void btnSend_Click(object sender, EventArgs e) + { + txtLog.Text += txtUserInput.Text + "\n"; + + txtLog.Text += "Bob: "; + + await foreach (var text in session!.ChatAsync(new ChatHistory.Message(AuthorRole.User, + txtUserInput.Text), inferenceParams)) + { + txtLog.Text += text; + } + + txtLog.Text += "\nUser: "; + } +} diff --git a/Controls/ChatControl.resx b/Controls/ChatControl.resx new file mode 100644 index 0000000..8b2ff64 --- /dev/null +++ b/Controls/ChatControl.resx @@ -0,0 +1,120 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + \ No newline at end of file diff --git a/Controls/CodeControl.Designer.cs b/Controls/CodeControl.Designer.cs new file mode 100644 index 0000000..615223c --- /dev/null +++ b/Controls/CodeControl.Designer.cs @@ -0,0 +1,57 @@ +namespace CraftAiTools.Controls; + +partial class CodeControl +{ + /// + /// Required designer variable. + /// + private System.ComponentModel.IContainer components = null; + + /// + /// Clean up any resources being used. + /// + /// true if managed resources should be disposed; otherwise, false. + protected override void Dispose(bool disposing) + { + if (disposing && (components != null)) + { + components.Dispose(); + } + base.Dispose(disposing); + } + + #region Component Designer generated code + + /// + /// Required method for Designer support - do not modify + /// the contents of this method with the code editor. + /// + private void InitializeComponent() + { + label1 = new Label(); + SuspendLayout(); + // + // label1 + // + label1.AutoSize = true; + label1.Location = new Point(86, 53); + label1.Name = "label1"; + label1.Size = new Size(35, 15); + label1.TabIndex = 0; + label1.Text = "Code"; + // + // CodeControl + // + AutoScaleDimensions = new SizeF(7F, 15F); + AutoScaleMode = AutoScaleMode.Font; + Controls.Add(label1); + Name = "CodeControl"; + Size = new Size(683, 543); + ResumeLayout(false); + PerformLayout(); + } + + #endregion + + private Label label1; +} diff --git a/Controls/CodeControl.cs b/Controls/CodeControl.cs new file mode 100644 index 0000000..c96b8a2 --- /dev/null +++ b/Controls/CodeControl.cs @@ -0,0 +1,9 @@ +namespace CraftAiTools.Controls; + +public partial class CodeControl : UserControl +{ + public CodeControl() + { + InitializeComponent(); + } +} diff --git a/Controls/CodeControl.resx b/Controls/CodeControl.resx new file mode 100644 index 0000000..8b2ff64 --- /dev/null +++ b/Controls/CodeControl.resx @@ -0,0 +1,120 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + \ No newline at end of file diff --git a/Controls/ImageControl.Designer.cs b/Controls/ImageControl.Designer.cs new file mode 100644 index 0000000..0cd8171 --- /dev/null +++ b/Controls/ImageControl.Designer.cs @@ -0,0 +1,57 @@ +namespace CraftAiTools.Controls; + +partial class ImageControl +{ + /// + /// Required designer variable. + /// + private System.ComponentModel.IContainer components = null; + + /// + /// Clean up any resources being used. + /// + /// true if managed resources should be disposed; otherwise, false. + protected override void Dispose(bool disposing) + { + if (disposing && (components != null)) + { + components.Dispose(); + } + base.Dispose(disposing); + } + + #region Component Designer generated code + + /// + /// Required method for Designer support - do not modify + /// the contents of this method with the code editor. + /// + private void InitializeComponent() + { + label1 = new Label(); + SuspendLayout(); + // + // label1 + // + label1.AutoSize = true; + label1.Location = new Point(112, 59); + label1.Name = "label1"; + label1.Size = new Size(40, 15); + label1.TabIndex = 0; + label1.Text = "Image"; + // + // ImageControl + // + AutoScaleDimensions = new SizeF(7F, 15F); + AutoScaleMode = AutoScaleMode.Font; + Controls.Add(label1); + Name = "ImageControl"; + Size = new Size(766, 531); + ResumeLayout(false); + PerformLayout(); + } + + #endregion + + private Label label1; +} diff --git a/Controls/ImageControl.cs b/Controls/ImageControl.cs new file mode 100644 index 0000000..1ebf0ac --- /dev/null +++ b/Controls/ImageControl.cs @@ -0,0 +1,9 @@ +namespace CraftAiTools.Controls; + +public partial class ImageControl : UserControl +{ + public ImageControl() + { + InitializeComponent(); + } +} diff --git a/Controls/ImageControl.resx b/Controls/ImageControl.resx new file mode 100644 index 0000000..8b2ff64 --- /dev/null +++ b/Controls/ImageControl.resx @@ -0,0 +1,120 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + \ No newline at end of file diff --git a/CraftAiTools.csproj b/CraftAiTools.csproj new file mode 100644 index 0000000..e10cc49 --- /dev/null +++ b/CraftAiTools.csproj @@ -0,0 +1,19 @@ + + + + WinExe + net9.0-windows + enable + true + enable + + + + + + + + + + + \ No newline at end of file diff --git a/CraftAiTools.sln b/CraftAiTools.sln new file mode 100644 index 0000000..cf21997 --- /dev/null +++ b/CraftAiTools.sln @@ -0,0 +1,25 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 17 +VisualStudioVersion = 17.13.35507.96 d17.13 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CraftAiTools", "CraftAiTools.csproj", "{53A65C64-25E3-481F-9D35-B0404953740A}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {53A65C64-25E3-481F-9D35-B0404953740A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {53A65C64-25E3-481F-9D35-B0404953740A}.Debug|Any CPU.Build.0 = Debug|Any CPU + {53A65C64-25E3-481F-9D35-B0404953740A}.Release|Any CPU.ActiveCfg = Release|Any CPU + {53A65C64-25E3-481F-9D35-B0404953740A}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {54D00CF7-2C6D-4A29-9712-3E7B82B10198} + EndGlobalSection +EndGlobal diff --git a/Globals.cs b/Globals.cs new file mode 100644 index 0000000..2bf8d2f --- /dev/null +++ b/Globals.cs @@ -0,0 +1,15 @@ +using CraftAiTools.Models; + +using LLama; + +namespace CraftAiTools; + +public static class Globals +{ + public const string ModelsPath = @"H:\AI_Models"; + + public static List Models { get; set; } = []; + + public static string? SelectModelName { get; set; } + public static string? SelectModelPath { get; set; } +} diff --git a/MainForm.Designer.cs b/MainForm.Designer.cs new file mode 100644 index 0000000..a28cf8c --- /dev/null +++ b/MainForm.Designer.cs @@ -0,0 +1,155 @@ +namespace CraftAiTools; + +partial class MainForm +{ + /// + /// Required designer variable. + /// + private System.ComponentModel.IContainer components = null; + + /// + /// Clean up any resources being used. + /// + /// true if managed resources should be disposed; otherwise, false. + protected override void Dispose(bool disposing) + { + if (disposing && (components != null)) + { + components.Dispose(); + } + base.Dispose(disposing); + } + + #region Windows Form Designer generated code + + /// + /// Required method for Designer support - do not modify + /// the contents of this method with the code editor. + /// + private void InitializeComponent() + { + TS_Main = new ToolStrip(); + TSB_Chat = new ToolStripButton(); + TSB_Image = new ToolStripButton(); + TSB_Code = new ToolStripButton(); + toolStripSeparator1 = new ToolStripSeparator(); + CB_Models = new ToolStripComboBox(); + LBL_Models = new ToolStripLabel(); + SS_Main = new StatusStrip(); + Panel_Main = new Panel(); + TS_Main.SuspendLayout(); + SuspendLayout(); + // + // TS_Main + // + TS_Main.Items.AddRange(new ToolStripItem[] { TSB_Chat, TSB_Image, TSB_Code, toolStripSeparator1, CB_Models, LBL_Models }); + TS_Main.Location = new Point(0, 0); + TS_Main.Name = "TS_Main"; + TS_Main.Size = new Size(1050, 47); + TS_Main.TabIndex = 0; + // + // TSB_Chat + // + TSB_Chat.DisplayStyle = ToolStripItemDisplayStyle.Text; + TSB_Chat.Font = new Font("Roboto", 10F); + TSB_Chat.ImageTransparentColor = Color.Magenta; + TSB_Chat.Margin = new Padding(5); + TSB_Chat.Name = "TSB_Chat"; + TSB_Chat.Padding = new Padding(7); + TSB_Chat.Size = new Size(110, 37); + TSB_Chat.Text = "Generic Chat"; + TSB_Chat.Click += TSB_Chat_Click; + // + // TSB_Image + // + TSB_Image.DisplayStyle = ToolStripItemDisplayStyle.Text; + TSB_Image.Font = new Font("Roboto", 10F); + TSB_Image.ImageTransparentColor = Color.Magenta; + TSB_Image.Margin = new Padding(5); + TSB_Image.Name = "TSB_Image"; + TSB_Image.Padding = new Padding(7); + TSB_Image.Size = new Size(130, 37); + TSB_Image.Text = "Generate Image"; + TSB_Image.Click += TSB_Image_Click; + // + // TSB_Code + // + TSB_Code.DisplayStyle = ToolStripItemDisplayStyle.Text; + TSB_Code.Font = new Font("Roboto", 10F); + TSB_Code.ImageTransparentColor = Color.Magenta; + TSB_Code.Margin = new Padding(5); + TSB_Code.Name = "TSB_Code"; + TSB_Code.Padding = new Padding(7); + TSB_Code.Size = new Size(113, 37); + TSB_Code.Text = "Generic Code"; + TSB_Code.Click += TSB_Code_Click; + // + // toolStripSeparator1 + // + toolStripSeparator1.Name = "toolStripSeparator1"; + toolStripSeparator1.Size = new Size(6, 47); + // + // CB_Models + // + CB_Models.Alignment = ToolStripItemAlignment.Right; + CB_Models.Font = new Font("Roboto", 9.75F, FontStyle.Regular, GraphicsUnit.Point, 0); + CB_Models.Name = "CB_Models"; + CB_Models.Size = new Size(225, 47); + CB_Models.Sorted = true; + CB_Models.ToolTipText = "Select Model..."; + CB_Models.SelectedIndexChanged += ModelChanged; + // + // LBL_Models + // + LBL_Models.Alignment = ToolStripItemAlignment.Right; + LBL_Models.Name = "LBL_Models"; + LBL_Models.Size = new Size(47, 44); + LBL_Models.Text = "Model: "; + // + // SS_Main + // + SS_Main.Location = new Point(0, 670); + SS_Main.Name = "SS_Main"; + SS_Main.Size = new Size(1050, 22); + SS_Main.TabIndex = 1; + // + // Panel_Main + // + Panel_Main.Anchor = AnchorStyles.Top | AnchorStyles.Bottom | AnchorStyles.Left | AnchorStyles.Right; + Panel_Main.BorderStyle = BorderStyle.Fixed3D; + Panel_Main.Location = new Point(0, 50); + Panel_Main.Margin = new Padding(0); + Panel_Main.Name = "Panel_Main"; + Panel_Main.Size = new Size(1050, 617); + Panel_Main.TabIndex = 2; + // + // MainForm + // + AutoScaleDimensions = new SizeF(7F, 15F); + AutoScaleMode = AutoScaleMode.Font; + ClientSize = new Size(1050, 692); + Controls.Add(Panel_Main); + Controls.Add(SS_Main); + Controls.Add(TS_Main); + Margin = new Padding(4, 3, 4, 3); + Name = "MainForm"; + Text = "Craft AI Tools"; + Load += MainForm_Load; + TS_Main.ResumeLayout(false); + TS_Main.PerformLayout(); + ResumeLayout(false); + PerformLayout(); + } + + #endregion + + private ToolStrip TS_Main; + private ToolStripButton TSB_Chat; + private ToolStripSeparator toolStripSeparator1; + private ToolStripComboBox CB_Models; + private ToolStripButton TSB_Image; + private ToolStripButton TSB_Code; + private ToolStripLabel LBL_Models; + private StatusStrip SS_Main; + private Panel Panel_Main; +} \ No newline at end of file diff --git a/MainForm.cs b/MainForm.cs new file mode 100644 index 0000000..34c500d --- /dev/null +++ b/MainForm.cs @@ -0,0 +1,92 @@ +using CraftAiTools.Controls; +using CraftAiTools.Models; + +using LLama; +using LLama.Common; + +namespace CraftAiTools; +public partial class MainForm : Form +{ + public MainForm() + { + InitializeComponent(); + + ScanModels(); + LoadModels(); + } + + private void LoadModels() + { + CB_Models.Items.Clear(); + + foreach (LLamaModel model in Globals.Models) + CB_Models.Items.Add(model.Name ?? ""); + } + + private static void ScanModels() + { + if (Directory.Exists(Globals.ModelsPath)) + { + // Scan The Models + string[] files = Directory.GetFiles(Globals.ModelsPath, "*.gguf", SearchOption.AllDirectories); + + // Clear The Pevious Models + Globals.Models.Clear(); + + foreach (string file in files) + { + Globals.Models.Add(new LLamaModel + { + Name = Path.GetFileNameWithoutExtension(file), + Path = file + }); + } + } + else + { + MessageBox.Show("Directory not found: " + Globals.ModelsPath); + } + } + + private void MainForm_Load(object sender, EventArgs e) + { + LoadUserControl(new ChatControl()); + } + + private void LoadUserControl(UserControl userControl) + { + Panel_Main.Controls.Clear(); + userControl.Dock = DockStyle.Fill; + Panel_Main.Controls.Add(userControl); + } + + private void TSB_Chat_Click(object sender, EventArgs e) + { + LoadUserControl(new ChatControl()); + } + + private void TSB_Image_Click(object sender, EventArgs e) + { + LoadUserControl(new ImageControl()); + } + + private void TSB_Code_Click(object sender, EventArgs e) + { + LoadUserControl(new CodeControl()); + } + + private void ModelChanged(object sender, EventArgs e) + { + // Get The Item + var selectedItem = CB_Models.SelectedItem?.ToString(); + + if (selectedItem is null) return; + + var model = Globals.Models?.FirstOrDefault(x => x.Name == selectedItem); + + if (model == null) return; + + Globals.SelectModelName = model.Name; + Globals.SelectModelPath = model.Path; + } +} diff --git a/MainForm.resx b/MainForm.resx new file mode 100644 index 0000000..d4014e9 --- /dev/null +++ b/MainForm.resx @@ -0,0 +1,126 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + 17, 17 + + + 122, 17 + + \ No newline at end of file diff --git a/Models/LLamaModel.cs b/Models/LLamaModel.cs new file mode 100644 index 0000000..1ca9c7d --- /dev/null +++ b/Models/LLamaModel.cs @@ -0,0 +1,8 @@ +namespace CraftAiTools.Models; + +public class LLamaModel +{ + public string? Name { get; set; } + + public string? Path { get; set; } +} diff --git a/Program.cs b/Program.cs new file mode 100644 index 0000000..ea70540 --- /dev/null +++ b/Program.cs @@ -0,0 +1,19 @@ +namespace CraftAiTools +{ + internal static class Program + { + /// + /// The main entry point for the application. + /// + [STAThread] + static void Main() + { + // To customize application configuration such as set high DPI settings or default font, + // see https://aka.ms/applicationconfiguration. + + ApplicationConfiguration.Initialize(); + + Application.Run(new MainForm()); + } + } +} \ No newline at end of file