运用AI搭建中间服务层(四)

MiddlewareService文件夹

在这个文件夹中,我们需要添加以下文件:

  • 名人服务.cs

  • 名人服务.cs

  • 名人结果.cs

  • ILandmarkService.cs

  • 地标服务 .cs

  • 地标结果 .cs

ICelebrityService.cs – 包装多个串行的认知服务来实现名人识别的中间服务层的接口定义,需要依赖注入

using System.Threading.Tasks;namespace CognitiveMiddlewareService.MiddlewareService
{public interface ICelebrityService{Task<CelebrityResult> Do(byte[] imgData);}
}

CelebrityService.cs – 包装多个串行的认知服务来实现名人识别中间服务层的逻辑代码

using CognitiveMiddlewareService.CognitiveServices;
using Newtonsoft.Json;
using System.Threading.Tasks;namespace CognitiveMiddlewareService.MiddlewareService
{public class CelebrityService : ICelebrityService{private readonly IVisionService visionService;private readonly IEntitySearchService entityService;public CelebrityService(IVisionService vs, IEntitySearchService ess){this.visionService = vs;this.entityService = ess;}public async Task<CelebrityResult> Do(byte[] imgData){// get original recognized resultvar stream = Helper.GetStream(imgData);Celebrity celebrity = await this.visionService.RecognizeCelebrityAsync(stream);if (celebrity != null){// get entity search resultstring entityName = celebrity.name;string jsonResult = await this.entityService.SearchEntityAsync(entityName);EntityResult er = JsonConvert.DeserializeObject<EntityResult>(jsonResult);if (er?.entities?.value.Length > 0){// isolation layer: decouple data structure then return abstract resultCelebrityResult cr = new CelebrityResult(){Name = er.entities.value[0].name,Description = er.entities.value[0].description,Url = er.entities.value[0].url,ThumbnailUrl = er.entities.value[0].image.thumbnailUrl,Confidence = celebrity.confidence};return cr;}}return null;}}
}

小提示:上面的代码中,用CelebrityResult接管了实体搜索结果和名人识别结果的部分有效字段,以达到解耦/隔离的作用,后面的代码只关心CelebrityResult如何定义的即可。

CelebrityResult.cs – 抽象出来的名人识别服务的返回结果

namespace CognitiveMiddlewareService.MiddlewareService
{public class CelebrityResult{public string Name { get; set; }public double Confidence { get; set; }public string Url { get; set; }public string Description { get; set; }public string ThumbnailUrl { get; set; }}
}

ILandmarkService.cs – 包装多个串行的认知服务来实现地标识别的中间服务层的接口定义,需要依赖注入

using CognitiveMiddlewareService.CognitiveServices;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;namespace CognitiveMiddlewareService.MiddlewareService
{public interface ILandmarkService{Task<LandmarkResult> Do(byte[] imgData);}
}

LandmarkService.cs – 包装多个串行的认知服务来实现地标识别的中间服务层的逻辑代码

using CognitiveMiddlewareService.CognitiveServices;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;namespace CognitiveMiddlewareService.MiddlewareService
{public class LandmarkService : ILandmarkService{private readonly IVisionService visionService;private readonly IEntitySearchService entityService;public LandmarkService(IVisionService vs, IEntitySearchService ess){this.visionService = vs;this.entityService = ess;}public async Task<LandmarkResult> Do(byte[] imgData){// get original recognized resultvar streamLandmark = Helper.GetStream(imgData);Landmark landmark = await this.visionService.RecognizeLandmarkAsync(streamLandmark);if (landmark != null){// get entity search resultstring entityName = landmark.name;string jsonResult = await this.entityService.SearchEntityAsync(entityName);EntityResult er = JsonConvert.DeserializeObject<EntityResult>(jsonResult);// isolation layer: decouple data structure then return abstract resultLandmarkResult lr = new LandmarkResult(){Name = er.entities.value[0].name,Description = er.entities.value[0].description,Url = er.entities.value[0].url,ThumbnailUrl = er.entities.value[0].image.thumbnailUrl,Confidence = landmark.confidence};return lr;}return null;}}
}

小提示:上面的代码中,用LandmarkResult接管了实体搜索结果和地标识别结果的部分有效字段,以达到解耦/隔离的作用,后面的代码只关心LandmarkResult如何定义的即可。

LandmarkResult.cs – 抽象出来的地标识别服务的返回结果

namespace CognitiveMiddlewareService.MiddlewareService
{public class LandmarkResult{public string Name { get; set; }public double Confidence { get; set; }public string Url { get; set; }public string Description { get; set; }public string ThumbnailUrl { get; set; }}
}

Processors文件夹

在这个文件夹中,我们需要添加以下文件:

  • IProcessService.cs

  • 进程服务 .cs

  • 聚合结果.cs

IProcessService.cs – 任务调度层服务的接口定义,需要依赖注入

using System.Threading.Tasks;namespace CognitiveMiddlewareService.Processors
{public interface IProcessService{Task<AggregatedResult> Process(byte[] imgData);}
}

ProcessService.cs – 任务调度层服务的逻辑代码

using CognitiveMiddlewareService.MiddlewareService;
using System.Collections.Generic;
using System.Threading.Tasks;namespace CognitiveMiddlewareService.Processors
{public class ProcessService : IProcessService{private readonly ILandmarkService landmarkService;private readonly ICelebrityService celebrityService;public ProcessService(ILandmarkService ls, ICelebrityService cs){this.landmarkService = ls;this.celebrityService = cs;}public async Task<AggregatedResult> Process(byte[] imgData){// preprocess// todo: create screening image classifier to get a rough category, then decide call which service// task dispatcher: parallelized run 'Do'// todo: put this logic into Dispatcher serviceList<Task> listTask = new List<Task>();var taskLandmark = this.landmarkService.Do(imgData);listTask.Add(taskLandmark);var taskCelebrity = this.celebrityService.Do(imgData);listTask.Add(taskCelebrity);await Task.WhenAll(listTask);LandmarkResult lmResult = taskLandmark.Result;CelebrityResult cbResult = taskCelebrity.Result;// aggregator// todo: put this logic into Aggregator serviceAggregatedResult ar = new AggregatedResult(){Landmark = lmResult,Celebrity = cbResult};return ar;// ranker// todo: if there have more than one result in AgregatedResult, need give them a ranking// output generator// todo: generate specified JSON data, such as Adptive Card}}
}

小提示:大家可以看到上面这个文件中有很多绿色的注释,带有todo文字的,对于一个更复杂的系统,可以用这些todo中的描述来设计独立的模块。

AggregatedResult.cs – 任务调度层服务的最终聚合结果定义

using CognitiveMiddlewareService.MiddlewareService;namespace CognitiveMiddlewareService.Processors
{public class AggregatedResult{public LandmarkResult Landmark { get; set; }public CelebrityResult Celebrity { get; set; }}
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/617017.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

大模型学习与实践笔记(二)

一、代码仓库&#xff1a; InternLM: https://github.com/InternLM/InternLM/ 课程讲师&#xff1a;宋志学大佬&#xff0c;d2l-ai-solutions-manual 开源项目负责人 二、Lagent框架 三、基于InternLM的智能对话 3.1 环境配置&#xff1a; cuda11.7 pytorch2.0.1 其他环境…

C++每日一练(15):简单幂计算

题目描述 输入两个数a和b&#xff0c;求a的b次方。 输入 输入两个整数a&#xff0c;b&#xff08;1<a<10&#xff0c;1<b<15&#xff09;。 输出 输出一个正整数&#xff0c;该值<1000000000000。 输入样例 3 3 输出样例 27 参考答案 #include<bits/stdc.h&…

CDH 6.3启动失败,由于日志写入权限原因导致cloudera-scm-server.log未生成

CDH 6.3启动失败&#xff0c;CM之前都能正常启动&#xff0c;服务器重启后&#xff0c;启动出现异常&#xff0c;需要排查具体错误&#xff0c;查看日志&#xff0c;发现日志cloudera-scm-server.log也未生成&#xff0c;不好定位具体原因。于是查看cloudera-scm-server状态&am…

CVE-2023-36025 Windows SmartScreen 安全功能绕过漏洞

CVE-2023-36025是微软于11月补丁日发布的安全更新中修复Windows SmartScreen安全功能绕过漏洞。攻击者可以通过诱导用户单击特制的URL来利用该漏洞&#xff0c;对目标系统进行攻击。成功利用该漏洞的攻击者能够绕过Windows Defender SmartScreen检查及其相关提示。该漏洞的攻击…

PCIe进阶之Gen3 Physical Layer Receive Logic(一)

1 文章概述 本篇文章是接着前面两篇文章进一步研究Gen3 Physical Layer Receive Logic的实现,具体包含Differential Receiver,CDR(Clock and Data Recovery)和Receiver Clock Compensation Logic 三个部分的介绍和解析。 1.1 Differential Receiver Gen3的Differential …

JBOD详解

JBOD是存储领域中一类重要的存储设备。 英文全称Just a bunch of disks, 中文也称之为硬盘存储扩展柜。 它是传统存储系统赖以生存的根基之一&#xff0c;如果没有JBOD&#xff0c;那一下子就会省去很多部件&#xff1a;后端HBA、SAS扩展器/FC成环器、线缆、JBOD控制模块等等…

【已解决】如何用递归实现位运算计算两数之和

本博文源于笔者正在思考的如何用递归进行计算两数之和。读者一般都会想到用while循环进行操作&#xff0c;位运算两数之和的思想就犹如辗转相除法。文章并附加了对这个方法的流程演示 问题来源 想要用递归实现两数之和。 代码实现 #include<stdio.h> int add(int num…

68.网游逆向分析与插件开发-角色数据的获取-利用蓝量属性分析角色数据基址

内容参考于&#xff1a;易道云信息技术研究院VIP课 上一个内容&#xff1a;67.网游逆向分析与插件开发-角色数据的获取-分析角色数据基址-CSDN博客 然后分析任何一个东西&#xff0c;逆向分析的本质就是找东西的意思&#xff0c;找东西核心的观念是内存里得有&#xff0c;就是…

[足式机器人]Part2 Dr. CAN学习笔记 - Ch03 傅里叶级数与变换

本文仅供学习使用 本文参考&#xff1a; B站&#xff1a;DR_CAN Dr. CAN学习笔记-Ch03 傅里叶级数与变换 1. 三角函数的正交性2. 周期为 2 π 2\pi 2π的函数展开为傅里叶级数3. 周期为 2 L 2L 2L的函数展开4. 傅里叶级数的复数形式5. 从傅里叶级数推导傅里叶变换FT6. 总结 1. …

《mybatis》--大数据量查询解决方案

阿丹-需求/场景&#xff1a; 之前写百万以及千万的导出数据的时候&#xff0c;对于将数据写道csv文件并压缩这里没有什么大问题了&#xff0c;但是出现了其他问题为&#xff1a; 1、我们需要将数据从数据库中拿出来&#xff0c;并且在进行装配的时候出现了一些问题。 2、对于整…

GPT实战系列-ChatGLM3管理工具的API接口

GPT实战系列-ChatGLM3管理外部借力工具 用ChatGLM的工具可以实现很多查询接口和执行命令&#xff0c;外部工具该如何配置使用&#xff1f;如何联合它们实现大模型查询助手功能&#xff1f;例如调用工具实现股票信息查询&#xff0c;网络天气查询等助手功能。 LLM大模型相关文章…

高效底座模型LLaMA

论文标题&#xff1a;LLaMA: Open and Efficient Foundation Language Models 论文链接&#xff1a;https://arxiv.org/pdf/2302.13971.pdf 论文来源&#xff1a;Meta AI 1 概述 大型语言模型&#xff08;Large Languages Models&#xff0c;LLMs&#xff09;通过大规模文本数…

TMC2226步进电机驱动---学习记录

基于TMC2226数据手册的学习 主要内容介绍&#xff1a; Package Outline TMC2226 手册中引脚解释&#xff08;按照手册表格顺序&#xff09; 了解每个引脚是接什么的&#xff0c;之后看原理图 &#xff08;借用立创广场kirito的原理图&#xff0c;后期换个&#xff09; 以前的疑…

UIUC CS241 讲义:众包系统编程书

原文&#xff1a;angrave/SystemProgramming 译者&#xff1a;飞龙 协议&#xff1a;CC BY-NC-SA 4.0 欢迎来到 Angrave 的众包系统编程维基书&#xff01;这个维基是由伊利诺伊大学的学生和教师共同建立的&#xff0c;是伊利诺伊大学 CS 的 Lawrence Angrave 的众包创作实验。…

SpringMVC的四种跳转方式

默认的跳转是请求转发&#xff0c;直接跳转到jsp页面展示&#xff0c;还可以使用框架提供的关键字redirect:&#xff0c;进行一个重定向操作&#xff0c;包括重定向页面和重定向action&#xff0c;使用框架提供的关键字forward:&#xff0c;进行服务器内部转发操作&#xff0c;…

【Golang】补码二进制字符串转整型

原码反码补码移码介绍 在计算机科学中&#xff0c;原码、反码和补码是用来表示有符号整数的三种不同的二进制编码方式。下面将详细解释每一种编码方式的特点和用途。 原码 原码是最直观的有符号数表示方法。在原码表示法中&#xff0c;最高位&#xff08;符号位&#xff09;用…

如何解决NAND系统性能问题?--NAND分类

一、故事引言 想象一下&#xff0c;你正在管理一座神奇的数据仓库&#xff0c;这个仓库没有沉重的门、旋转的磁盘和机械手臂&#xff0c;而是由一群训练有素的“数据小飞侠”组成。这些小飞侠们居住在一个叫做闪存芯片&#xff08;NAND Flash&#xff0c;本文主人公&#xff0…

vue3+TS使用component 组件的实例

目录 一.项目中实际使用 1.代码使用 二.元素 1.使用方式&#xff1a; 2.代码解释 一.项目中实际使用 1.代码使用 之前&#xff0c;我们使用过&#xff0c;在login相关的几个页面使用component 实现登录/注册/找回密码等页面的动态切换&#xff0c;在天转到这个页面时&…

第一天业务题

1-1 请说一下你项目中是如何进行项目管理和发布的 我们项目主要是用到GOGS进行项目代码的管理,jenkins进行项目的部署和编译. 首先GOGS部署在我们公司的服务器上,可以保证代码的安全,在日常的工作中,首先我会将代码拉取到本地,然后功能开发完毕后PUSH到远端,然后GOGS会向JenKin…

Bug:Goland左侧丢失项目结构(Goland常用快捷键)

Goland快捷键&小tips 1 常用快捷键 # 格式化代码 optioncommandL# 在项目中搜索文件中的内容 commandshiftF# 搜索.go文件 shiftshift&#xff08;按两次shift&#xff09;# 修改方法、变量&#xff08;同时替换引用处的名称&#xff09; fnshiftF6# 将选中代码抽取为方法…