【百度大脑新品体验】智能春联写诗~C#开荒攻略
goJhou 发布于2019-04 浏览:6341 回复:14
4
收藏
最后编辑于2022-04

哈喽 雷迪斯安德 尖头们 嗨起来!

 

是我勾勾~

本期又来开教程贴啦,这次带来的是新品喔!热乎的C#智能春联与写诗调用教程~

 

文档参考这里哟!

http://ai.baidu.com/docs#/IntelligentWriting-API/f85d34cc

 

step 0! 检查权限!

各位小主进入自己的应用列表,编辑一下应用,看看结构化智能写作是否勾选上啦?

 

Step 1!获取accesstoken!

万事找入口,ak第一步!

String authHost = "https://aip.baidubce.com/oauth/2.0/token";
HttpClient client = new HttpClient();
List> paraList = new List>();
paraList.Add(new KeyValuePair("grant_type", "client_credentials"));
paraList.Add(new KeyValuePair("client_id", clientId));
paraList.Add(new KeyValuePair("client_secret", clientSecret));

HttpResponseMessage response = client.PostAsync(authHost, new FormUrlEncodedContent(paraList)).Result;
String result = response.Content.ReadAsStringAsync().Result;
TOKEN= JObject.Parse(result)["access_token"].ToString();


Step 2!燥起来!

核心代码供出:

string url = "https://aip.baidubce.com/rpc/2.0/nlp/v1/couplets?access_token=" + TOKEN;
string poemURL = "https://aip.baidubce.com/rpc/2.0/nlp/v1/poem?access_token=" + TOKEN;

JObject obj = new JObject();

obj["text"] = "百度";
obj["index"] = 0;


HttpWebRequest poem = (HttpWebRequest)WebRequest.Create(poemURL);

poem.ContentType = "application/json";
poem.Method = "POST";

byte[] poemcontent = Encoding.UTF8.GetBytes(obj.ToString());

using (Stream poemStr = poem.GetRequestStream())
{
poemStr.Write(poemcontent, 0, poemcontent.Length);
}
HttpWebResponse poemresp = (HttpWebResponse)poem.GetResponse();
Stream poemreceiveStream = poemresp.GetResponseStream();
string poemres = new StreamReader(poemreceiveStream).ReadToEnd();

 

加上合适的UI呈现,简单的demo就完成啦~

下方是以 百度 生成的春联与诗喔~

 

 

本demo项目基于.net 4.5. 依赖newtonsoft.json

demo工程链接:https://gitee.com/Jack.Zhou/Baidu.SmartPoem

 

最后祝小主们 5分钟速通~good night~

 

收藏
点赞
4
个赞
共14条回复 最后由用户已被禁言回复于2022-04
#16goJhou回复于2019-08
#15 yu3242296回复
干的漂亮

????

0
#15yu3242296回复于2019-08

干的漂亮

0
#14风搅火回复于2019-04
#13 goJhou回复
项目代码可以兼容本次更新喔。我跑了下没任何问题

太敬业了,赞一个

0
#13goJhou回复于2019-04
#12 用户已被禁言回复
智能写诗/春联接口文档已更新,麻烦同步更新一下,谢谢~ 链接:https://ai.baidu.com/docs#/IntelligentWriting-API/f85d34cc
展开

项目代码可以兼容本次更新喔。我跑了下没任何问题

0
#12用户已被禁言回复于2019-04

智能写诗/春联接口文档已更新,麻烦同步更新一下,谢谢~

链接:https://ai.baidu.com/docs#/IntelligentWriting-API/f85d34cc

0
#11goJhou回复于2019-04
#10 风搅火回复
动态春联,很特别

一起玩一玩

0
#10风搅火回复于2019-04

动态春联,很特别

0
#9rose20135188回复于2019-04

谢谢分享,有了这个以后自己写春联贴

0
#8痛了会哭的骚年回复于2019-04
#7 笔墨哥回复
好嗨哟!感觉AI已经达到了高潮~

好震撼,好夺目

0
#7笔墨哥回复于2019-04

好嗨哟!感觉AI已经达到了高潮~

0
#5痛了会哭的骚年回复于2019-04

以前只用过网上的智能写诗工具,没想到有一天自己也能搞出一个来

0
#4让天涯回复于2019-04

不错,不用再担心自己会文思枯竭了。。。

0
#3goJhou回复于2019-04
#2 liguanghui2588回复
这佬可以,跟着学学春联制作

好的哇~

0
#2liguanghui2588回复于2019-04

这佬可以,跟着学学春联制作

0
TOP
切换版块