-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMainWindow.xaml.cs
More file actions
105 lines (87 loc) · 3.97 KB
/
MainWindow.xaml.cs
File metadata and controls
105 lines (87 loc) · 3.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
using System.Diagnostics;
using System.Media;
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;
using static System.Net.Mime.MediaTypeNames;
using Microsoft.ML.OnnxRuntimeGenAI;
using System.Windows.Automation.Peers;
namespace AdMirai
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
this.Title = "AdMirai";
}
private void Seleccionar_Click(object sender, RoutedEventArgs e)
{
//Console.Write($"Seleccionar_Click()");
Trace.WriteLine("Seleccionar_Click()");
// Configure open file dialog box
var dialog = new Microsoft.Win32.OpenFileDialog();
dialog.FileName = "Document"; // Default file name
dialog.DefaultExt = ".jpg"; // Default file extension
dialog.Filter = "Imágenes (.jpg)|*.jpg"; // Filter files by extension
// Show open file dialog box
bool? result = dialog.ShowDialog();
// Process open file dialog box results
if (result == true)
{
textBoxResult.Text = "";
Task.Run(() =>
{
// Open document
string filename = dialog.FileName;
//Console.Write($"filename {filename}");
Trace.WriteLine($"filename {filename}");
// path for model and images
var modelPath = @"C:\AdMirai\phi3_onnx";
var img = Images.Load(filename);
// define prompts
var systemPrompt = "You are an AI assistant that helps people find information. Answer questions using a direct style. Do not share more information that the requested by the users.";
string userPrompt = "Describe la imagen en idioma español";
var fullPrompt = $"<|system|>{systemPrompt}<|end|><|user|><|image_1|>{userPrompt}<|end|><|assistant|>";
// load model and create processor
using Model model = new Model(modelPath);
using MultiModalProcessor processor = new MultiModalProcessor(model);
using var tokenizerStream = processor.CreateStream();
// create the input tensor with the prompt and image
Console.WriteLine("Reading the image...");
var inputTensors = processor.ProcessImages(fullPrompt, img);
using GeneratorParams generatorParams = new GeneratorParams(model);
generatorParams.SetSearchOption("max_length", 3072);
generatorParams.SetInputs(inputTensors);
// generate response
using var generator = new Generator(model, generatorParams);
while (!generator.IsDone())
{
generator.ComputeLogits();
generator.GenerateNextToken();
var seq = generator.GetSequence(0)[^1];
var decodedResult = tokenizerStream.Decode(seq);
Trace.Write(decodedResult);
Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Render,
new Action(() =>
{
textBoxResult.Text = textBoxResult.Text + decodedResult;
}
));
} // while
SystemSounds.Beep.Play();
});
} //if
} // private void
} // public
} // namespace