File size: 10,648 Bytes
7f6d402
8f010e5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
72b4aa1
8f010e5
 
 
72b4aa1
8f010e5
 
 
 
 
 
 
72b4aa1
8f010e5
 
 
 
 
72b4aa1
8f010e5
 
 
 
 
 
 
 
 
 
 
 
 
7f6d402
8f010e5
7f6d402
8f010e5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7f6d402
8f010e5
 
 
 
 
 
 
 
7f6d402
 
 
8f010e5
 
 
 
 
 
 
 
 
 
7f6d402
8f010e5
 
 
72b4aa1
8f010e5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
72b4aa1
 
 
 
 
 
 
 
 
 
 
 
123ade7
72b4aa1
 
 
 
 
 
 
 
7f6d402
72b4aa1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
123ade7
72b4aa1
 
 
 
 
 
 
7f6d402
 
72b4aa1
 
 
 
 
 
 
 
 
 
 
 
7f6d402
72b4aa1
 
 
123ade7
72b4aa1
123ade7
72b4aa1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7f6d402
72b4aa1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
<html>
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <!-- import vue -->
    <script src="https://unpkg.com/vue@next"></script>
    <!-- import Elm CSS -->
    <link rel="stylesheet" href="https://unpkg.com/element-plus/dist/index.css">
    <!-- import Elm JavaScript -->
    <script src="https://unpkg.com/element-plus"></script>
    <!-- import axios -->
    <script src="https://unpkg.com/axios@1.1.2/dist/axios.min.js"></script>
    <title>mj</title>
    <style>
      body {
        display: flex;
        justify-content: center;
        background-color: #f5f5f5;
      }

      #app {
        width: 1000px;
      }

      .task {
        border: 1px solid #ddd;
        background-color: #fff;
        border-radius: 10px;
        margin-bottom: 20px;
        padding: 20px;
      }

      image {
        border-radius: 10px;
      }
    </style>
  </head>

  <body>
    <div id="app">
      <el-form label-position="top">
        <el-form-item label="请输入要生成的图片内容,尽量使用英文">
          <el-input type="textarea" v-model="question" placeholder="例如:A golden Labrador dog"></el-input>
          <el-button style="margin-top: 10px;" type="primary" @click="submit" :loading="loading">提交</el-button>
        </el-form-item>
      </el-form>
      <div class="result" style="margin-top: 10px;">
        <h1>我的作品</h1>
        <div v-for="(item, index) in tasks" class="task" :key="item.msg_Id">
          <p>任务id:<el-button link type="primary">{{item.msg_Id}}</el-button> </p>
          <p>{{item.question}}</p>
          <p>
            <el-button type="primary" @click="getResult(item.msg_Id)">刷新</el-button>
          </p>
          <template v-if="item.result">
            <el-image fit="scale-down" lazy :src="item.result.url" :alt="item.question"></el-image>
            <p>
              <el-button type="primary" plain @click="variation(item, 1)">V1</el-button>
              <el-button type="primary" plain @click="variation(item, 2)">V2</el-button>
              <el-button type="primary" plain @click="variation(item, 3)">V3</el-button>
              <el-button type="primary" plain @click="variation(item, 4)">V4</el-button>
            </p>
            <p>
              <el-button type="primary" plain @click="upscale(item, 1)">U1</el-button>
              <el-button type="primary" plain @click="upscale(item, 2)">U2</el-button>
              <el-button type="primary" plain @click="upscale(item, 3)">U3</el-button>
              <el-button type="primary" plain @click="upscale(item, 4)">U4</el-button>
            </p>
            <template v-if="item.result.upscaleUrl">
              <h5>大图</h5>
              <el-image fit="scale-down" lazy :src="item.result.upscaleUrl" :alt="item.question"></el-image>
            </template>
          </template>
        </div>
        <h2>其他人的作品(最近10个)</h2>
        <div v-for="item in otherCase" :key="item.id" class="task">
          <h3>prompt:{{ formatPrompt(item.content) }}</h3>
          <h3>time:{{ formatDate(item.timestamp) }}</h3>
          <div v-for="attachment in item.attachments" :key="attachment.id">
            <el-image fit="scale-down" lazy :src="attachment.url" :alt="formatPrompt(item.content)"></el-image>
          </div>
        </div>
      </div>
    </div>
    <script>
      const App = {
        data() {
          return {
            question: "",
            msg_Id: "",
            loading: false,
            tasks: [],
            otherCase: [],
          };
        },
        mounted() {
          this.initTask();
          this.getOtherCase();
        },
        methods: {
          initTask() {
            this.tasks = JSON.parse(window.localStorage.getItem("task") || JSON.stringify([]));
          },
          submit() {
            if (!this.question) {
              this.$message.error("请输入图片内容");
              return;
            }
            this.msg_Id = grs();
            this.loading = true;
            axios({
              method: "post",
              url: "https://gghfpp.laf.dev/mj-func",
              data: {
                type: "imagine",
                param: {
                  msg_Id: this.msg_Id,
                  question: this.question,
                },
              },
            })
              .then((res) => {
                console.log("[ res ] >", res);
                this.$message.success("上传成功,正在画图,请稍后...");
                window.localStorage.setItem(
                  "task",
                  JSON.stringify([{ msg_Id: this.msg_Id, question: this.question, result: null }, ...this.tasks])
                );
                this.initTask();
                this.getResult(this.msg_Id);
              })
              .catch((err) => {
                console.log("[ err ] >", err);
              })
              .finally(() => {
                this.loading = false;
              });
          },
          getResult(msg_Id) {
            axios({
              method: "post",
              url: "https://gghfpp.laf.dev/mj-func",
              data: {
                type: "retrieveMessages",
              },
            })
              .then((res) => {
                console.log("[ res ] >", res);
                const { data } = res;
                const resIndex = data.findIndex((item) => item.content.includes(msg_Id));
                if (resIndex < 0) {
                  this.$message.success("正在努力生成,请耐心等待");
                  return;
                }
                const { id, attachments } = data[resIndex];
                if (!attachments.length) {
                  this.$message.success("正在努力生成,请耐心等待");
                  return;
                }
                const { pic } = attachments[0];
                if (pic) {
                  const myIndex = this.tasks.findIndex((item) => item.msg_Id == msg_Id);
                  this.tasks[myIndex].result = { id, url: pic };
                  window.localStorage.setItem("task", JSON.stringify(this.tasks));
                } else {
                  this.$message.success("正在努力生成,请耐心等待");
                  return;
                }
              })
              .catch((err) => {
                console.log("[ err ] >", err);
              });
          },
        variation({ result, question, msg_Id }, index) {
          console.log(result, question, index);
          // this.$message.warning('正在努力开发中...')
          // return
          const loadingInstance = this.$loading({
            lock: true,
            text: '正在变换图片...',
            spinner: 'el-icon-loading',
            background: 'rgba(0, 0, 0, 0.7)'
          });
          axios({
            method: 'post',
            url: 'https://gghfpp.laf.dev/mj-func',
            data: {
              type: 'variation',
              param: {
                id: result.id,
                url: result.url,
                question,
                index
              }
            }
          }).then(res => {
            const { data } = res
            console.log('[ variation ] >', data)
            window.localStorage.setItem('task', JSON.stringify([
              { msg_Id, question, result: { id: data.id, url: data.uri } },
              ...this.tasks
            ]))
            this.initTask()
          }).catch(err => {
            console.log('[ err ] >', err)
          }).finally(() => {
            loadingInstance.close();
          })
        },
        upscale({ result, question, msg_Id }, index) {
          console.log(result, question, index);
          // this.$loading('正在生成大图')
          const loadingInstance = this.$loading({
            lock: true,
            text: '正在生成大图...',
            spinner: 'el-icon-loading',
            background: 'rgba(0, 0, 0, 0.7)'
          });
          axios({
            method: 'post',
            url: 'https://gghfpp.laf.dev/mj-func',
            data: {
              type: 'upscale',
              param: {
                id: result.id,
                url: result.url,
                question,
                index
              }
            }
          }).then(res => {
            const { data } = res
            console.log('[ upscale data ] >', data)
            const taskIndex = this.tasks.findIndex(item => item.msg_Id == msg_Id)
            this.tasks[taskIndex].result.upscaleUrl = data.uri
            window.localStorage.setItem('task', JSON.stringify(this.tasks))
          }).catch(err => {
            console.log('[ err ] >', err)
          }).finally(() => {
            loadingInstance.close();
          })
        },

        getOtherCase() {
          axios({
            method: 'post',
            url: 'https://gghfpp.laf.dev/mj-func',
            data: {
              type: 'retrieveMessages'
            }
          }).then(res => {
            const { data } = res
            this.otherCase = data.filter(item => item.attachments.length).slice(0, 10)
          })
        },
        formatPrompt(str) {
          return str.substring(str.indexOf(']') + 1, str.indexOf('--seed')) + `(${str.substring(str.indexOf('[') + 1, str.indexOf(']'))})`
        },
        // 格式化日期时间
        padLeftZero(str) {
          return ('00' + str).substr(str.length);
        },
        formatDate(date, fmt = "yyyy-MM-dd hh:mm:ss") {
          if (/(y+)/.test(fmt)) {
            fmt = fmt.replace(RegExp.$1, (new Date(date).getFullYear() + '').substr(4 - RegExp.$1.length));
          }
          const o = {
            'M+': new Date(date).getMonth() + 1,
            'd+': new Date(date).getDate(),
            'h+': new Date(date).getHours(),
            'm+': new Date(date).getMinutes(),
            's+': new Date(date).getSeconds()
          }
          for (const k in o) {
            if (new RegExp(`(${k})`).test(fmt)) {
              const str = o[k] + '';
              fmt = fmt.replace(RegExp.$1, (RegExp.$1.length === 1) ? str : this.padLeftZero(str));
            }
          }
          return fmt;
        },
      }
    };
    const app = Vue.createApp(App);
    app.use(ElementPlus);
    app.mount("#app");

    function grs() {
      return 'Charlie-' + 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
        var r = Math.random() * 16 | 0,
          v = c == 'x' ? r : (r & 0x3 | 0x8);
        return v.toString(16);
      });
    }
  </script>
</body>

</html>