sd.js 4.1 KB
Newer Older
update  
杨龙伟 已提交
1 2 3 4 5 6
import axios from "axios";
import { fetchEventSource } from '@microsoft/fetch-event-source';
export const apiKey = process.env.INSCODE_API_KEY;

export const apiUrl = 'https://inscode-api.csdn.net/api/v1/gpt/';
export default {
杨龙伟 已提交
7
    draw (config) {
update  
杨龙伟 已提交
8 9
        return new Promise(function(resolve,rejcet){
            const data = {
杨龙伟 已提交
10
                prompt: config.prompt,
update  
杨龙伟 已提交
11
                steps: config?.steps??20,
Y
ylwdev 已提交
12
                negative_prompt: config.negative_prompt?config.negative_prompt+',nfsw,no underwear,tits':'nfsw,no underwear,tits',
update  
杨龙伟 已提交
13 14
                width: config?.width??512,
                height: config?.height??512,
杨龙伟 已提交
15 16 17 18
                cfg_scale: config.cfg_scale??6,
                seed : config.seed??-1,
                sampler_name: config.sampler_name??"DPM++ SDE Karras",
                sd_model_checkpoint:config.sd_model_checkpoint??"chilloutmix_NiPrunedFp32Fix"
update  
杨龙伟 已提交
19 20 21 22
            }
            const headers = {
                'content-type': 'application/json'
            };
Y
ylwdev 已提交
23
            let api = 'https://gpu-pod65d1a546eb9c3114aef516a1-6006.node.inscode.run/sdapi/v1/txt2img'
杨龙伟 已提交
24
            console.log('sd data',data)
update  
杨龙伟 已提交
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
            axios.post(api, data, { headers }).then(response => {
                if (response.status === 200 && response?.data?.images){
                    const image = response?.data?.images[0]
                    resolve('data:image/png;base64,' + image)
                }
            }).catch(err => {
                rejcet(err)
            });

        })
    },
    gpt(prompt){
        return new Promise(function(resolve,rejcet){
            const data = {
                'messages': [
                    {
                        'role': 'user',
                        'content': prompt
                    }],
                'stream_token': false,
                'apikey': apiKey
            }
            const headers = {
                'content-type': 'application/json'
            };
            axios.post(apiUrl, data, { headers }).then(response => {
杨龙伟 已提交
51 52 53
                console.log('response.data',response.data)
                if(response.data.indexOf("升级 InsCode Pro 会员")>-1){
                    rejcet("您的 AI API 免费次数已经用完,已无法使用涉及 AI 的应用,升级 InsCode Pro 会员(inscode.csdn.net)后,可免费使用平台内全部 AI 应用。")
update  
杨龙伟 已提交
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
                }else if(response.data.indexOf("敏感")>-1){
                    rejcet("抱歉,该问题含有敏感词信息,请换一个问题")
                }else{
                    resolve(response.data)
                }
            }).catch(err => {
                rejcet(err)
            });

        })
    },
    gpt2(prompt){
        return new Promise(function(resolve,rejcet){
            const body = {
                messages: [
                    {
                        role: 'user',
                        content: prompt
                    }
                ],
                apikey: apiKey
            }
            fetchEventSource(apiUrl, {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/json'
                },
                body: JSON.stringify(body),
                onopen: (response) => {
                    console.log('open',response)
                },
                onmessage: (msg) => {
                    if (msg.data === '[DONE]') {
                       // this.loading = false;
                        return;
                    };
                    const data = JSON.parse(msg.data);
                    const finish_reason = data.choices[0].finish_reason;
                    const finish = finish_reason === 'stop' || finish_reason === 'length';
                    const content = data.choices[0].delta.content;

                    if (finish) {
                       // this.loading = false;
                    } else if (content) {
                        console.log('gpt: response',content)
                        resolve(content)
                    }
                },
                onerror: (err) => {
                    rejcet(err)
                    console.log("error", err);
                }
            });
        })
    }
}