current state of !stuff!

This commit is contained in:
femsci 2023-11-18 04:50:31 +01:00
parent 962f4c15a1
commit 60c896acd1
Signed by: femsci
GPG key ID: 08F7911F0E650C67
18 changed files with 156 additions and 29 deletions

View file

@ -0,0 +1,6 @@
namespace Nyanlabs.Umogen.Core.Models;
public interface IDocSerializable
{
public string SerializeToDocument();
}

View file

@ -1,6 +1,6 @@
namespace Nyanlabs.Umogen.Core.Models; namespace Nyanlabs.Umogen.Core.Models;
public interface IEntity : INLSerializable public interface IEntity : INLSerializable, IDocSerializable
{ {
public string GetName(); public string GetName();
public string? IdCode(); public string? IdCode();

View file

@ -17,8 +17,13 @@ public class LegalEntity(string Name, string? Nip = null, string? Headquarters =
return Nip; return Nip;
} }
public string NLSerialize() public string NLQuerySerialize()
{ {
return $"{Name}; with NIP {Nip ?? "unknown"}{(Headquarters != null ? $"; located in {Headquarters}" : "")}{(Representative != null ? $"; represented by {Representative.GetName()}" : "")}"; return $"{Name}; with NIP {Nip ?? "unknown"}{(Headquarters != null ? $"; located in {Headquarters}" : "")}{(Representative != null ? $"; represented by {Representative.GetName()}" : "")}";
} }
public string SerializeToDocument()
{
return $"{Name}, reprezentowaną przez {Representative.GetName()}";
}
} }

View file

@ -2,5 +2,5 @@ namespace Nyanlabs.Umogen.Core.Models;
public interface INLSerializable public interface INLSerializable
{ {
public string NLSerialize(); public string NLQuerySerialize();
} }

View file

@ -18,8 +18,13 @@ public record Person(string Name, string Surname, string? Id = null, string? Pes
return IDCode!; return IDCode!;
} }
public string NLSerialize() public string NLQuerySerialize()
{ {
return $"{Name} {Surname}; {(Pesel != null ? $"PESEL {Pesel}" : "unknown PESEL")}; {(IDCode != null ? $"ID series {IDCode}" : "unknown ID series")}; {(DoB != null ? $"born {DoB.Value:dd MMM yyyy}" : "unknown date of birth")}"; return $"{Name} {Surname}; {(Pesel != null ? $"PESEL {Pesel}" : "unknown PESEL")}; {(IDCode != null ? $"ID series {IDCode}" : "unknown ID series")}; {(DoB != null ? $"born {DoB.Value:dd MMM yyyy}" : "unknown date of birth")}";
} }
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}" : "")}";
}
} }

View file

@ -1,3 +1,5 @@
using System.Data;
namespace Nyanlabs.Umogen.Core.Models; namespace Nyanlabs.Umogen.Core.Models;
public enum UmoDoctype public enum UmoDoctype
@ -16,6 +18,17 @@ public static class UmoDoctypeBindings
{ UmoDoctype.CONTR_ASS, ("Contract for Assignment", "Umowa o Dzieło") } { UmoDoctype.CONTR_ASS, ("Contract for Assignment", "Umowa o Dzieło") }
}; };
public static string TemplateDoc(this UmoDoctype t)
{
return t switch
{
UmoDoctype.CONTR_EMPLOYMENT => "uop.fodt",
UmoDoctype.CONTR_SERVICE => "uz.fodt",
UmoDoctype.CONTR_ASS => "ud.fodt",
_ => throw new DataException("invalid"),
};
}
public static (string, string) Name(this UmoDoctype t) public static (string, string) Name(this UmoDoctype t)
{ {
return s_umoass[t]; return s_umoass[t];
@ -43,8 +56,8 @@ public class UmoDocument : INLSerializable
public Person Employee { get; set; } public Person Employee { get; set; }
//Null is 'unspecified time' //Null is 'unspecified time'
public ValidTime? ValidTime { get; set; } public ValidTime? ValidTime { get; set; }
public string NLSerialize() public string NLQuerySerialize()
{ {
return $"{Doctype.Name().Item2} for {Employee.NLSerialize()} working for {Employer.NLSerialize()}; valid {ValidTime?.NLSerialize() ?? "for unspecified time"}."; return $"{Doctype.Name().Item2} for {Employee.NLQuerySerialize()} working for {Employer.NLQuerySerialize()}; valid {ValidTime?.NLQuerySerialize() ?? "for unspecified time"}.";
} }
} }

View file

@ -0,0 +1,49 @@
using System.Data;
using System.Diagnostics;
namespace Nyanlabs.Umogen.Core.Models;
public class UmoDocumentResult(UmoDoctype type, Person employee, IEntity employer, ValidTime? validity, string? content)
{
public UmoDoctype DocType { get; } = type;
public Person Employee { get; } = employee;
public IEntity Employer { get; } = employer;
public ValidTime? Validity { get; } = validity;
public string? Content { get; } = content;
const string ISSUE_DATE = "$DATETIME_ISSUE",
ENACT_DATE = "$DATETIME",
EMPLOYER = "$EMPLOYER",
EMPLOYEE = "$EMPLOYEE",
VALID_TIME = "$VALIDITY_TIME",
WORK_TYPE = "$WORK_TYPE",
DYNAMIC_DATA = "$WORK_DYNAMIC_DATA",
START_DATE = "$WORK_START_DATE";
public async Task<byte[]> ProcessPdf()
{
string xml = await File.ReadAllTextAsync(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);
string tmp = Path.GetTempFileName();
await File.WriteAllTextAsync(tmp, xml);
await Task.Run(async () =>
{
var proc = Process.Start("soffice", $"--headless --convert-to pdf {tmp}");
await proc.WaitForExitAsync();
if (proc.ExitCode != 0)
{
throw new DataException("invaliddddd");
}
});
byte[] data = await File.ReadAllBytesAsync(Path.ChangeExtension(tmp, "pdf"));
return data;
}
}

View file

@ -1,6 +1,6 @@
namespace Nyanlabs.Umogen.Core.Models; namespace Nyanlabs.Umogen.Core.Models;
public readonly struct ValidTime : INLSerializable public readonly struct ValidTime : INLSerializable, IDocSerializable
{ {
public ValidTime(DateTime start, DateTime end) public ValidTime(DateTime start, DateTime end)
{ {
@ -14,9 +14,19 @@ public readonly struct ValidTime : INLSerializable
End = start + duration; End = start + duration;
} }
public static readonly ValidTime Invalid = new(DateTime.MaxValue, DateTime.MinValue);
public readonly DateTime Start { get; } public readonly DateTime Start { get; }
public readonly DateTime End { get; } public readonly DateTime End { get; }
public readonly TimeSpan Duration => End - Start; public readonly TimeSpan Duration => End - Start;
public string NLSerialize() => $"from {Start:dd MMM yyyy} until {End:dd MMM yyyy}"; public string NLQuerySerialize()
{
return this.Start != DateTime.MaxValue ? $"from {Start:dd MMM yyyy} until {End:dd MMM yyyy}" : "for unspecified time";
}
public string SerializeToDocument()
{
return this.Start != DateTime.MaxValue ? $"na czas od {Start:dd MMM yyyy} do {End:dd MMM yyyy}" : "na czas nieokreślony";
}
} }

View file

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

View file

@ -1,9 +1,5 @@
using System;
using System.Collections.Generic;
using System.Data; using System.Data;
using System.Linq;
using System.Text.Json; using System.Text.Json;
using System.Threading.Tasks;
namespace Nyanlabs.Umogen.Core; namespace Nyanlabs.Umogen.Core;

View file

@ -5,7 +5,7 @@ namespace Nyanlabs.Umogen.Core;
public class Umogen public class Umogen
{ {
public const string DEFAULT_API_KEY_FILE = "umogenkey.secret"; public const string DEFAULT_API_KEY_FILE = "umogenkey.secret";
public const string PROMPT = "You write Polish legal contracts in Polish language in markdown format. Fill unknown fields with signature placeholder (underscore) with enough space to sign it with pen."; 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 static readonly JsonSerializerOptions JSON_OPTS = new(JsonSerializerDefaults.Web) public static readonly JsonSerializerOptions JSON_OPTS = new(JsonSerializerDefaults.Web)
{ {
PropertyNamingPolicy = JsonNamingPolicy.SnakeCaseLower PropertyNamingPolicy = JsonNamingPolicy.SnakeCaseLower

View file

@ -2,6 +2,9 @@
<PageTitle>umogen</PageTitle> <PageTitle>umogen</PageTitle>
<div class="nav-side">
<Navi />
</div>
<div class="page"> <div class="page">
<main> <main>
<article class="content px-4"> <article class="content px-4">

View file

@ -8,8 +8,23 @@ main {
flex: 1; flex: 1;
} }
.sidebar { .nav-side {
background-image: linear-gradient(180deg, rgb(5, 39, 103) 0%, #3a0647 70%); color: #fefefe;
display: flex;
position: sticky;
top: 0;
left: 0;
margin: 0;
height: 100%;
width: 12rem;
background-image: linear-gradient(
180deg,
rgb(10, 140, 173) 0%,
#198342 25%,
#fe00fe 50%,
#198342 75%,
rgb(10, 140, 173) 100%
);
} }
.top-row { .top-row {

View file

@ -0,0 +1,3 @@
<div class="container">
<h1>umogen</h1>
</div>

View file

@ -0,0 +1,3 @@
h2 {
text-align: center;
}

View file

@ -1,6 +1,19 @@
html, html,
body { body {
font-family: "Helvetica Neue", Helvetica, Arial, sans-serif; font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
position: relative;
width: 100%;
height: 100%;
margin: 0;
}
footer,
header,
hgroup,
menu,
nav,
section {
display: block;
} }
h1:focus { h1:focus {
@ -30,6 +43,12 @@ a,
padding-top: 1.1rem; padding-top: 1.1rem;
} }
.container {
position: relative;
width: 100%;
height: 100%;
}
.valid.modified:not([type="checkbox"]) { .valid.modified:not([type="checkbox"]) {
outline: 1px solid #26b050; outline: 1px solid #26b050;
} }

View file

@ -15,10 +15,10 @@ public class NLSerializationTests
UmoDocument doc = new(UmoDoctype.CONTR_EMPLOYMENT, ModelEmployer, ModelEmployee); UmoDocument doc = new(UmoDoctype.CONTR_EMPLOYMENT, ModelEmployer, ModelEmployee);
// When // When
var query = doc.NLSerialize(); var query = doc.NLQuerySerialize();
// Then // Then
Assert.Equal($"Umowa o Pracę for {ModelEmployee.NLSerialize()} working for {ModelEmployer.NLSerialize()}; valid for unspecified time.", query); Assert.Equal($"Umowa o Pracę for {ModelEmployee.NLQuerySerialize()} working for {ModelEmployer.NLQuerySerialize()}; valid for unspecified time.", query);
} }
[Fact] [Fact]
@ -28,10 +28,10 @@ public class NLSerializationTests
UmoDocument doc = new(UmoDoctype.CONTR_EMPLOYMENT, ModelEmployer, ModelEmployee, new(new DateTime(2023, 11, 17), TimeSpan.FromDays(1))); UmoDocument doc = new(UmoDoctype.CONTR_EMPLOYMENT, ModelEmployer, ModelEmployee, new(new DateTime(2023, 11, 17), TimeSpan.FromDays(1)));
// When // When
var query = doc.NLSerialize(); var query = doc.NLQuerySerialize();
// Then // Then
Assert.Equal($"Umowa o Pracę for {ModelEmployee.NLSerialize()} working for {ModelEmployer.NLSerialize()}; valid from 17 Nov 2023 until 18 Nov 2023.", query); Assert.Equal($"Umowa o Pracę for {ModelEmployee.NLQuerySerialize()} working for {ModelEmployer.NLQuerySerialize()}; valid from 17 Nov 2023 until 18 Nov 2023.", query);
} }
[Fact] [Fact]
@ -41,7 +41,7 @@ public class NLSerializationTests
Person p = ModelEmployer; Person p = ModelEmployer;
// When // When
var query = p.NLSerialize(); var query = p.NLQuerySerialize();
// Then // Then
Assert.Equal("Kotek Miauczyński; PESEL 04281308999; ID series CBS4327563; born 13 Aug 2004", query); Assert.Equal("Kotek Miauczyński; PESEL 04281308999; ID series CBS4327563; born 13 Aug 2004", query);
@ -59,7 +59,7 @@ public class NLSerializationTests
}; };
// When // When
var query = p.NLSerialize(); var query = p.NLQuerySerialize();
// Then // Then
Assert.Equal("Kotek Miauczyński; unknown PESEL; unknown ID series; unknown date of birth", query); Assert.Equal("Kotek Miauczyński; unknown PESEL; unknown ID series; unknown date of birth", query);
@ -74,7 +74,7 @@ public class NLSerializationTests
LegalEntity company = ModelCompany; LegalEntity company = ModelCompany;
// When // When
var query = company.NLSerialize(); var query = company.NLQuerySerialize();
// Then // Then
Assert.Equal("Nyanbyte P.S.A.; with NIP 1313131313; located in Miaumiaśna 13, Miauczki; represented by Kotek Miauczyński", query); Assert.Equal("Nyanbyte P.S.A.; with NIP 1313131313; located in Miaumiaśna 13, Miauczki; represented by Kotek Miauczyński", query);

View file

@ -1,8 +1,3 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Nyanlabs.Umogen.Core; using Nyanlabs.Umogen.Core;
using Nyanlabs.Umogen.Core.Models; using Nyanlabs.Umogen.Core.Models;
@ -26,12 +21,11 @@ public class GenerationTests
UmoDocument doc = new(UmoDoctype.CONTR_EMPLOYMENT, ModelEmployer, ModelEmployee); UmoDocument doc = new(UmoDoctype.CONTR_EMPLOYMENT, ModelEmployer, ModelEmployee);
UmoProcess proc = new(eng); UmoProcess proc = new(eng);
var enu = proc.Ask(doc.NLSerialize()); 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) await foreach (var str in enu)
{ {
Console.Write(str); Console.Write(str);
File.AppendAllText("/tmp/doc.md", str);
} }
Console.WriteLine("~"); Console.WriteLine("~");