rongrunxiang il y a 5 mois
commit
c8c71df651

+ 33 - 0
.gitignore

@@ -0,0 +1,33 @@
+HELP.md
+target/
+!.mvn/wrapper/maven-wrapper.jar
+!**/src/main/**/target/
+!**/src/test/**/target/
+
+### STS ###
+.apt_generated
+.classpath
+.factorypath
+.project
+.settings
+.springBeans
+.sts4-cache
+
+### IntelliJ IDEA ###
+.idea
+*.iws
+*.iml
+*.ipr
+
+### NetBeans ###
+/nbproject/private/
+/nbbuild/
+/dist/
+/nbdist/
+/.nb-gradle/
+build/
+!**/src/main/**/build/
+!**/src/test/**/build/
+
+### VS Code ###
+.vscode/

+ 0 - 0
models/a.txt


+ 0 - 0
models/b


+ 67 - 0
pom.xml

@@ -0,0 +1,67 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
+    <modelVersion>4.0.0</modelVersion>
+    <parent>
+        <groupId>org.springframework.boot</groupId>
+        <artifactId>spring-boot-starter-parent</artifactId>
+        <version>2.7.14</version>
+        <relativePath/> <!-- lookup parent from repository -->
+    </parent>
+    <groupId>org.example</groupId>
+    <artifactId>car</artifactId>
+    <version>0.0.1-SNAPSHOT</version>
+    <name>car</name>
+    <description>car</description>
+    <url/>
+    <licenses>
+        <license/>
+    </licenses>
+    <developers>
+        <developer/>
+    </developers>
+    <scm>
+        <connection/>
+        <developerConnection/>
+        <tag/>
+        <url/>
+    </scm>
+    <properties>
+        <java.version>1.8</java.version>
+    </properties>
+    <dependencies>
+        <dependency>
+            <groupId>org.springframework.boot</groupId>
+            <artifactId>spring-boot-starter-web</artifactId>
+        </dependency>
+
+        <dependency>
+            <groupId>org.projectlombok</groupId>
+            <artifactId>lombok</artifactId>
+            <optional>true</optional>
+        </dependency>
+        <dependency>
+            <groupId>org.springframework.boot</groupId>
+            <artifactId>spring-boot-starter-test</artifactId>
+            <scope>test</scope>
+        </dependency>
+    </dependencies>
+
+    <build>
+        <plugins>
+            <plugin>
+                <groupId>org.springframework.boot</groupId>
+                <artifactId>spring-boot-maven-plugin</artifactId>
+                <configuration>
+                    <excludes>
+                        <exclude>
+                            <groupId>org.projectlombok</groupId>
+                            <artifactId>lombok</artifactId>
+                        </exclude>
+                    </excludes>
+                </configuration>
+            </plugin>
+        </plugins>
+    </build>
+
+</project>

+ 13 - 0
src/main/java/org/example/car/CarApplication.java

@@ -0,0 +1,13 @@
+package org.example.car;
+
+import org.springframework.boot.SpringApplication;
+import org.springframework.boot.autoconfigure.SpringBootApplication;
+
+@SpringBootApplication
+public class CarApplication {
+
+    public static void main(String[] args) {
+        SpringApplication.run(CarApplication.class, args);
+    }
+
+}

+ 63 - 0
src/main/java/org/example/car/controller/Tool.java

@@ -0,0 +1,63 @@
+package org.example.car.controller;
+
+import org.example.car.model.Judge;
+import org.example.car.utils.FileUtil;
+import org.springframework.stereotype.Controller;
+import org.springframework.web.bind.annotation.GetMapping;
+import org.springframework.web.bind.annotation.PostMapping;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.ResponseBody;
+
+import java.io.IOException;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.Paths;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.stream.Collectors;
+
+@ResponseBody
+@RequestMapping("/tool")
+@Controller
+public class Tool {
+
+    @PostMapping("/call")
+    public Judge callTools(String modelName, String uid) {
+        Judge res = new Judge();
+        List<String> models = getModels();
+        if(modelName == null || !models.contains(modelName)){
+            res.setAccuracy(0.0);
+            res.setLosses(0.0);
+            res.setMemoryInfoList(0.0);
+            return res;
+        }
+        String command = "python --input-model-dir " + "models/" + modelName
+                + "--outout-dir" + "output-dir"
+                + " --output-timestamp" + uid;
+        try{
+            Process process = Runtime.getRuntime().exec(command);
+        }catch (IOException e) {
+            e.printStackTrace();
+            res.setAccuracy(0.0);
+            res.setLosses(0.0);
+            res.setMemoryInfoList(0.0);
+            return res;
+        }
+
+
+        return res;
+    }
+
+    @GetMapping("/getModels")
+    public List<String> getModels() {
+        String modelsDirectory = "models";
+        try{
+            return FileUtil.listModels(modelsDirectory);
+        }catch (IOException e){
+            e.printStackTrace();
+            return new ArrayList<>();
+        }
+    }
+
+
+}

+ 10 - 0
src/main/java/org/example/car/model/CallResult.java

@@ -0,0 +1,10 @@
+package org.example.car.model;
+
+import lombok.Data;
+
+@Data
+public class CallResult {
+    private String model;
+    private String method;
+    private Judge result;
+}

+ 10 - 0
src/main/java/org/example/car/model/Judge.java

@@ -0,0 +1,10 @@
+package org.example.car.model;
+
+import lombok.Data;
+
+@Data
+public class Judge {
+    Double Losses;
+    Double Accuracy;
+    Double MemoryInfoList;
+}

+ 42 - 0
src/main/java/org/example/car/model/R.java

@@ -0,0 +1,42 @@
+package org.example.car.model;
+
+import lombok.Data;
+
+import java.io.Serializable;
+
+@Data
+public class R<T> implements Serializable {
+    private static final long serialVersionUID = 1L;
+    private Integer code;
+    private String msg;
+    private T data;
+
+    public static <T> R<T> success(T object) {
+        return new R<T>(1, object, "");
+    }
+
+    public static <T> R<T> success() {
+        return new R<T>(1, null, "");
+    }
+
+    public static <T> R<T> error(String msg) {
+        return new R<T>(0, null, msg);
+    }
+
+
+    public static <T> R<T> error(int code, String message) {
+        return new R<T>(code, null, message);
+    }
+
+
+    public R(int code, T data, String msg) {
+        this.code = code;
+        this.data = data;
+        this.msg = msg;
+    }
+
+    public R(int code, T data) {
+        this(code, data, "");
+    }
+
+}

+ 31 - 0
src/main/java/org/example/car/utils/FileUtil.java

@@ -0,0 +1,31 @@
+package org.example.car.utils;
+
+import org.example.car.controller.Tool;
+
+import java.io.IOException;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.Paths;
+import java.util.List;
+import java.util.stream.Collectors;
+
+public class FileUtil {
+
+    public static List<String> listModels(String path) throws IOException {
+        Path dir = Paths.get(path);
+        return Files.walk(dir,1)
+                .filter(p -> !Files.isDirectory(p))
+                .map(p -> p.getFileName().toString())
+                .map(FileUtil::removeExtension)
+                .collect(Collectors.toList());
+    }
+
+    public static String removeExtension(String filename) {
+        int pos = filename.lastIndexOf(".");
+        if (pos == -1) {
+            return filename;
+        }
+        return filename.substring(0, pos);
+    }
+
+}

+ 1 - 0
src/main/resources/application.properties

@@ -0,0 +1 @@
+spring.application.name=car

+ 13 - 0
src/test/java/org/example/car/CarApplicationTests.java

@@ -0,0 +1,13 @@
+package org.example.car;
+
+import org.junit.jupiter.api.Test;
+import org.springframework.boot.test.context.SpringBootTest;
+
+@SpringBootTest
+class CarApplicationTests {
+
+    @Test
+    void contextLoads() {
+    }
+
+}