absolutely horrendous

This commit is contained in:
femsci 2023-11-18 05:59:32 +01:00
parent 60c896acd1
commit 24ba0f6314
Signed by: femsci
GPG key ID: 08F7911F0E650C67
7 changed files with 92 additions and 24 deletions

View file

@ -25,6 +25,6 @@ public record Person(string Name, string Surname, string? Id = null, string? Pes
public string SerializeToDocument()
{
return $"{Name} {Surname}{(Pesel != null ? $" PESEL {Pesel}" : "")}{(IDCode != null ? $" legitymującą się dowodem {IDCode}" : "")}{(DoB != null ? $" ur. {DoB.Value:dd MMM yyyy}" : "")}";
return $"{Name} {Surname}{(Pesel != null ? $" PESEL {Pesel}" : "")}{(IDCode != null ? $" legitymując{(Pesel == null ? 'e' : (((int.Parse(Pesel[^2].ToString()) & 0x1) == 0x1) ? 'y' : 'a'))} się dowodem {IDCode}" : "")}";
}
}

View file

@ -0,0 +1,21 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.Json.Serialization;
using System.Threading.Tasks;
namespace Nyanlabs.Umogen.Core.Models;
public class UmoAnalModel
{
[JsonPropertyName("c_type")]
public string ContractType { get; set; }
[JsonPropertyName("j_pos")]
public string JobPosition { get; set; }
[JsonPropertyName("k_lit")]
public string ValueLiteral { get; set; }
[JsonPropertyName("conds")]
public ICollection<string> Conditions { get; set; }
public UmoDoctype Doctype => UmoDoctypeBindings.GetFromName(ContractType).Value;
}

View file

@ -3,13 +3,14 @@ using System.Diagnostics;
namespace Nyanlabs.Umogen.Core.Models;
public class UmoDocumentResult(UmoDoctype type, Person employee, IEntity employer, ValidTime? validity, string? content)
public class UmoDocumentResult(UmoDoctype? type, Person employee, IEntity employer, ValidTime? validity, decimal? payment, UmoAnalModel anal)
{
public UmoDoctype DocType { get; } = type;
public UmoDoctype DocType { get; set; } = type ?? anal.Doctype;
public Person Employee { get; } = employee;
public IEntity Employer { get; } = employer;
public ValidTime? Validity { get; } = validity;
public string? Content { get; } = content;
public decimal? Payment { get; } = payment;
public UmoAnalModel Anal { get; } = anal;
const string ISSUE_DATE = "$DATETIME_ISSUE",
ENACT_DATE = "$DATETIME",
@ -17,24 +18,29 @@ public class UmoDocumentResult(UmoDoctype type, Person employee, IEntity employe
EMPLOYEE = "$EMPLOYEE",
VALID_TIME = "$VALIDITY_TIME",
WORK_TYPE = "$WORK_TYPE",
DYNAMIC_DATA = "$WORK_DYNAMIC_DATA",
DYNAMIC_DATA = "$$WORK_DYNAMIC_DATA$$",
PAYMENT = "$PAYMENT",
PAYMENT_LIT = "$PAYMENT_LIT",
START_DATE = "$WORK_START_DATE";
public async Task<byte[]> ProcessPdf()
public async Task<byte[]> ProcessPdf(string? template = null)
{
string xml = await File.ReadAllTextAsync(DocType.TemplateDoc());
string xml = await File.ReadAllTextAsync(template ?? DocType.TemplateDoc());
xml = xml.Replace(EMPLOYER, Employer.SerializeToDocument())
.Replace(EMPLOYEE, Employee.SerializeToDocument())
.Replace(VALID_TIME, (Validity ?? ValidTime.Invalid).SerializeToDocument())
.Replace(ENACT_DATE, DateTime.Now.ToString("dd.MM.yyyy"))
.Replace(ISSUE_DATE, DateTime.Now.ToString("dd.MM.yyyy"))
.Replace(DYNAMIC_DATA, Content);
.Replace(PAYMENT_LIT, Anal.ValueLiteral)
.Replace(PAYMENT, Payment.Value.ToString())
.Replace(WORK_TYPE, Anal.JobPosition)
.Replace(DYNAMIC_DATA, string.Join('\n', Anal.Conditions.Select(a => $"- {a}")));
string tmp = Path.GetTempFileName();
await File.WriteAllTextAsync(tmp, xml);
await Task.Run(async () =>
{
var proc = Process.Start("soffice", $"--headless --convert-to pdf {tmp}");
var proc = Process.Start("soffice", $"--headless --convert-to pdf --outdir /tmp/ {tmp}");
await proc.WaitForExitAsync();
if (proc.ExitCode != 0)
{
@ -44,6 +50,9 @@ public class UmoDocumentResult(UmoDoctype type, Person employee, IEntity employe
byte[] data = await File.ReadAllBytesAsync(Path.ChangeExtension(tmp, "pdf"));
File.Delete(tmp);
File.Delete(Path.ChangeExtension(tmp, "pdf"));
return data;
}
}

View file

@ -1,6 +0,0 @@
namespace Nyanlabs.Umogen.Core.Pdf;
public class PdfRenderer
{
}

View file

@ -5,7 +5,7 @@ namespace Nyanlabs.Umogen.Core;
public class Umogen
{
public const string DEFAULT_API_KEY_FILE = "umogenkey.secret";
public const string PROMPT = "You analyze employment prompt requests and return document type (one of these 'Umowa o Pracę', 'Umowa Zlecenie', 'Umowa o Dzieło') and elaborate description of job details as a list separated by hyphens and newlines. You write them in format: '<type>\n<description>'";
public const string PROMPT = "You analyze employment prompt requests and return document type (one of these 'Umowa o Pracę', 'Umowa Zlecenie', 'Umowa o Dzieło') and elaborate description of job details as a list separated by hyphens and newlines. Also give a very generalized job position. You write them in format: '{ \"c_type\": \"<contract type>\", \"j_pos\": \"<rodzaj zawodu>\", \"k_lit\": \"<kwota słownie>\", \"conds\": [ <warunki zatrudnienia> ]'";
public static readonly JsonSerializerOptions JSON_OPTS = new(JsonSerializerDefaults.Web)
{
PropertyNamingPolicy = JsonNamingPolicy.SnakeCaseLower

View file

@ -21,14 +21,11 @@ public class GenerationTests
UmoDocument doc = new(UmoDoctype.CONTR_EMPLOYMENT, ModelEmployer, ModelEmployee);
UmoProcess proc = new(eng);
var enu = proc.Ask("I would like to employ Jan Kowalski ID NO CBS3727348 for part time job as a shopkeeper.");
await foreach (var str in enu)
{
Console.Write(str);
}
Console.WriteLine("~");
// var enu = proc.Ask("I would like to employ Jan Kowalski ID NO CBS3727348 for part time job as a shopkeeper and pay 1400zł.");
// await foreach (var str in enu)
// {
// Console.Write(str);
// }
}
}

View file

@ -0,0 +1,47 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;
using Nyanlabs.Umogen.Core;
using Nyanlabs.Umogen.Core.Models;
namespace Nyanlabs.Umogen.CoreTests;
public class PdfTest
{
private Person ModelEmployer => new("Kotek", "Miauczyński", "CBS4327563", "04281308999", new DateTime(2004, 08, 13));
private Person ModelEmployee => new("Miau", "Kotczyński", "CBS69696969", "76011694336", new DateTime(1976, 1, 16));
[Fact]
public async void RenderBoi()
{
// Given
string root = Environment.CurrentDirectory.Replace("test/Nyanlabs.Umogen.WebTests/bin/Debug/net8.0", "");
UmoEngine eng = new(Path.Combine(root, Core.Umogen.DEFAULT_API_KEY_FILE));
UmoProcess p = new(eng);
StringBuilder sb = new();
await foreach (var str in p.Ask($"Zatrudnij {ModelEmployee.NLQuerySerialize()} jako programistę z wynagrodzeniem 12137zł."))
{
Console.Write(str);
sb.Append(str);
}
var anal = JsonSerializer.Deserialize<UmoAnalModel>(sb.ToString(), Core.Umogen.JSON_OPTS);
UmoDocumentResult r = new(null, ModelEmployer, ModelEmployee, ValidTime.Invalid, 2137, anal);
// When
byte[] b = await r.ProcessPdf(Path.Combine(root, $"templates/{r.DocType.TemplateDoc()}"));
// Then
await File.WriteAllBytesAsync(Path.Combine(root, "out.pdf"), b);
}
}