B/S架构的实现代码用例
时间: 2023-12-09 12:03:44 浏览: 85
以下是一个简单的B/S架构实现的代码用例,使用Java语言和Spring框架:
1. 前端代码(HTML、CSS、JavaScript):
```html
<!DOCTYPE html>
<html>
<head>
<title>B/S Architecture Example</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css">
</head>
<body>
<div class="container mt-5">
<h1>B/S Architecture Example</h1>
<div class="form-group">
<label for="input-text">Enter some text:</label>
<input type="text" class="form-control" id="input-text" placeholder="Type something...">
</div>
<button type="button" class="btn btn-primary" onclick="sendText()">Send</button>
<hr>
<div id="output"></div>
</div>
<script src="https://code.jquery.com/jquery-3.5.1.slim.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs@3.2.0/dist/tf.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/@tensorflow-models/universal-sentence-encoder@4.2.0/dist/universal-sentence-encoder.min.js"></script>
<script src="app.js"></script>
</body>
</html>
```
2. 后端代码(Java + Spring):
```java
package com.example.bsaexample;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import org.tensorflow.Tensor;
import org.tensorflow.Tensors;
import org.tensorflow.framework.ConfigProto;
import org.tensorflow.framework.GPUOptions;
import org.tensorflow.framework.Session;
import org.tensorflow.framework.Session.Runner;
import org.tensorflow.framework.TensorShape;
import java.util.Arrays;
@SpringBootApplication
@RestController
public class BsaExampleApplication {
public static void main(String[] args) {
SpringApplication.run(BsaExampleApplication.class, args);
}
@PostMapping(value = "/predict", consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
public String predict(@RequestBody String input) {
GPUOptions gpuOptions = GPUOptions.newBuilder().setVisibleDeviceList("0").build();
ConfigProto configProto = ConfigProto.newBuilder().setGpuOptions(gpuOptions).build();
try (Session session = Session.create(configProto.toByteArray())) {
Tensor<String> inputTensor = Tensors.create(new String[] { input });
Runner runner = session.runner().feed("input", inputTensor).fetch("output");
Tensor<Float> outputTensor = runner.run().get(0).expect(Float.class);
float[] output = outputTensor.copyTo(new float[1][512])[0];
return Arrays.toString(output);
} catch (Exception e) {
e.printStackTrace();
return "Error: " + e.getMessage();
}
}
}
```
3. 前后端交互代码(JavaScript):
```javascript
async function sendText() {
const input = $("#input-text").val();
if (input.trim() !== "") {
$("#output").text("Loading...");
const output = await predict(input);
$("#output").text(output);
}
}
async function predict(input) {
const model = await tf.loadGraphModel("model/model.json");
const encoder = await use.load();
const encodedInput = await encoder.embed([ input ]);
const inputTensor = tf.tensor(encodedInput);
const outputTensor = model.execute({ "input": inputTensor });
const output = await outputTensor.array();
return output;
}
```
这个例子实现了一个简单的文本编码预测功能,用户在前端输入一段文本,通过JavaScript调用后端的REST API进行计算,后端使用TensorFlow进行计算并返回结果。这个例子还包括了一些常见的开发框架和库,如Spring、Bootstrap、jQuery、TensorFlow.js和Universal Sentence Encoder。
阅读全文
相关推荐
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![docx](https://img-home.csdnimg.cn/images/20241231044901.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)