Spaces:
No application file
No application file
罗峰
commited on
Commit
·
5f616a6
1
Parent(s):
76fcc1a
文件提交
Browse files- Dockerfile.txt +22 -0
- pom.xml +68 -0
- src/main/java/com/bit/AIController.java +83 -0
- src/main/java/com/bit/DemoApplication.java +20 -0
Dockerfile.txt
ADDED
@@ -0,0 +1,22 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
FROM openjdk:8-alpine
|
2 |
+
MAINTAINER wfq
|
3 |
+
WORKDIR /code
|
4 |
+
COPY . .
|
5 |
+
RUN wget https://dlcdn.apache.org/maven/maven-3/3.9.2/binaries/apache-maven-3.9.2-bin.tar.gz
|
6 |
+
RUN tar -zxvf apache-maven-3.9.2-bin.tar.gz
|
7 |
+
RUN pwd
|
8 |
+
RUN ls -la
|
9 |
+
ENV PATH /code/apache-maven-3.9.2/bin:$PATH
|
10 |
+
ENV MAVEN_HOME=/code/apache-maven-3.9.2
|
11 |
+
RUN mvn -v
|
12 |
+
RUN mvn install -Dmaven.test.skip=true
|
13 |
+
## 更新apk源
|
14 |
+
RUN apk update
|
15 |
+
## 设置时区
|
16 |
+
RUN apk add -U tzdata
|
17 |
+
RUN cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
|
18 |
+
## 设置输出格式
|
19 |
+
ENV LANG en_US.UTF-8
|
20 |
+
ENV LANGUAGE en_US:en
|
21 |
+
ENV LC_ALL en_US.UTF-8
|
22 |
+
ENTRYPOINT [ "sh", "-c", "java $JAVA_OPTS -Duser.region=CN -Duser.timezone=GMT=GMT+8 -Djava.security.egd=file:/dev/./urandom -jar target/demo.jar"]
|
pom.xml
ADDED
@@ -0,0 +1,68 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
<?xml version="1.0" encoding="UTF-8"?>
|
2 |
+
<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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
3 |
+
<modelVersion>4.0.0</modelVersion>
|
4 |
+
|
5 |
+
<groupId>com.bitservice</groupId>
|
6 |
+
<artifactId>Aidemo</artifactId>
|
7 |
+
<version>0.0.1-SNAPSHOT</version>
|
8 |
+
<packaging>jar</packaging>
|
9 |
+
|
10 |
+
<name>demo</name>
|
11 |
+
<description>Demo project for Spring Boot</description>
|
12 |
+
<parent>
|
13 |
+
<groupId>org.springframework.boot</groupId>
|
14 |
+
<artifactId>spring-boot-starter-parent</artifactId>
|
15 |
+
<version>2.3.12.RELEASE</version>
|
16 |
+
<relativePath /> <!-- lookup parent from repository -->
|
17 |
+
</parent>
|
18 |
+
|
19 |
+
<properties>
|
20 |
+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
21 |
+
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
|
22 |
+
<java.version>1.8</java.version>
|
23 |
+
</properties>
|
24 |
+
|
25 |
+
<dependencies>
|
26 |
+
<dependency>
|
27 |
+
<groupId>org.springframework.boot</groupId>
|
28 |
+
<artifactId>spring-boot-starter-web</artifactId>
|
29 |
+
</dependency>
|
30 |
+
|
31 |
+
<dependency>
|
32 |
+
<groupId>org.springframework.boot</groupId>
|
33 |
+
<artifactId>spring-boot-starter-test</artifactId>
|
34 |
+
<scope>test</scope>
|
35 |
+
</dependency>
|
36 |
+
<dependency>
|
37 |
+
<groupId>com.azure</groupId>
|
38 |
+
<artifactId>azure-ai-openai</artifactId>
|
39 |
+
<version>1.0.0-beta.2</version>
|
40 |
+
</dependency>
|
41 |
+
<dependency>
|
42 |
+
<groupId>com.alibaba</groupId>
|
43 |
+
<artifactId>fastjson</artifactId>
|
44 |
+
<version>1.2.47</version>
|
45 |
+
</dependency>
|
46 |
+
<dependency>
|
47 |
+
<groupId>org.apache.httpcomponents</groupId>
|
48 |
+
<artifactId>httpclient</artifactId>
|
49 |
+
</dependency>
|
50 |
+
<dependency>
|
51 |
+
<groupId>org.projectlombok</groupId>
|
52 |
+
<artifactId>lombok</artifactId>
|
53 |
+
<scope>provided</scope>
|
54 |
+
</dependency>
|
55 |
+
</dependencies>
|
56 |
+
|
57 |
+
<build>
|
58 |
+
<finalName>/demo</finalName>
|
59 |
+
<plugins>
|
60 |
+
<plugin>
|
61 |
+
<groupId>org.springframework.boot</groupId>
|
62 |
+
<artifactId>spring-boot-maven-plugin</artifactId>
|
63 |
+
</plugin>
|
64 |
+
</plugins>
|
65 |
+
</build>
|
66 |
+
|
67 |
+
|
68 |
+
</project>
|
src/main/java/com/bit/AIController.java
ADDED
@@ -0,0 +1,83 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
package com.bit;
|
2 |
+
|
3 |
+
import java.io.IOException;
|
4 |
+
|
5 |
+
import lombok.extern.slf4j.Slf4j;
|
6 |
+
import org.apache.http.Header;
|
7 |
+
import org.apache.http.HttpEntity;
|
8 |
+
import org.apache.http.HttpResponse;
|
9 |
+
import org.apache.http.client.ClientProtocolException;
|
10 |
+
import org.apache.http.client.ResponseHandler;
|
11 |
+
import org.apache.http.client.methods.CloseableHttpResponse;
|
12 |
+
import org.apache.http.client.methods.HttpGet;
|
13 |
+
import org.apache.http.client.methods.HttpPost;
|
14 |
+
import org.apache.http.entity.StringEntity;
|
15 |
+
import org.apache.http.impl.client.CloseableHttpClient;
|
16 |
+
import org.apache.http.impl.client.HttpClientBuilder;
|
17 |
+
import org.apache.http.util.EntityUtils;
|
18 |
+
import org.springframework.web.bind.annotation.PostMapping;
|
19 |
+
import org.springframework.web.bind.annotation.RestController;
|
20 |
+
|
21 |
+
import com.alibaba.fastjson.JSONArray;
|
22 |
+
import com.alibaba.fastjson.JSONObject;
|
23 |
+
|
24 |
+
@RestController
|
25 |
+
@Slf4j
|
26 |
+
public class AIController {
|
27 |
+
@PostMapping("/txt2pic")
|
28 |
+
String txt2pic(String prompt, String size) {
|
29 |
+
String result = "";
|
30 |
+
String api_key = System.getenv("OPENAI_API_KEY");
|
31 |
+
String api_base = System.getenv("OPENAI_API_BASE");
|
32 |
+
String api_version = "2023-06-01-preview";
|
33 |
+
String url = api_base + "openai/images/generations:submit?api-version=" + api_version;
|
34 |
+
CloseableHttpClient httpclient = HttpClientBuilder.create().build();
|
35 |
+
HttpPost httpPost = new HttpPost(url);
|
36 |
+
ResponseHandler<String> handler = new ResponseHandler<String>() {
|
37 |
+
@Override
|
38 |
+
public String handleResponse(HttpResponse response) throws ClientProtocolException, IOException {
|
39 |
+
// TODO Auto-generated method stub
|
40 |
+
Header[] headers = response.getHeaders("operation-location");
|
41 |
+
return headers[0].getValue();
|
42 |
+
}
|
43 |
+
};
|
44 |
+
// 设置请求格式
|
45 |
+
String jsonData = "{\"prompt\":\"" + prompt + "\",\"size\":\"" + size + "\",\"n\":1}";
|
46 |
+
log.info("查询请求:"+jsonData);
|
47 |
+
StringEntity entity = new StringEntity(jsonData, "utf-8");// 解决中文乱码问题
|
48 |
+
entity.setContentEncoding("UTF-8");
|
49 |
+
entity.setContentType("application/json");
|
50 |
+
httpPost.setEntity(entity);
|
51 |
+
httpPost.addHeader("api-key", api_key);
|
52 |
+
httpPost.addHeader("Content-Type", "application/json");
|
53 |
+
CloseableHttpResponse response = null;
|
54 |
+
try {
|
55 |
+
result = httpclient.execute(httpPost, handler);
|
56 |
+
HttpGet httpget = new HttpGet(result);
|
57 |
+
httpget.addHeader("api-key", api_key);
|
58 |
+
httpget.addHeader("Content-Type", "application/json");
|
59 |
+
String status = "";
|
60 |
+
while (!status.equals( "succeeded")) {
|
61 |
+
Thread.sleep(1000);
|
62 |
+
response = httpclient.execute(httpget);
|
63 |
+
HttpEntity he = response.getEntity();
|
64 |
+
String ret=EntityUtils.toString(response.getEntity());
|
65 |
+
JSONObject obj = JSONObject.parseObject(ret);
|
66 |
+
status=obj.getString("status");
|
67 |
+
log.info("当前状态:"+status);
|
68 |
+
if (status .equals("succeeded")) {
|
69 |
+
JSONArray js=obj.getJSONObject("result").getJSONArray("data");
|
70 |
+
String images_url=js.getJSONObject(0).getString("url");
|
71 |
+
log.info("获取到图片url:"+images_url);
|
72 |
+
return images_url;
|
73 |
+
}
|
74 |
+
}
|
75 |
+
} catch (Exception e) {
|
76 |
+
// TODO Auto-generated catch block
|
77 |
+
e.printStackTrace();
|
78 |
+
}
|
79 |
+
return null;
|
80 |
+
}
|
81 |
+
|
82 |
+
|
83 |
+
}
|
src/main/java/com/bit/DemoApplication.java
ADDED
@@ -0,0 +1,20 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
package com.bit;
|
2 |
+
|
3 |
+
import org.springframework.boot.SpringApplication;
|
4 |
+
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
5 |
+
import org.springframework.stereotype.Controller;
|
6 |
+
import org.springframework.web.bind.annotation.GetMapping;
|
7 |
+
|
8 |
+
@SpringBootApplication
|
9 |
+
@Controller
|
10 |
+
public class DemoApplication {
|
11 |
+
|
12 |
+
@GetMapping("/")
|
13 |
+
String home() {
|
14 |
+
return "index.html";
|
15 |
+
}
|
16 |
+
|
17 |
+
public static void main(String[] args) {
|
18 |
+
SpringApplication.run(DemoApplication.class, args);
|
19 |
+
}
|
20 |
+
}
|