山东大学项目实训 创新实训 法律文书专家系统 个人报告四 项目开发 : 法律文书摘要生成 法律预测功能的开发

个人工作:


在本项目中,我主要负责后端程序的开发,个人工作主要围绕设计数据库,开发后端服务,与DeepSeek模型进行交互等方面。由于一些其他因素,本项目后端转为使用java开发,另外我本人还负责了一部分前端代码,主要是前后端交互部分,和部分界面布局的改进。目前项目还未接入mongodb数据库,此工作将会在后期进行。

 

法律文书摘要生成:

功能界面:

山东大学项目实训 创新实训 法律文书专家系统 个人报告四 项目开发 : 法律文书摘要生成 法律预测功能的开发_第1张图片

可以选择上传文件,也可以拖曳上传文件

后端将上传的文件进行处理,提取出其中的文字信息(txt,docx,pdf文件均支持),然后将文字信息进行包装,发给模型进行处理,然后将模型返回的结果发挥前端

控制器代码:


@RestController
@RequestMapping("/wenshu")

public class WenShuController {


    @PostMapping("/upload")
    public Result handleFileUpload(@RequestParam("file") MultipartFile file) {
        String filename = file.getOriginalFilename();
        if (filename == null) {
            return Result.error("空文件");
        }

        try {
            String textContent = extractTextFromFile(file);

            // 构建请求

            ObjectMapper mapper = new ObjectMapper();
            Map data = new HashMap<>();
            data.put("document", textContent);

            String jsonBody = mapper.writeValueAsString(data);


            HttpRequest request = HttpRequest.newBuilder()
                    .uri(URI.create("http://localhost:7860/summarize"))
                    .header("Content-Type", "application/json")
                    .POST(HttpRequest.BodyPublishers.ofString(jsonBody))
                    .build();

            // 发送请求并处理响应

            HttpClient client = HttpClient.newHttpClient();
            HttpResponse response = client.send(
                    request, HttpResponse.BodyHandlers.ofString());

            System.out.println("Status code: " + response.statusCode());

            System.out.println("Response body: " + response.body());

            //返回结果
            return Result.success(response.body());


        } catch (UnsupportedOperationException e) {

            return Result.error("不支持的文件类型");
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("文件解析失败");
        }
    }

    private String extractTextFromFile(MultipartFile file) throws Exception {
        String filename = file.getOriginalFilename();
        if (filename == null) throw new UnsupportedOperationException("无效文件名");

        if (filename.endsWith(".txt")) {
            return readTxt(file.getInputStream());
        } else if (filename.endsWith(".pdf")) {
            return readPdf(file.getInputStream());
        } else if (filename.endsWith(".docx")) {
            return readDocx(file.getInputStream());
        } else {
            throw new UnsupportedOperationException("不支持的文件类型: " + filename);
        }
    }

    private String readTxt(InputStream is) throws IOException {
        BufferedReader reader = new BufferedReader(new InputStreamReader(is));
        StringBuilder sb = new StringBuilder();
        String line;
        while ((line = reader.readLine()) != null) {
            sb.append(line).append("\n");
        }
        return sb.toString();
    }

    private String readPdf(InputStream is) throws IOException {
        try (PDDocument document = PDDocument.load(is)) {
            PDFTextStripper stripper = new PDFTextStripper();
            return stripper.getText(document);
        }
    }

    private String readDocx(InputStream is) throws IOException {
        try (XWPFDocument docx = new XWPFDocument(is)) {
            XWPFWordExtractor extractor = new XWPFWordExtractor(docx);
            return extractor.getText();
        }
    }
}




 

 

 

法律预测报告:

功能界面:

山东大学项目实训 创新实训 法律文书专家系统 个人报告四 项目开发 : 法律文书摘要生成 法律预测功能的开发_第2张图片可以选择上传文件,也可以拖曳上传文件,上传的文件将会被提取文字,自动填充在 输入框中,也可以自己修改输入框,点击发送将会将输入框中的信息发送至后端

然后将文字信息进行包装,发给模型进行处理,然后将模型返回的结果发挥前端

控制器代码:

 

@RestController
@RequestMapping("/yuce")


public class YuceController {

    @PostMapping("/upload")
    public Result handleFileUpload(@RequestParam("file") MultipartFile file) {
        String filename = file.getOriginalFilename();
        if (filename == null) {
            return Result.error("空文件");
        }

        try {
            String textContent = extractTextFromFile(file);
            System.out.println(textContent);
                return Result.success(textContent);

        } catch (UnsupportedOperationException e) {

            return Result.error("不支持的文件类型");
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("文件解析失败");
        }
    }


    @PostMapping("/send")
    public Result handleRequest(@RequestBody String requestData) {
        System.out.println("接收到的字符串: " + requestData);
        // 构建请求
        try {

        //requestData="现在,你作为法律专家,根据上述案件信息,给出法律预测报告,如罚款金额,刑期时长等:\n"+requestData+"@";


        ObjectMapper mapper = new ObjectMapper();
        Map data = new HashMap<>();
        data.put("fact", requestData);

        String jsonBody = mapper.writeValueAsString(data);


        HttpRequest request = HttpRequest.newBuilder()
                .uri(URI.create("http://localhost:7860/prediction"))
                .header("Content-Type", "application/json")
                .POST(HttpRequest.BodyPublishers.ofString(jsonBody))
                .build();

        // 发送请求并处理响应

        HttpClient client = HttpClient.newHttpClient();
        HttpResponse response = client.send(
                request, HttpResponse.BodyHandlers.ofString());

        System.out.println("Status code: " + response.statusCode());
        System.out.println("Response body: " + response.body());



            //返回结果
        return Result.success(response.body());



        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("错误");
        }
    }












    private String extractTextFromFile(MultipartFile file) throws Exception {
        String filename = file.getOriginalFilename();
        if (filename == null) throw new UnsupportedOperationException("无效文件名");

        if (filename.endsWith(".txt")) {
            return readTxt(file.getInputStream());
        } else if (filename.endsWith(".pdf")) {
            return readPdf(file.getInputStream());
        } else if (filename.endsWith(".docx")) {
            return readDocx(file.getInputStream());
        } else {
            throw new UnsupportedOperationException("不支持的文件类型: " + filename);
        }
    }

    private String readTxt(InputStream is) throws IOException {
        BufferedReader reader = new BufferedReader(new InputStreamReader(is));
        StringBuilder sb = new StringBuilder();
        String line;
        while ((line = reader.readLine()) != null) {
            sb.append(line).append("\n");
        }
        return sb.toString();
    }

    private String readPdf(InputStream is) throws IOException {
        try (PDDocument document = PDDocument.load(is)) {
            PDFTextStripper stripper = new PDFTextStripper();
            return stripper.getText(document);
        }
    }


    private String readDocx(InputStream is) throws IOException {
        try (XWPFDocument docx = new XWPFDocument(is)) {
            XWPFWordExtractor extractor = new XWPFWordExtractor(docx);
            return extractor.getText();
        }
    }
}






 

 

 

你可能感兴趣的:(法律文书专家系统项目记录,后端,人工智能,学习,java)