Prechádzať zdrojové kódy

AI-添加ChatGTPAI帮助类,主页UI优化

liyijie 1 rok pred
rodič
commit
cf83746e18

+ 377 - 0
PDF Office/Helper/ChatGTPAIHelper.cs

@@ -0,0 +1,377 @@
+using Newtonsoft.Json;
+using Newtonsoft.Json.Linq;
+using PDF_Master.Properties;
+using System;
+using System.Collections.Generic;
+using System.IO;
+using System.Linq;
+using System.Net;
+using System.Text;
+using System.Text.RegularExpressions;
+using System.Threading.Tasks;
+
+namespace PDF_Master.Helper
+{
+    public static class ChatGTPAIHelper
+    {
+        private static string host = "http://api-us-east-1.compdf.com:8082";
+
+        private static string translate = "http://101.132.103.13:8030";
+
+        /// <summary>
+        /// 翻译文档Key接口
+        /// </summary>
+        private static string Uri_fileKeytranslate = translate + "/v1/translate/fileUpload";
+
+        /// <summary>
+        /// 翻译文档接口
+        /// </summary>
+        private static string Uri_filetranslate = translate + "/v1/translate/fileTranslateHandle";
+
+        /// <summary>
+        /// 翻译文本接口
+        /// </summary>
+        private static string Uri_texttranslate = translate + "/v1/translate/textTrans";
+
+        ///// <summary>
+        ///// FAQ接口
+        ///// </summary>
+        //private static string Uri_FAQ = host + "/find-faq";
+
+        ///// <summary>
+        ///// 跳转接口
+        ///// </summary>
+        //private static string Uri_GoToView = host + "/recognition";
+
+        ///// <summary>
+        ///// 摘要接口
+        ///// </summary>
+        //private static string Uri_Summary = host + "/summary";
+
+        /// <summary>
+        /// 错别字纠正
+        /// </summary>
+        private static string Uri_Correction = host + "/api/correct-typos";
+
+        /// <summary>
+        /// 错别字纠正
+        /// </summary>
+        private static string Uri_Rewrite = host + "/api/rewrite";
+
+        /// <summary>
+        /// 翻译指定内容,可以指定语言
+        /// </summary>
+        /// <param name="content"></param>
+        /// <param name="language">1=中文 2=英文 3=法语 4=汉语</param>
+        /// <returns></returns>
+        public async static Task<string> fileKeyTranslate(string content, int language)
+        {
+            System.Collections.Specialized.NameValueCollection namevalue = new System.Collections.Specialized.NameValueCollection();
+            //需要翻译的内容
+            namevalue["file"] = content;
+            //1=中文 2=英文 3=法语 4=汉语
+            namevalue["projectId"] = "2";
+            namevalue["version"] = "1.0.1";
+            if (!string.IsNullOrEmpty(Settings.Default.UserDate.id))
+            {
+                namevalue["userId"] = Settings.Default.UserDate.id;
+            }
+            return await PostString(Uri_fileKeytranslate, namevalue);
+        }
+
+        /// <summary>
+        /// 翻译指定内容,可以指定语言
+        /// </summary>
+        /// <param name="fileKey">文本Key</param>
+        /// <param name="fromlanguage">文本语言</param>
+        /// <param name="tolanguage">需要翻译的语言</param>
+        /// <returns></returns>
+        public static String fileTranslate(string fileKey, string fromlanguage, string tolanguage)
+        {
+
+            HttpWebResponse response = null;
+            ServicePointManager.DefaultConnectionLimit = 200;
+            HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(Uri_filetranslate);
+            request.Method = "Post";
+            request.ContentType = "application/json";
+            //request.Accept = "application/vnd.api+json;version=1";
+            request.UserAgent = "Apifox/1.0.0 (https://www.apifox.cn)";
+            request.Timeout = 20000;
+            request.ServicePoint.Expect100Continue = false;
+            StringWriter sw = new StringWriter();
+            using (JsonWriter writer = new JsonTextWriter(sw))
+            {
+                writer.WriteStartObject();
+                writer.WritePropertyName("fileKey");
+                writer.WriteValue(fileKey);
+                writer.WritePropertyName("from");
+                writer.WriteValue(fromlanguage);
+                writer.WritePropertyName("to");
+                writer.WriteValue(tolanguage);
+                writer.WritePropertyName("projectId");
+                writer.WriteValue("2");
+                writer.WritePropertyName("version");
+                writer.WriteValue("1.0.1");
+                writer.WritePropertyName("userId");
+                writer.WriteValue(Settings.Default.UserDate.id);
+                writer.WriteEndObject();
+            }
+            try
+            {
+                string postBody = sw.ToString();
+                using (StreamWriter writer = new StreamWriter(request.GetRequestStream()))
+                {
+                    writer.Write(postBody);
+                    writer.Close();
+                }
+
+                response = (HttpWebResponse)request.GetResponse();
+                using (StreamReader reader = new StreamReader(response.GetResponseStream()))
+                {
+                    string responseData = reader.ReadToEnd();
+                    Console.WriteLine(responseData);
+                    reader.Close();
+                    JObject jobject = (JObject)JsonConvert.DeserializeObject(responseData);
+                    if (response != null)
+                    {
+                        response.Close();
+                    }
+                    if (request != null)
+                    {
+                        request.Abort();
+                    }
+
+                    if (jobject["code"].ToObject<string>().ToLower() == "200")
+                    {
+
+                    }
+                    //return jobject["code"].ToObject<string>().ToLower();
+
+                    return "200";
+                }
+            }
+            catch
+            {
+                return "300";
+            }
+
+        }
+
+
+        /// <summary>
+        /// 翻译指定内容,可以指定语言
+        /// </summary>
+        /// <param name="content">翻译内容</param>
+        /// <param name="fromlanguage">文本语言</param>
+        /// <param name="tolanguage">需要翻译的语言</param>
+        /// <returns></returns>
+        public static String textTranslate(string content, string fromlanguage, string tolanguage)
+        {
+
+            HttpWebResponse response = null;
+            ServicePointManager.DefaultConnectionLimit = 200;
+            HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(Uri_filetranslate);
+            request.Method = "Post";
+            request.ContentType = "application/json";
+            //request.Accept = "application/vnd.api+json;version=1";
+            request.UserAgent = "Apifox/1.0.0 (https://www.apifox.cn)";
+            request.Timeout = 20000;
+            request.ServicePoint.Expect100Continue = false;
+            StringWriter sw = new StringWriter();
+            using (JsonWriter writer = new JsonTextWriter(sw))
+            {
+                writer.WriteStartObject();
+                writer.WritePropertyName("q");
+                writer.WriteValue(content);
+                writer.WritePropertyName("from");
+                writer.WriteValue(fromlanguage);
+                writer.WritePropertyName("to");
+                writer.WriteValue(tolanguage);
+                writer.WritePropertyName("projectId");
+                writer.WriteValue("2");
+                writer.WritePropertyName("version");
+                writer.WriteValue("1.0.1");
+                writer.WritePropertyName("userId");
+                writer.WriteValue(Settings.Default.UserDate.id);
+                writer.WriteEndObject();
+            }
+            try
+            {
+                string postBody = sw.ToString();
+                using (StreamWriter writer = new StreamWriter(request.GetRequestStream()))
+                {
+                    writer.Write(postBody);
+                    writer.Close();
+                }
+
+                response = (HttpWebResponse)request.GetResponse();
+                using (StreamReader reader = new StreamReader(response.GetResponseStream()))
+                {
+                    string responseData = reader.ReadToEnd();
+                    Console.WriteLine(responseData);
+                    reader.Close();
+                    JObject jobject = (JObject)JsonConvert.DeserializeObject(responseData);
+                    if (response != null)
+                    {
+                        response.Close();
+                    }
+                    if (request != null)
+                    {
+                        request.Abort();
+                    }
+
+                    if (jobject["code"].ToObject<string>().ToLower() == "200")
+                    {
+
+                    }
+                    //return jobject["code"].ToObject<string>().ToLower();
+
+                    return "200";
+                }
+            }
+            catch
+            {
+                return "300";
+            }
+
+        }
+
+        public async static Task<string> textTranslate(string content, int language)
+        {
+            System.Collections.Specialized.NameValueCollection namevalue = new System.Collections.Specialized.NameValueCollection();
+            //需要翻译的内容
+            namevalue["q"] = content;
+            //1=中文 2=英文 3=法语 4=汉语
+            namevalue["from"] = "auto";
+            namevalue["to"] = "en";
+            namevalue["projectId"] = "2";
+            namevalue["version"] = "1.0.1";
+            if (!string.IsNullOrEmpty(Settings.Default.UserDate.id))
+            {
+                namevalue["userId"] = Settings.Default.UserDate.id;
+            }
+            return await PostString(Uri_filetranslate, namevalue);
+        }
+
+        
+
+
+        /// <summary>
+        /// 纠错
+        /// </summary>
+        /// <param name="content"></param>
+        /// <returns></returns>
+        public async static Task<string> Correction(string content)
+        {
+            System.Collections.Specialized.NameValueCollection namevalue = new System.Collections.Specialized.NameValueCollection();
+            //需要纠错的内容
+            namevalue["project_id"] = "3";
+            namevalue["version"] = "1.0.1";
+            if (!string.IsNullOrEmpty(Settings.Default.UserDate.id))
+            {
+                namevalue["user_id"] = Settings.Default.UserDate.id;
+            }
+            namevalue["content"] = content;
+            return await PostString(Uri_Correction, namevalue);
+        }
+
+        /// <summary>
+        /// 重写
+        /// </summary>
+        /// <param name="content"></param>
+        /// <returns></returns>
+        public async static Task<string> Rewrite(string content)
+        {
+            System.Collections.Specialized.NameValueCollection namevalue = new System.Collections.Specialized.NameValueCollection();
+            //需要纠错的内容
+            namevalue["project_id"] = "3";
+            namevalue["version"] = "1.0.1";
+            if (!string.IsNullOrEmpty(Settings.Default.UserDate.id))
+            {
+                namevalue["user_id"] = Settings.Default.UserDate.id;
+            }
+            namevalue["content"] = content;
+            return await PostString(Uri_Rewrite, namevalue);
+        }
+
+
+        ///// <summary>
+        ///// 页面跳转
+        ///// </summary>
+        ///// <param name="content"></param>
+        ///// <returns></returns>
+        //public async static Task<string> GoToView(string content)
+        //{
+        //    System.Collections.Specialized.NameValueCollection namevalue = new System.Collections.Specialized.NameValueCollection();
+        //    //需要翻译的内容
+        //    namevalue["str"] = content;
+        //    return await PostString(Uri_GoToView, namevalue, "flag");
+        //}
+
+        /// <summary>
+        /// 从链接中返回存在的链接(仅返回第一个找到的链接)
+        /// </summary>
+        /// <param name="content"></param>
+        /// <returns></returns>
+        public static List<string> GetLinkFromString(string content)
+        {
+            if (string.IsNullOrEmpty(content))
+            {
+                return null;
+            }
+            // 创建正则表达式模式
+            string pattern = @"(https?://[^\s]+)";
+
+            // 获取所有匹配项
+            MatchCollection matches = Regex.Matches(content, pattern);
+
+            List<string> list = new List<string>();
+            // 遍历并打印所有匹配项
+            foreach (Match match in matches)
+            {
+                list.Add(match.Value);
+            }
+            return list;
+        }
+
+        /// <summary>
+        /// 从接口获取信息,获取message对应value值
+        /// </summary>
+        /// <param name="url"></param>
+        /// <param name="namevalue"></param>
+        /// <returns></returns>
+        private async static Task<string> PostString(string url, System.Collections.Specialized.NameValueCollection namevalue, string key = "message")
+        {
+            string repsonseData = "";
+            try
+            {
+                using (var client = new WebClient())
+                {
+                    byte[] bytes = await client.UploadValuesTaskAsync(url, namevalue);
+                    //转换成字符串类型
+                    var json = Encoding.Default.GetString(bytes);
+                    //将Json格式字符串转换成键值对
+                    var values = JsonConvert.DeserializeObject<Dictionary<string, string>>(json);
+
+                    string unicode = "";
+                    // 遍历字典对象输出键值对
+                    foreach (KeyValuePair<string, string> item in values)
+                    {
+                        if (item.Key == key)
+                        {
+                            unicode = item.Value;
+                        }
+                    }
+                    //将Unicode格式转换成String
+                    repsonseData = Regex.Unescape(unicode);
+                }
+            }
+            catch (Exception ex)
+            {
+                return null;
+            }
+            return repsonseData;
+        }
+    }
+}
+

+ 5 - 0
PDF Office/PDF Master.csproj

@@ -393,6 +393,7 @@
     <Compile Include="EventAggregators\ViewContentEvent.cs" />
     <Compile Include="Helper\ADServiceHelper.cs" />
     <Compile Include="Helper\CacheFilePath.cs" />
+    <Compile Include="Helper\ChatGTPAIHelper.cs" />
     <Compile Include="Helper\ConverterHelper.cs" />
     <Compile Include="Helper\CropPageUndoManager.cs" />
     <Compile Include="Helper\ErrorCodeHelper.cs" />
@@ -2352,6 +2353,10 @@
     <Resource Include="Resources\HomeTools\add_files.png" />
     <Resource Include="Resources\HomeTools\Translation.png" />
     <Resource Include="Resources\HomeTools\ChatGPTTranslation.png" />
+    <Resource Include="Resources\HomeIcon\home.png" />
+    <Resource Include="Resources\HomeIcon\Translation.png" />
+    <Resource Include="Resources\HomeIcon\Rewriting.png" />
+    <Resource Include="Resources\HomeIcon\Error_Correction.png" />
     <Content Include="source\models\OCR.model">
       <CopyToOutputDirectory>Always</CopyToOutputDirectory>
     </Content>

BIN
PDF Office/Resources/HomeIcon/Error_Correction.png


BIN
PDF Office/Resources/HomeIcon/Rewriting.png


BIN
PDF Office/Resources/HomeIcon/Translation.png


BIN
PDF Office/Resources/HomeIcon/home.png


+ 45 - 12
PDF Office/Views/HomeContent.xaml

@@ -60,14 +60,23 @@
         <StackPanel>
             <Button
                 x:Name="Home"
-                Height="40"
+                Height="64"
                 Margin="32,32,32,0"
                 Command="{Binding ShowToolCommand}"
                 CommandParameter="Guid"
                 Foreground="#FFFFFF"
-                Style="{StaticResource btn.brand}">
+                Style="{StaticResource NoColorBtn}">
+                
                 <StackPanel Orientation="Horizontal">
+                    <Image
+                         Margin="0,0,8,0"
+                    Width="28"
+                    Height="28"
+                    Source="pack://application:,,,/PDF Master;component/Resources/HomeIcon/home.png" />
                     <TextBlock
+                        Width="156"
+                        Foreground="{StaticResource color.sys.text.neutral.lv1}"
+                        FontWeight="DemiBold"
                         VerticalAlignment="Center"
                         FontFamily="Segoe UI"
                         FontSize="16"
@@ -75,14 +84,22 @@
                 </StackPanel>
             </Button>
             <Button
-                Height="40"
-                Margin="32,32,32,0"
+                Height="64"
+                Margin="32,8,32,0"
                 Command="{Binding ShowToolCommand}"
                 CommandParameter="ChatGPTAITranslationContent"
                 Foreground="#FFFFFF"
-                Style="{StaticResource btn.brand}">
+                Style="{StaticResource NoColorBtn}">
                 <StackPanel Orientation="Horizontal">
+                    <Image
+                         Margin="0,0,8,0"
+                    Width="28"
+                    Height="28"
+                    Source="pack://application:,,,/PDF Master;component/Resources/HomeIcon/Translation.png" />
                     <TextBlock
+                        Width="156"
+                        Foreground="{StaticResource color.sys.text.neutral.lv1}"
+                        FontWeight="DemiBold"
                         VerticalAlignment="Center"
                         FontFamily="Segoe UI"
                         FontSize="16"
@@ -90,14 +107,22 @@
                 </StackPanel>
             </Button>
             <Button
-                Height="40"
-                Margin="32,32,32,0"
+                Height="64"
+                Margin="32,8,32,0"
                 Command="{Binding ShowToolCommand}"
                 CommandParameter="ChatGPTAIRewritingContent"
                 Foreground="#FFFFFF"
-                Style="{StaticResource btn.brand}">
+                Style="{StaticResource NoColorBtn}">
                 <StackPanel Orientation="Horizontal">
+                    <Image
+                        Margin="0,0,8,0"
+                    Width="28"
+                    Height="28"
+                    Source="pack://application:,,,/PDF Master;component/Resources/HomeIcon/Rewriting.png" />
                     <TextBlock
+                        Width="156"
+                        Foreground="{StaticResource color.sys.text.neutral.lv1}"
+                        FontWeight="DemiBold"
                         VerticalAlignment="Center"
                         FontFamily="Segoe UI"
                         FontSize="16"
@@ -105,14 +130,22 @@
                 </StackPanel>
             </Button>
             <Button
-                Height="40"
-                Margin="32,32,32,0"
+                Height="64"
+                Margin="32,8,32,0"
                 Command="{Binding ShowToolCommand}"
                 CommandParameter="ChatGPTAIErrorCorrectionContent"
                 Foreground="#FFFFFF"
-                Style="{StaticResource btn.brand}">
+                Style="{StaticResource NoColorBtn}">
                 <StackPanel Orientation="Horizontal">
+                    <Image
+                         Margin="0,0,8,0"
+                    Width="28"
+                    Height="28"
+                    Source="pack://application:,,,/PDF Master;component/Resources/HomeIcon/Error_Correction.png" />
                     <TextBlock
+                        Width="156"
+                        Foreground="{StaticResource color.sys.text.neutral.lv1}"
+                        FontWeight="DemiBold"
                         VerticalAlignment="Center"
                         FontFamily="Segoe UI"
                         FontSize="16"
@@ -123,7 +156,7 @@
                 Visibility="Collapsed"
                 x:Name="BtnOpenPDF"
                 Height="40"
-                Margin="32,32,32,0"
+                Margin="32,8,32,0"
                 Command="{Binding OpenFileCommand}"
                 Foreground="#FFFFFF"
                 Style="{StaticResource btn.brand}">