文心一言智能写诗

package com.baidu.nlp.baidu_nlp.control;


import com.baidu.aip.nlp.AipNlp;

import okhttp3.*;

import java.io.*;
import java.lang.reflect.Array;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
import java.util.Set;

import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
class Sample {
    public static final String API_KEY = "";
    public static final String SECRET_KEY = "";
    static final OkHttpClient HTTP_CLIENT = new OkHttpClient().newBuilder().build();

    
      @RequestMapping(value="/baidu_nlp_poem",method = RequestMethod.GET)  
   	  public  String pome() throws Exception, InterruptedException{
    	  
      while(true){
      Scanner scann=new Scanner(System.in);
      String welcome="你好!我是智能小助手,我可以帮你写诗!";
       for (int i = 0; i < welcome.length(); i++) {
		 System.out.print(welcome.charAt(i));
  		 Thread.sleep(100);
 	  }
       
      System.out.println();
      String want="请输入你想要作诗的标题";
      for (int i = 0; i < want.length(); i++) {
		System.out.print(want.charAt(i));
		 Thread.sleep(100);
	  }
      System.out.println("");
      String poem=scann.nextLine();
      String later="请稍后.......";
      for (int i = 0; i < later.length(); i++) {
		System.out.print(later.charAt(i));
		 Thread.sleep(100);
	  }
     System.out.println();	
    	MediaType mediaType = MediaType.parse("application/json");
         
        RequestBody body = RequestBody.create(mediaType, "{\"text\":\""
        		+ ""
        		+ poem
        		+ "\"}");
        Request request = new Request.Builder()
            .url("https://aip.baidubce.com/rpc/2.0/nlp/v1/poem?access_token=" + getAccessToken())
            .method("POST", body)
            .addHeader("Content-Type", "application/json")
            .addHeader("Accept", "application/json")
            .build();
        Response response = HTTP_CLIENT.newCall(request).execute();
        
        String result=response.body().string();
 
        JSONObject json = new JSONObject(result);
        //System.out.println(result);
        String poem1=json.get("poem").toString();
        poem1=poem1.replace("[", "");
        poem1=poem1.replace("]", "");
        JSONObject json1 = new JSONObject(poem1);
        System.out.println("您好!"+json1.get("title")+"诗句如下:");
        String poemOuput=json1.get("content")+"";
        for (int i = 0; i < poemOuput.length(); i++) {
  		System.out.print(poemOuput.charAt(i));
  		 Thread.sleep(100);
  	   }
        System.out.println(); 
    }
    }
    
    /**
     * 从用户的AK,SK生成鉴权签名(Access Token)
     *
     * @return 鉴权签名(Access Token)
     * @throws IOException IO异常
     */
    static String getAccessToken() throws IOException {
        MediaType mediaType = MediaType.parse("application/x-www-form-urlencoded");
        RequestBody body = RequestBody.create(mediaType, "grant_type=client_credentials&client_id=" + API_KEY
                + "&client_secret=" + SECRET_KEY);
        Request request = new Request.Builder()
                .url("https://aip.baidubce.com/oauth/2.0/token")
                .method("POST", body)
                .addHeader("Content-Type", "application/x-www-form-urlencoded")
                .build();
        Response response = HTTP_CLIENT.newCall(request).execute();
        return new JSONObject(response.body().string()).getString("access_token");
    }
    
   
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值