Browse Source

add documentAI

ChingYi 9 months ago
parent
commit
360726111c
4 changed files with 250 additions and 0 deletions
  1. 81 0
      DocAI/Data/RedactInfoExtractResponse.cs
  2. 51 0
      DocAI/Data/VerifyUserResponse.cs
  3. 115 0
      DocAI/DocAI.cs
  4. 3 0
      KdanCommon.csproj

+ 81 - 0
DocAI/Data/RedactInfoExtractResponse.cs

@@ -0,0 +1,81 @@
+using Newtonsoft.Json;
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace KdanCommon.DocAI.Data
+{
+    public partial class RedactInfoExtractResponse
+    {
+        [JsonProperty("pages")]
+        public Page[] Pages { get; set; }
+
+        [JsonProperty("infos")]
+        public Info[] Infos { get; set; }
+    }
+
+    public partial class Info
+    {
+        [JsonProperty("type")]
+        public string Type { get; set; }
+
+        [JsonProperty("blocks")]
+        public Block[] Blocks { get; set; }
+    }
+
+    public partial class Block
+    {
+        [JsonProperty("content")]
+        public string Content { get; set; }
+
+        [JsonProperty("extractionContent")]
+        public string ExtractionContent { get; set; }
+
+        [JsonProperty("extractionType")]
+        public string ExtractionType { get; set; }
+
+        [JsonProperty("boundingRegions")]
+        public BoundingRegion[] BoundingRegions { get; set; }
+    }
+
+    public partial class BoundingRegion
+    {
+        [JsonProperty("pageNumber")]
+        public long PageNumber { get; set; }
+
+        [JsonProperty("boundingPolygon")]
+        public BoundingPolygon[] BoundingPolygon { get; set; }
+    }
+
+    public partial class BoundingPolygon
+    {
+        [JsonProperty("isEmpty")]
+        public bool IsEmpty { get; set; }
+
+        [JsonProperty("x")]
+        public double X { get; set; }
+
+        [JsonProperty("y")]
+        public double Y { get; set; }
+    }
+
+    public partial class Page
+    {
+        [JsonProperty("pageNumber")]
+        public long PageNumber { get; set; }
+
+        [JsonProperty("angle")]
+        public long Angle { get; set; }
+
+        [JsonProperty("width")]
+        public double Width { get; set; }
+
+        [JsonProperty("height")]
+        public double Height { get; set; }
+
+        [JsonProperty("unit")]
+        public long Unit { get; set; }
+    }
+}

+ 51 - 0
DocAI/Data/VerifyUserResponse.cs

@@ -0,0 +1,51 @@
+using Newtonsoft.Json;
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace KdanCommon.DocAI.Data
+{
+    public partial class VerifyUserResponse
+    {
+        [JsonProperty("user_id")]
+        public long UserId { get; set; }
+
+        [JsonProperty("email")]
+        public string Email { get; set; }
+
+        [JsonProperty("redact")]
+        public Redact Redact { get; set; }
+
+        [JsonProperty("chatbot")]
+        public Chatbot Chatbot { get; set; }
+
+        [JsonProperty("available_licenses")]
+        public string AvailableLicenses { get; set; }
+
+        [JsonProperty("current_time")]
+        public DateTimeOffset CurrentTime { get; set; }
+
+        [JsonProperty("access_ai")]
+        public bool AccessAi { get; set; }
+    }
+
+    public partial class Chatbot
+    {
+        [JsonProperty("bot_used")]
+        public int BotUsed { get; set; }
+
+        [JsonProperty("available_bots")]
+        public int AvailableBots { get; set; }
+    }
+
+    public partial class Redact
+    {
+        [JsonProperty("pages_used")]
+        public int PagesUsed { get; set; }
+
+        [JsonProperty("available_pages")]
+        public int AvailablePages { get; set; }
+    }
+}

+ 115 - 0
DocAI/DocAI.cs

@@ -0,0 +1,115 @@
+using KdanCommon.DocAI.Data;
+using KdanCommon.Helpers;
+using Newtonsoft.Json;
+using System;
+using System.Collections.Generic;
+using System.IO;
+using System.Linq;
+using System.Net.Http;
+using System.Net.Mime;
+using System.Text;
+using System.Threading.Tasks;
+using Windows.Media.Protection.PlayReady;
+using Windows.Storage;
+using Windows.Web.Http.Headers;
+using System.Xml.Linq;
+using System.IO.Pipes;
+
+namespace KdanCommon.DocAI
+{
+    public class DocAI
+    {
+        // DocAI website : https://kdanai.azurewebsites.net/swagger/index.html
+
+        private static string DocAIDomain = "https://kdanai.azurewebsites.net";
+
+        private HttpClient _httpClient = null;
+        private string _appName = null;
+        private string _appVersion = null;
+        private string _platform = null;
+
+        private Uri _verifyUserUri = null;
+        private Uri _redactInfoExtractUri = null;
+
+        public DocAI(string appName, string appVersion)
+        {
+            _httpClient = new HttpClient();
+            _appName = appName;
+            _appVersion = appVersion;
+            _platform = "uwp";
+
+            _verifyUserUri = new Uri($"{DocAIDomain}/verify_user");
+            _redactInfoExtractUri = new Uri($"{DocAIDomain}/redact_info_extract");
+        }
+
+        ~DocAI()
+        {
+            _httpClient.Dispose();
+        }
+
+        #region Public Method
+        public void SetAccessToken(string accessToken)
+        {
+            _httpClient.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", accessToken);
+        }
+
+        public async Task VerifyUser()
+        {
+            var content = new MultipartFormDataContent();
+            var result = await _httpClient.PostAsync(_verifyUserUri, content);
+            if (result.StatusCode == System.Net.HttpStatusCode.OK)
+            {
+                var jsonString = await result.Content.ReadAsStringAsync();
+                var response = JsonTool.DeserializeJSON<VerifyUserResponse>(jsonString);
+                if (response != null)
+                {
+
+                }
+            }
+        }
+
+        public async Task RedactInfoExtract(StorageFile file, string redactInfo, string docType, string pageRange = null, string redactCustomInfo = null)
+        {
+            using (var fileStream = await file.OpenStreamForReadAsync())
+            {
+                var content = new MultipartFormDataContent();
+                var streamContent = new StreamContent(fileStream);
+                streamContent.Headers.ContentDisposition = new System.Net.Http.Headers.ContentDispositionHeaderValue("form-data");
+                streamContent.Headers.ContentDisposition.Name = "redact_file";
+                streamContent.Headers.ContentDisposition.FileName = file.Name;
+                content.Add(streamContent);
+                content.Add(CreateStringContent(redactInfo, "redact_info"));
+                content.Add(CreateStringContent(_appName, "app_name"));
+                content.Add(CreateStringContent(_appVersion, "app_version"));
+                content.Add(CreateStringContent(_platform, "platform"));
+                content.Add(CreateStringContent(docType, "doc_type"));
+                if (!string.IsNullOrEmpty(redactCustomInfo))
+                    content.Add(CreateStringContent(redactCustomInfo, "redact_custom_info"));
+                if (!string.IsNullOrEmpty(pageRange))
+                    content.Add(CreateStringContent(pageRange, "page_range"));
+                var result = await _httpClient.PostAsync(_redactInfoExtractUri, content);
+                if (result.StatusCode == System.Net.HttpStatusCode.OK)
+                {
+                    var jsonString = await result.Content.ReadAsStringAsync();
+                    var response = JsonTool.DeserializeJSON<RedactInfoExtractResponse>(jsonString);
+                    if (response != null)
+                    {
+                      
+                    }
+                }
+            }
+        }
+
+        #endregion
+
+        #region Private Method
+        private StringContent CreateStringContent(string content, string name)
+        {
+            var stringContent = new StringContent(content);
+            stringContent.Headers.ContentDisposition = new System.Net.Http.Headers.ContentDispositionHeaderValue("form-data");
+            stringContent.Headers.ContentDisposition.Name = name;
+            return stringContent;
+        }
+        #endregion
+    }
+}

+ 3 - 0
KdanCommon.csproj

@@ -126,6 +126,9 @@
     <Compile Include="CMSCollection\Data\ViewerEventBarSettingResponse.cs" />
     <Compile Include="CMSCollection\Data\ViewerEventBarSettingResponse.cs" />
     <Compile Include="CMSCollection\Data\WindowsCardsResponse.cs" />
     <Compile Include="CMSCollection\Data\WindowsCardsResponse.cs" />
     <Compile Include="CMSCollection\Data\WindowsEventbarResponse.cs" />
     <Compile Include="CMSCollection\Data\WindowsEventbarResponse.cs" />
+    <Compile Include="DocAI\Data\RedactInfoExtractResponse.cs" />
+    <Compile Include="DocAI\Data\VerifyUserResponse.cs" />
+    <Compile Include="DocAI\DocAI.cs" />
     <Compile Include="GoogleCloud\Data\Lang.cs" />
     <Compile Include="GoogleCloud\Data\Lang.cs" />
     <Compile Include="GoogleCloud\Data\Vision\ImgesRequest.cs" />
     <Compile Include="GoogleCloud\Data\Vision\ImgesRequest.cs" />
     <Compile Include="GoogleCloud\Data\Vision\ImgesResponse.cs" />
     <Compile Include="GoogleCloud\Data\Vision\ImgesResponse.cs" />